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)
| 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; | |
| } | |
| } |
| 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 |
| import crypto from 'crypto'; | |
| /** | |
| * Gets random bytes as Uint8Array | |
| */ | |
| function randomBytes(size: number): Uint8Array { | |
| return crypto.randomBytes(size); | |
| } | |
| /** |
| 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; | |
| } | |
| } |
| import { Mutex, MutexInterface } from 'async-mutex'; | |
| type ObjectId = string; | |
| type Object = number; | |
| type ObjectMap = Map<ObjectId, { | |
| object?: Object; | |
| lock: MutexInterface; | |
| }>; |
| 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(); |
| import { Mutex, MutexInterface } from 'async-mutex'; | |
| type ObjectId = string; | |
| type Object = number; | |
| type ObjectMap = Map<ObjectId, { | |
| object?: Object; | |
| lock: MutexInterface; | |
| }>; |
| 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]> |
| /** | |
| * Use this when object lifetime matches object "readiness" | |
| */ | |
| class X { | |
| protected _destroyed: boolean = false; | |
| public static async createX(): Promise<X> { | |
| return new X; | |
| } |