@sonyahon/js-utils

0.0.5 • Public • Published

@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-utils

API

Misc

nop()

A noop function, yields undefined

import {nop} from '@sonyahon/js-utils';
nop(); // undefined

sleep(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 window is smaller than by elements 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, undefined will 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'

Keywords

Install

npm i @sonyahon/js-utils

DownloadsWeekly Downloads

10

Version

0.0.5

License

MIT

Unpacked Size

20.3 kB

Total Files

18

Last publish

Collaborators

  • sonyahon