This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import crypto from 'crypto'; | |
/** | |
* Gets random bytes as Uint8Array | |
*/ | |
function randomBytes(size: number): Uint8Array { | |
return crypto.randomBytes(size); | |
} | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Mutex, MutexInterface } from 'async-mutex'; | |
type ObjectId = string; | |
type Object = number; | |
type ObjectMap = Map<ObjectId, { | |
object?: Object; | |
lock: MutexInterface; | |
}>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Mutex, MutexInterface } from 'async-mutex'; | |
type ObjectId = string; | |
type Object = number; | |
type ObjectMap = Map<ObjectId, { | |
object?: Object; | |
lock: MutexInterface; | |
}>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Use this when object lifetime matches object "readiness" | |
*/ | |
class X { | |
protected _destroyed: boolean = false; | |
public static async createX(): Promise<X> { | |
return new X; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// mutating input and output | |
const rec = (inputs: Array<number>, outputs: Array<number> = []) => { | |
if (!inputs.length) { | |
return outputs; | |
} else { | |
const input = inputs.shift()!; | |
outputs.push(input + 1); | |
return rec(inputs, outputs); | |
} | |
}; |