Skip to content

Instantly share code, notes, and snippets.

Avatar
🚀
Lightspeed

Roger Qiu CMCDragonkai

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

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 }];
View validate_public_key_noble.ts
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 Oct 3, 2022
Get Random Bytes from Webcrypto #webcrypto #js
View random.ts
/**
* 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 Jul 24, 2022
Migrate Issues from GitLab to GitHub
View issue-migrate.sh
#!/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 Jul 5, 2022
TypeScript Let Rec (Asynchronous Recursive Data Structures) #javascript #typescript
View typescript_let_rec.md

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 Jun 19, 2022
Reload the DNS Cache in Network Manager
View reload_dns_cache_network_manager.md

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 Feb 10, 2022
Compound Growth (Interest) Rate
View compound_growth_rate.md

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 Feb 1, 2022
General Data Validation #typescript #javascript
View parse.ts
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 Jan 16, 2022
EventBus with `emitAsync` #javascript #eventemitter
View EventBus.ts
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 Oct 11, 2021
Random bytes and bits for Node.js #javascript #typescript
View random_bytes_bits.ts
import crypto from 'crypto';
/**
* Gets random bytes as Uint8Array
*/
function randomBytes(size: number): Uint8Array {
return crypto.randomBytes(size);
}
/**