call
can be installed with NPM or Yarn.
# Installing with NPM
npm i --save @ninetynine/call
# Installing with Yarn
yarn add @ninetynine/call
call
provides a helper that makes it easy to call single or multiple functions.
const call = require('@ninetynine/call')
function one (text) {
console.log('1. %s', text)
}
function two (text) {
console.log('2. %s', text)
}
const three = null
call(one, 'Hello, world')
// > 1. 'Hello, world'
call([one, two], 'Hello, world')
// > 1. 'Hello, world'
// > 2. 'Hello, world'
call(three, 'Hello, world')
// > Ignored
call([one, two, three], 'Hello, world')
// > 1. 'Hello, world'
// > 2. 'Hello, world'
// > Ignored
call
also provides a helper that makes it easy to chain functions together while transforming the database from each different function, this is called chain
.
The first function in a chain gets passed the arguments given to chain
. Then each function after that gets giving the returned variable from the previous function and the arguments given to chain
.
const { chain } = require('@ninetynine/call')
function one (text) {
return `${text}, `
}
function two (text, original) {
// text: 'Hello, '
// original: 'Hello'
return `${text}World`
}
chain(one, 'Hello')
// > 'Hello, '
chain([one, two], 'Hello')
// > 'Hello, World'
call
uses some internal functions in call
and chain
. They can be accessed externally as well.
argsToArray
simply wraps a non-array variable in an array
. This helps to standardize variables to allow for the spread operator.
const { argsToArray } = require('@ninetynine/call')
const var = 'Hello, World'
argsToArray(var)
// > ['Hello, World']
isCallable
checks that a variable is either an array
or function
. This is used to filter out invalid functions in call
and chain
.
const { isCallable } = require('@ninetynine/call')
const noop = require('@ninetynine/noop')
const fn1 = noop
const fn2 = undefined
isCallable(fn1)
// > true
isCallable(fn2)
// > false