0.0.10 • Published 3 years ago
@sonyahon/js-utils v0.0.10
@sonyahon/js-utils
A collection of functions i use reguarly and don't want to write again
Installation
npm install --save @sonyahon/js-utils
yarn add @sonyahon/js-utilsAPI
Misc
nop()
A noop function, yields undefined
import {nop} from '@sonyahon/js-utils';
nop(); // undefinedsleep(delay: number, resolveValue?: any)
Yields are Promise, which resolves after the delay ms. The promise yields resolveValue, or undefined if not supplied.
import {sleep} from '@sonyahon/js-utils';
(async () => {
await sleep(100); // undefined after 100ms
await sleep(100, 'done'); // 'done' after 100ms
})();Array manipulations
partition(target: any[], by: number, window = by)
Partitions array with optional padding
- if
windowis smaller thanbyelements will be reused - if there is not enough elements in the
target, last partition will be smaller
import {partition} from '@sonyahon/js-utils';
partition([1, 2, 3, 4], 2); // [[1, 2], [3, 4]]
partition([1, 2, 3, 4], 3, 1); // [[1, 2, 3], [2, 3, 4]]zip(...arrays: any[][])
Zips passed arrays together
- if lengths of passed arrays is not equal,
undefinedwill be used instead of missing elements
import {zip} from '@sonyahon/js-utils';
zip(['a', 'b'], [1, 2]); // [['a', 1], ['b', 2]]String
capitalize(str: string)
Capitalizes a string
import {capitalize} from '@sonyahon/js-utils';
capitalize(''); // ''
capitalize('hello'); // 'Hello'Events
An implementation of EventEmitter, with a bit more flexible API than regular node/browser one
Error
An implementation of Rust-like Result<Ok, Err> type