/metautil

Metarhia utilities 🧰

Primary LanguageJavaScriptMIT LicenseMIT

Metarhia utilities

ci status snyk npm version npm downloads/month npm downloads license

Usage

  • Install: npm install metautil
  • Require: const metautil = require('metautil')

Async utilities

  • toBool = [() => true, () => false]
    • Example: const created = await mkdir(path).then(...toBool);
  • timeout(msec: number, signal?: AbortSignal): Promise<void>
  • delay(msec: number, signal?: AbortSignal): Promise<void>
  • timeoutify(promise: Promise<unknown>, msec: number): Promise<unknown>

Crypto utilities

  • cryptoRandom(min?: number, max?: number): number
  • random(min?: number, max?: number): number
  • generateUUID(): string
  • generateKey(length: number, possible: string): string
  • crcToken(secret: string, key: string): string
  • generateToken(secret: string, characters: string, length: number): string
  • validateToken(secret: string, token: string): boolean
  • serializeHash(hash: Buffer, salt: Buffer): string
  • deserializeHash(phcString: string): HashInfo
  • hashPassword(password: string): Promise<string>
  • validatePassword(password: string, serHash: string): Promise<boolean>
  • md5(fileName: string): Promise<string>
  • getX509(cert: X509Certificate): Strings

Datetime utilities

  • duration(s: string | number): number
  • nowDate(date?: Date): string
  • nowDateTimeUTC(date?: Date, timeSep?: string): string
  • parseMonth(s: string): number
  • parseDay(s: string): number
  • parseEvery(s: string): Every
  • nextEvent(every: Every, date?: Date): number

Error utilities

  • Class Error
    • constructor(message: string, code: number)
  • isError(instance: object): boolean

File system utilities

  • directoryExists(path: string): Promise<boolean>
  • ensureDirectory(path: string): Promise<boolean>

HTTP utilities

  • parseHost(host?: string): string
  • parseParams(params: string): Cookies
  • parseCookies(cookie: string): Headers
  • parseRange(range: string): StreamRange

Network utilities

  • fetch(url: string, options?: FetchOptions): Promise<Response>
  • receiveBody(stream: IncomingMessage): Promise<Buffer | null>
  • ipToInt(ip?: string): number
  • intToIp(int: number): string
  • httpApiCall(url: string, options: ApiOptions): Promise<object>

Objects utilities

  • makePrivate(instance: object): object
  • protect(allowMixins: Strings, ...namespaces: Namespaces): void
  • jsonParse(buffer: Buffer): Dictionary | null
  • isHashObject(o: string | number | boolean | object): boolean
  • flatObject(source: Dictionary, fields: Strings): Dictionary
  • unflatObject(source: Dictionary, fields: Strings): Dictionary
  • getSignature(method: Function): Strings
  • namespaceByPath(namespace: Dictionary, path: string): Dictionary | null
  • serializeArguments(fields: Strings, args: Dictionary): string

Class Pool

  • constructor(options: { timeout?: number })
  • items: Array<unknown>
  • free: Array<boolean>
  • queue: Array<unknown>
  • current: number
  • size: number
  • available: number
  • timeout: number
  • next(): Promise<unknown>
  • add(item: unknown): void
  • capture(): Promise<unknown>
  • release(item: unknown): void
  • isFree(item: unknown): boolean

Array utilities

sample(array: Array<unknown>): unknown

const cards = ['🂡', '🃒', '🂮', '🂷', '🃚'];
const card = sample(cards);

shuffle(array: Array<unknown>): Array<unknown>

const players = [{ id: 10 }, { id: 12 }, { id: 15 }];
const places = shuffle(players);

projection(source: object, fields: Array<string>): Array<unknown>

const player = { name: 'Marcus', score: 1500, socket };
const playerState = projection(player, ['name', 'score']);

Class Semaphore

  • constructor(concurrency: number, size?: number, timeout?: number)
  • concurrency: number
  • counter: number
  • timeout: number
  • size: number
  • empty: boolean
  • queue: Array<QueueElement>
  • enter(): Promise<void>
  • leave(): void

Strings utilities

  • replace(str: string, substr: string, newstr: string): string
  • between(s: string, prefix: string, suffix: string): string
  • split(s: string, separator: string): [string, string]
  • isFirstUpper(s: string): boolean
  • isFirstLower(s: string): boolean
  • isFirstLetter(s: string): boolean
  • toLowerCamel(s: string): string
  • toUpperCamel(s: string): string
  • toLower(s: string): string
  • toCamel(separator: string): (s: string) => string
  • spinalToCamel(s: string): string
  • snakeToCamel(s: string): string
  • isConstant(s: string): boolean
  • fileExt(fileName: string): string
  • parsePath(relPath: string): Strings
  • trimLines(s: string): string

Units utilities

  • bytesToSize(bytes: number): string
  • sizeToBytes(size: string): number

License & Contributors

Copyright (c) 2017-2023 Metarhia contributors. Metautil is MIT licensed.
Metautil is a part of Metarhia technology stack.