- Install:
npm install metautil - Require:
const metautil = require('metautil')
toBool = [() => true, () => false]- Example:
const created = await mkdir(path).then(...toBool);
- Example:
timeout(msec: number, signal?: AbortSignal): Promise<void>delay(msec: number, signal?: AbortSignal): Promise<void>timeoutify(promise: Promise<unknown>, msec: number): Promise<unknown>collect(keys: Array<string>, options?: CollectorOptions): Collectoroptions.exact?: booleanoptions.timeout?: numberoptions.reassign?: boolean
Async collection is an utility to collect needed keys and signalize on done.
constructor(keys: Array<string>, options?: CollectorOptions)options.exact?: booleanoptions.timeout?: numberoptions.reassign?: booleanoptions.defaults?: objectoptions.validate?: (data: Record<string, unknown>) => unknown
set(key: string, value: unknown): voidwait(key: string, fn: AsyncFunction | Promise<unknown>, ...args?: Array<unknown>): voidtake(key: string, fn: Function, ...args?: Array<unknown>): voidcollect(sources: Record<string, Collector>): voidfail(error: Error): voidabort(): voidthen(onFulfilled: Function, onRejected?: Function): Promise<unknown>done: booleandata: Dictionarykeys: Array<string>count: numberexact: booleantimeout: numberdefaults: objectreassign: booleanvalidate?: (data: Record<string, unknown>) => unknownsignal: AbortSignal
Collect keys with .set method:
const ac = collect(['userName', 'fileName']);
setTimeout(() => ac.set('fileName', 'marcus.txt'), 100);
setTimeout(() => ac.set('userName', 'Marcus'), 200);
const result = await ac;
console.log(result);Collect keys with .wait method from async or promise-returning function:
const ac = collect(['user', 'file']);
ac.wait('file', getFilePromisified, 'marcus.txt');
ac.wait('user', getUserPromisified, 'Marcus');
try {
const result = await ac;
console.log(result);
} catch (error) {
console.error(error);
}Collect keys with .take method from callback-last-error-first function:
const ac = collect(['user', 'file'], { timeout: 2000, exact: false });
ac.take('file', getFileCallback, 'marcus.txt');
ac.take('user', getUserCallback, 'Marcus');
const result = await ac;Set default values โโfor unset keys using the options.defaults argument:
const defaults = { key1: 'sub1', key2: 'sub1' };
const dc = collect(['key1', 'key2'], { defaults, timeout: 2000 });
dc.set('key2', 'sub2');
const result = await dc;Compose collectors (collect subkeys from multiple sources):
const dc = collect(['key1', 'key2', 'key3']);
const key1 = collect(['sub1']);
const key3 = collect(['sub3']);
dc.collect({ key1, key3 });
const result = await dc;Complex example: compare Promise.allSettled + Promise.race vs Collector in next two examples:
// Collect 4 keys from different contracts with Promise.allSettled + Promise.race
const promise1 = new Promise((resolve, reject) => {
fs.readFile('README.md', (err, data) => {
if (err) return void reject(err);
resolve(data);
});
});
const promise2 = fs.promises.readFile('README.md');
const url = 'http://worldtimeapi.org/api/timezone/Europe';
const promise3 = fetch(url).then((data) => data.json());
const promise4 = new Promise((resolve) => {
setTimeout(() => resolve('value4'), 50);
});
const timeout = new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('Timed out')), 1000);
});
const data = Promise.allSettled([promise1, promise2, promise3, promise4]);
try {
const keys = await Promise.race([data, timeout]);
const [key1, key2, key3, key4] = keys.map(({ value }) => value);
const result = { key1, key2, key3, key4 };
console.log(result);
} catch (err) {
console.log(err);
}Compare with:
// Collect 4 keys from different contracts with Collector
const dc = collect(['key1', 'key2', 'key3', 'key4'], { timeout: 1000 });
dc.take('key1', fs.readFile, 'README.md');
dc.wait('key2', fs.promises.readFile, 'README.md');
const url = 'http://worldtimeapi.org/api/timezone/Europe';
dc.wait(
'key3',
fetch(url).then((data) => data.json()),
);
setTimeout(() => dc.set('key4', 'value4'), 50);
try {
const result = await dc;
console.log(result);
} catch (err) {
console.log(err);
}cryptoRandom(min?: number, max?: number): numberrandom(min?: number, max?: number): numbergenerateUUID(): stringgenerateKey(possible: string, length: number): stringcrcToken(secret: string, key: string): stringgenerateToken(secret: string, characters: string, length: number): stringvalidateToken(secret: string, token: string): booleanserializeHash(hash: Buffer, salt: Buffer): stringdeserializeHash(phcString: string): HashInfohashPassword(password: string): Promise<string>validatePassword(password: string, serHash: string): Promise<boolean>md5(fileName: string): Promise<string>getX509(cert: X509Certificate): Strings
const x509 = new crypto.X509Certificate(cert);
const domains = metautil.getX509names(x509);duration(s: string | number): numbernowDate(date?: Date): stringnowDateTimeUTC(date?: Date, timeSep?: string): stringparseMonth(s: string): numberparseDay(s: string): numberparseEvery(s: string): EverynextEvent(every: Every, date?: Date): number
- Class
Errorconstructor(message: string, options?: number | string | ErrorOptions)options.code?: number | stringoptions.cause?: Error
message: stringstack: stringcode?: number | stringcause?: Error
- Class
DomainErrorconstructor(code?: string, options?: number | string | ErrorOptions)options.code?: number | stringoptions.cause?: Error
message: stringstack: stringcode?: number | stringcause?: ErrortoError(errors: Errors): Error
isError(instance: object): boolean
exists(path: string): Promise<boolean>directoryExists(path: string): Promise<boolean>fileExists(path: string): Promise<boolean>ensureDirectory(path: string): Promise<boolean>parsePath(relPath: string): Strings
parseHost(host?: string): stringparseParams(params: string): CookiesparseCookies(cookie: string): HeadersparseRange(range: string): StreamRange
- Deprecated in 4.x:
fetch(url: string, options?: FetchOptions): Promise<Response> receiveBody(stream: IncomingMessage): Promise<Buffer | null>ipToInt(ip?: string): numberintToIp(int: number): stringhttpApiCall(url: string, options: ApiOptions): Promise<object>options.method?: HttpMethodoptions.headers?: objectoptions.body?: Body
makePrivate(instance: object): objectprotect(allowMixins: Strings, ...namespaces: Namespaces): voidjsonParse(buffer: Buffer): Dictionary | nullisHashObject(o: string | number | boolean | object): booleanflatObject(source: Dictionary, fields: Strings): DictionaryunflatObject(source: Dictionary, fields: Strings): DictionarygetSignature(method: Function): StringsnamespaceByPath(namespace: Dictionary, path: string): Dictionary | nullserializeArguments(fields: Strings, args: Dictionary): stringfirstKey(obj: Dictionary): string | undefinedisInstanceOf(obj: unknown, constrName: string): boolean
constructor(options: PoolOptions)options.timeout?: number
items: Array<unknown>free: Array<boolean>queue: Array<unknown>current: numbersize: numberavailable: numbertimeout: numbernext(): Promise<unknown>add(item: unknown): voidcapture(): Promise<unknown>release(item: unknown): voidisFree(item: unknown): boolean
const pool = new metautil.Pool();
pool.add({ a: 1 });
pool.add({ a: 2 });
pool.add({ a: 3 });
if (pool.isFree(obj1)) console.log('1 is free');
const item = await pool.capture();
if (pool.isFree(obj1)) console.log('1 is captured');
const obj = await pool.next();
// obj is { a: 2 }
pool.release(item);const cards = ['๐ก', '๐', '๐ฎ', '๐ท', '๐'];
const card = sample(cards);const players = [{ id: 10 }, { id: 12 }, { id: 15 }];
const places = shuffle(players);const player = { name: 'Marcus', score: 1500, socket };
const playerState = projection(player, ['name', 'score']);constructor(options: SemaphoreOptions)options.concurrency: numberoptions.size?: numberoptions.timeout?: number
concurrency: numbercounter: numbertimeout: numbersize: numberempty: booleanqueue: Array<QueueElement>enter(): Promise<void>leave(): void
const options = { concurrency: 3, size: 4, timeout: 1500 };
const semaphore = new Semaphore(options);
await semaphore.enter();
// Do something
semaphore.leave();replace(str: string, substr: string, newstr: string): stringbetween(s: string, prefix: string, suffix: string): stringsplit(s: string, separator: string): [string, string]isFirstUpper(s: string): booleanisFirstLower(s: string): booleanisFirstLetter(s: string): booleantoLowerCamel(s: string): stringtoUpperCamel(s: string): stringtoLower(s: string): stringtoCamel(separator: string): (s: string) => stringspinalToCamel(s: string): stringsnakeToCamel(s: string): stringisConstant(s: string): booleanfileExt(fileName: string): stringparsePath(relPath: string): StringstrimLines(s: string): string
bytesToSize(bytes: number): stringsizeToBytes(size: string): number
const size = bytesToSize(100000000);
const bytes = sizeToBytes(size);
console.log({ size, bytes });
// { size: '100 MB', bytes: 100000000 }| Symbol | zeros | Unit |
|---|---|---|
| yb | 24 | yottabyte |
| zb | 21 | zettabyte |
| eb | 18 | exabyte |
| pb | 15 | petabyte |
| tb | 12 | terabyte |
| gb | 9 | gigabyte |
| mb | 6 | megabyte |
| kb | 3 | kilobyte |
- Events:
constructor(options?: { maxListeners?: number })emit(eventName: EventName, data: unknown): Promise<void>on(eventName: EventName, listener: Listener): voidonce(eventName: EventName, listener: Listener): voidoff(eventName: EventName, listener?: Listener): void
- Adapters:
toPromise(eventName: EventName): Promise<unknown>toAsyncIterable(eventName: EventName): AsyncIterable<unknown>
- Utilities:
clear(eventName?: EventName): voidlisteners(eventName?: EventName): Listener[]listenerCount(eventName?: EventName): numbereventNames(): EventName[]
Examples:
const ee = new Emitter();
ee.on('eventA', (data) => {
console.log({ data });
// Prints: { data: 'value' }
});
ee.emit('eventA', 'value');const ee = new Emitter();
setTimeout(() => {
ee.emit('eventA', 'value');
}, 100);
const result = await ee.toPromise('eventA');const ee = new Emitter();
passReferenceSomewhere(ee);
const iterable = ee.toAsyncIterable('eventB');
for await (const eventData of iterable) {
console.log({ eventData });
}Copyright (c) 2017-2025 Metarhia contributors.
Metautil is MIT licensed.
Metautil is a part of Metarhia technology stack.