Skip to content

A zero-dependency 📦 tree-shakable🌲 collection of missing JavaScript utilities.

License

Notifications You must be signed in to change notification settings

fullstacksjs/toolbox

Repository files navigation

logo


download status version MIT License

fullstacksjs/toolbox

Useful JavaScript utility functions

Installation

npm :

$ npm add --save-dev @fullstacksjs/toolbox

or using yarn

$ yarn add --dev @fullstacksjs/toolbox

Usage

Array

ensureArray: force a value to an array

ensureArray(value)

ensureArray(null);           // [null]
ensureArray(undefined);      // [undefined]
ensureArray('string');       // ['string']
ensureArray(['array']);      // ['array']
ensureArray([1, 2, 3]);      // [1, 2, 3]
ensureArray({ foo: 'bar' }); // [{ foo: 'bar' }]

create array of length n with from offset with an step

range(length, { offset, step })

range(0);                          // []
range(10);                         // [0,1,2,3,4,5,6,7,8,9,10]
range(3, { offset: 5 });           // [5,6,7]
range(3, { offset: -2, step: 10}); // [-2, 8, 18]

toArray: Convert a value to an array

toArray(value)

toArray(null);       // []
toArray(undefined);  // []
toArray('string');   // ['string']
toArray(['array']);  // ['array']
toArray(iteratable); // [...iteratable]
toArray(others);     // [others]

Env

  • getEnv: get environment variable or given fallback

getEnv(name, fallback)

// FOO=foo BAR=bar node
env.getEnv('FOO');        // foo
env.getEnv('BAR');        // undefined
env.getEnv('BAR', 'bar'); // bar
  • getRequiredEnv: get environment variable or given fallback or throw

getRequiredEnv(name, fallback)

// FOO=foo BAR=bar node
env.getRequiredEnv('FOO');        // foo
env.getRequiredEnv('BAR', 'bar'); // bar
env.getRequiredEnv('BAR');        // Error: BAR is required
  • getNodeEnv: give NODE_ENV value or given fallback value

getNodeEnv(fallback)

env.getNodeEnv();           // undefined
env.getNodeEnv('anything'); // 'anything'
// NODE_ENV=anything
env.getNodeEnv();           // null

is: strict check NODE_ENV with given value

// NODE_ENV=production node
env.is('development'); // false
env.is('production');  // true

match: check NODE_ENV starts with given value (case insensitive)

// NODE_ENV=production node
env.match('prod');        // true
env.match('PROD');        // true
env.match('ProductioN');  // true
env.match('duction');     // false
env.match('development'); // false

isDev: check env matches 'development'

// NODE_ENV=development node
env.isDev(); // true

isProd: check env matches 'production'

// NODE_ENV=production node
env.isProd(); // true

isTest: check env matches 'test'

// NODE_ENV=test node
env.isTest(); // true

Function

noop

No operation ¯\_(ツ)_/¯

  noop() // undefined

callAll

HOC that call all given functions

  callAll(foo, bar, baz)(...args)                         // void (foo(...args), bar(...args), baz(...args));
  callAll(bar,'string', 5, null, undefined, baz)(...args) // void (bar(...args), baz(...args));

Guards

isString

check given value is string or not

isString();      // false
isString(1);     // false
isString(['1']); // false
isString('1');   // true

isIterable

check given value is iterable or not

isIterable([]);                           // true
isIterable(new Map());                    // true
isIterable({ [Symbol.iterator]() { } }]); // true
isIterable('some');                       // true
isIterable({});                           // false
isIterable();                             // false
  • isNullOrEmpty: check given value is null/undefined/empty string or not
isNullOrEmpty();          // true
isNullOrEmpty(null);      // true
isNullOrEmpty(undefined); // true
isNullOrEmpty('');        // true
isNullOrEmpty('f');       // false
isNullOrEmpty(1);         // false
  • isFunction: check given value is function or not
isFunction(() => {}); // true
isFunction();         // false
isFunction(true);     // false

Numbers

safeDivide: divide two numbers returns fallback if result is not a finite number

safeDivide(4, 2);          // 2
safeDivide(4, 0);          // 0
safeDivide(null, NaN);     // 0
safeDivide(null, NaN, 10); // 10
safeDivide(4, 0, 'Oops');  // 'Oops'

clamp: clamp a number between two values

clamp :: number Range -> Number

clamp(-10);                      // 0
clamp(-10, { min: -5 });         // -5
clamp(10);                       // 1
clamp(30, { max: 20 });          // 20
clamp(5, { min: -20, max: 20 }); // 5

randomInt: generate random integer number between range.

randomRange();                      // <random> 0 -> Number.MAX_SAFE_INTEGER
randomRange({ min: -10, max: 50 }); // <random> -10 -> 50
randomRange({ max: 50 });           // <random> 0 -> 50
randomRange({ min: 50 });           // <random> 50 -> Number.MAX_SAFE_INTEGER

toInteger: parse string to integer (radix 10)

toInteger('100'); // 100
toInteger('1.42'); // 1.42

isInRange: Checks if num is between min and max (and including borders).

isInRange(100, [0, 50]); // false
isInRange(100, [0, 100]); // true
isInRange(100, [50, 150]); // true
isInRange(100, [100, 150]); // true

Credits