0.3.11 • Published 4 years ago

@frontendmonster/utils v0.3.11

Weekly downloads
30
License
MIT
Repository
github
Last release
4 years ago

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
0.3.11

4 years ago

0.3.10

4 years ago

0.3.9

4 years ago

0.3.8

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.2

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.1

4 years ago

0.1.1

5 years ago

0.0.3

5 years ago