Skip to content

Instantly share code, notes, and snippets.

Avatar
🚀
Lightspeed

Roger Qiu CMCDragonkai

🚀
Lightspeed
View GitHub Profile
@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);
}
/**
@CMCDragonkai
CMCDragonkai / range.ts
Last active Oct 10, 2021
Generators and Iterators in JavaScript #javascript #typescript
View range.ts
function* range(start: number, stop?: number, step = 1): Generator<number> {
if (stop == null) {
stop = start;
start = 0;
}
for (let i = start; step > 0 ? i < stop : i > stop; i += step) {
yield i;
}
}
@CMCDragonkai
CMCDragonkai / object_map.ts
Created Oct 5, 2021
Singleton Object Map using Locking #typescript #javascript
View object_map.ts
import { Mutex, MutexInterface } from 'async-mutex';
type ObjectId = string;
type Object = number;
type ObjectMap = Map<ObjectId, {
object?: Object;
lock: MutexInterface;
}>;
@CMCDragonkai
CMCDragonkai / rwlock-read.ts
Last active Jun 22, 2022
Read Write Lock #javascript #typescript #concurrency
View rwlock-read.ts
import type { MutexInterface } from 'async-mutex';
import { Mutex } from 'async-mutex';
/**
* Single threaded read-preferring read write lock
*/
class RWLock {
protected _readerCount: number = 0;
protected _writerCount: number = 0;
protected lock: Mutex = new Mutex();
@CMCDragonkai
CMCDragonkai / concurrent_coalesced_async_construction.ts
Created Sep 28, 2021
Concurrently Coalesced Asynchronous Construction #typescript
View concurrent_coalesced_async_construction.ts
import { Mutex, MutexInterface } from 'async-mutex';
type ObjectId = string;
type Object = number;
type ObjectMap = Map<ObjectId, {
object?: Object;
lock: MutexInterface;
}>;
@CMCDragonkai
CMCDragonkai / hybrid_methods.ts
Last active Oct 19, 2021
Hybrid Promise and Callback Methods #javascript #typescript
View hybrid_methods.ts
import callbackify from 'util-callbackify';
type Callback<P extends Array<any> = [], R = any, E extends Error = Error> = {
(e: E, ...params: Partial<P>): R;
(e?: null | undefined, ...params: P): R;
};
async function maybeCallback<T>(
f: () => Promise<T>,
callback?: Callback<[T]>