@frontendmonster/utils
v0.3.11
Published
Common JavaScript utility functions
Downloads
7
Readme
Utils
Common JavaScript utility functions
Arrays
- forceArray: force a value to an array
forceArray(null); //=> [null]
forceArray(undefined); //=> [undefined]
forceArray('string'); //=> ['string']
forceArray(['array']); //=> ['array']
forceArray({ foo: 'bar' }); //=> [{ foo: 'bar' }]
- toArray: Convert a value to an array
toArray(null); //=> []
toArray(undefined); //=> []
toArray('string'); //=> ['string']
toArray(['array']); //=> ['array']
toArray(itratable); //=> [...iteratable]
toArray(others); //=> [others]
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 (baz(args));
Numbers
- 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)
import { toInteger } from '@frontendmonster/utils';
toInteger('100'); //=> 100
toInteger('1.42'); //=> 1.42
- isInRange: Checks if num is between min and max (and including borders).
import { isInRange } from '@frontendmonster/utils';
isInRange(100, [0, 50]); //=> false
isInRange(100, [0, 100]); //=> true
isInRange(100, [50, 150]); //=> true
isInRange(100, [100, 150]); //=> true
Env
- get: give NODE_ENV value or given fallback value
// $ NODE_ENV=foo node
import env from '@frontendmonster/utils';
env.get(); //=> 'foo'
env.get('fallback'); //=> 'foo'
// $ node
import env from '@frontendmonster/utils';
env.get(); //=> 'development'
env.get(undefined); //=> undefined
env.get(null); //=> null
env.get('anything'); //=> 'anything'
- is: strict check NODE_ENV with given value
// NODE_ENV=proudction node
import env from '@frontendmonster/utils';
env.is('development'); //=> false
env.is('production'); //=> true
- match: check NODE_ENV starts with given value (case insensitive)
// NODE_ENV=proudction node
import env from '@frontendmonster/utils';
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
import env from '@frontendmonster/utils';
env.isDev(); //=> true
- isProd: check env matches 'production'
// NODE_ENV=production node
import env from '@frontendmonster/utils';
env.isProd(); //=> true
- isTest: check env matches 'test'
// NODE_ENV=test node
import env from '@frontendmonster/utils';
env.isTest(); //=> true