Skip to content

Instantly share code, notes, and snippets.

View CMCDragonkai's full-sized avatar
🚀
Lightspeed

Roger Qiu CMCDragonkai

🚀
Lightspeed
View GitHub Profile
@CMCDragonkai
CMCDragonkai / tagged_unions_in_typescript.md
Created November 6, 2022 05:50
Tagged Unions in TypeScript #typescript #adt

There are number of ways of creating tagged unions.

type X = { type: A, prop: number } | { type: B, prop: string };

type Y = { type: A, data: { prop: number } } | { type: B, data: { prop: string } };

type Z = ['A', { prop: number }] | ['B', { prop: string }];
@CMCDragonkai
CMCDragonkai / validate_public_key_noble.ts
Created October 5, 2022 05:48
Validate Public Key #ed25519
import * as nobleEd25519 from '@noble/ed25519';
/**
* Checks if the public key is a point on the Ed25519 curve
*/
function validatePublicKey(publicKey: Buffer): boolean {
try {
nobleEd25519.Point.fromHex(publicKey);
return true;
} catch {
@CMCDragonkai
CMCDragonkai / random.ts
Last active October 3, 2022 02:21
Get Random Bytes from Webcrypto #webcrypto #js
/**
* Webcrypto random bytes is limited to 65,536 in one call.
* The below functions will make repeated calls to acquire random bytes.
*/
const webcrypto = globalThis.crypto?.webcrypto || globalThis.window?.crypto;
async function sleep(ms: number): Promise<void> {
return await new Promise<void>((r) => setTimeout(r, ms));
}
@CMCDragonkai
CMCDragonkai / issue-migrate.sh
Last active July 24, 2022 05:58
Migrate Issues from GitLab to GitHub
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
shopt -s inherit_errexit
count=0
glab api 'projects/:fullpath/issues?state=opened' --paginate | jq -c '.[]' | while read -r issue_object; do
@CMCDragonkai
CMCDragonkai / typescript_let_rec.md
Last active July 5, 2022 05:42
TypeScript Let Rec (Asynchronous Recursive Data Structures) #javascript #typescript

Imagine this...

async function main () {
  const config = await (async (
    a = 'a',
    b = 'b',
    c = `${a}/${b}`,
    d = ((
 e = 'e',
@CMCDragonkai
CMCDragonkai / reload_dns_cache_network_manager.md
Created June 19, 2022 06:39
Reload the DNS Cache in Network Manager

Reload the DNS Cache in Network Manager

You may be using NetworkManager with dnsmasq as the DNS plugin. If so, it's being used as a local caching nameserver.

When its data is outdated, you can force it to reload its DNS cache with:

Use:

nmcli general reload dns-full
@CMCDragonkai
CMCDragonkai / compound_growth_rate.md
Created February 10, 2022 00:45
Compound Growth (Interest) Rate

Compound Growth (Interest) Rate

Suppose something grew 300% over 20 years, and you want to know what was the annual growth rate.

Use the compound interest rate formula:

A = P(1 + r/n)^(n*t)
@CMCDragonkai
CMCDragonkai / parse.ts
Last active February 1, 2022 01:06
General Data Validation #typescript #javascript
import { CustomError } from 'ts-custom-error';
class ErrorParse extends CustomError {
public readonly errors: Array<ErrorRule>;
constructor(errors: Array<ErrorRule>) {
const message = errors.map((e) => e.message).join('; ');
super(message);
this.errors = errors;
}
}
@CMCDragonkai
CMCDragonkai / EventBus.ts
Created January 16, 2022 08:47
EventBus with `emitAsync` #javascript #eventemitter
import { EventEmitter } from 'events';
class EventBus extends EventEmitter {
protected kCapture: symbol;
constructor (...args: ConstructorParameters<typeof EventEmitter>) {
super(...args);
// EventEmitter's captureRejections option is only accessible through a private symbol
// Here we augment the construction and save it as a property
@CMCDragonkai
CMCDragonkai / random_bytes_bits.ts
Created October 11, 2021 03:47
Random bytes and bits for Node.js #javascript #typescript
import crypto from 'crypto';
/**
* Gets random bytes as Uint8Array
*/
function randomBytes(size: number): Uint8Array {
return crypto.randomBytes(size);
}
/**