Go Functions
Utility functions for functions.
- version: 1.0.0
- license: GNU LGPLv3
Installation
npm i go-fns
or
yarn add go-fns
Usage
ES6
import { not } from 'go-fns'
[1, 2, 3.1].filter(not(Number.isInteger)); // => [3.1]
Node
const { not } = require('go-fns');
[1, 2, 3.1].filter(not(Number.isInteger)); // => [3.1]
Web browser
<script src="dist/go-fns.min.js"></script>
<script>
const { not } = Functions;
[1, 2, 3.1].filter(not(Number.isInteger)); // => [3.1]
</script>
Documentation
Table of Contents
Utility
pipe
Creates a function that executes a series of functions in the order provided. First it passes the parameters to the first function and then the output is used as the input for the next function in the sequence, and so on until it reaches the end and finally it returns the output of the last function.
Parameters
-
fns
...Function The functions to execute sequentially.
Examples
pipe(Math.floor)(4.5); // => 4
pipe(Math.floor, Math.sqrt)(4.5); // => 2
- Throws TypeError if any of the given values is not a function, null or undefined.
Returns Function The new function that sequentially executes the given functions.
Meta
- since: 1.0.0
spread
- See: Spread operator
- See: Function.prototype.apply
Creates a function that takes an array of values and passes the values to the given function as individual parameters.
Parameters
-
fn
Function The function to execute with individual parameters.
Examples
function sum(a, b) {
return a + b;
}
spread(sum)([1, 2]); // => 3
- Throws TypeError if
fn
is not a function.
Returns Function The new function that passes an array of values to the given function as individual parameters.
Meta
- since: 1.0.0
attempt
Attempts to execute a function and returns the result or if there was an error, the error object caught.
Parameters
-
fn
Function The function to attempt. -
args
...any? The arguments for the function.
Examples
attempt(JSON.parse, "1"); // => 1
attempt(JSON.parse, ""); // => SyntaxError
- Throws TypeError if
fn
is not a function.
Returns (any | Error) The result of executing the function or the error object caught if there was an error.
Meta
- since: 1.0.0
repeat
Executes a function for a specified number of times and returns the results in a new array.
Parameters
-
fn
Function The function to execute. -
count
number The number of times to execute. (optional, default0
) -
args
...any? The arguments for the function.
Examples
repeat(Math.floor, 3, 1.5); // => [1, 1, 1]
- Throws TypeError if
fn
is not a function.
Returns Array The new array containing the results.
Meta
- since: 1.0.0
delay
- See: setTimeout
Executes a function after a specified number of milliseconds with the arguments provided.
Parameters
-
fn
Function The function to execute after a delay. -
delay
number The number of milliseconds to wait before executing the function. (optional, default0
) -
args
...any? The arguments for the function.
Examples
delay(log, 1000, "hi"); // => prints "hi" after 1 second.
- Throws TypeError if
fn
is not a function.
Returns number The timer id.
Meta
- since: 1.0.0
Logic
Propositional logic connectives
and
Creates a function that tests if all the given functions return a truthy value. The parameters you pass when invoking the created function will be passed down to the given functions with the this binding of the respective function.
Parameters
-
fns
...Function The functions to test.
Examples
and(isString, isPrimitive)("abc"); // => true
and(isString, isPrimitive)(new String("abc")); // => false
- Throws TypeError if any of the given values is not a function, null or undefined.
Returns Function The new function that tests if all the given functions return a truthy value.
Meta
- since: 1.0.0
or
Creates a function that tests if any of the given functions return a truthy value. The parameters you pass when invoking the created function will be passed down to the given functions with the this binding of the respective function.
Parameters
-
fns
...Function The functions to test.
Examples
or(isString, isNumber)("abc"); // => true
or(isString, isNumber)(true); // => false
- Throws TypeError if any of the given values is not a function, null or undefined.
Returns Function The new function that tests if any of the functions return a truthy value.
Meta
- since: 1.0.0
xor
Creates a function that tests if exactly one of the given functions returns a truthy value. The parameters you pass when invoking the created function will be passed down to the given functions with the this binding of the respective function.
Parameters
-
fns
...Function The functions to test.
Examples
xor(isString, isNumber)("abc"); // => true
xor(isString, isNumber)(1); // => true
xor(isString, isNumber)(true); // => false
- Throws TypeError if any of the given values is not a function, null or undefined.
Returns Function The new function that tests if exactly one of the given functions return a truthy value.
Meta
- since: 1.0.0
nor
Creates a function that tests if all the given functions return a falsy value. The parameters you pass when invoking the created function will be passed down to the given functions with the this binding of the respective function.
Parameters
-
fns
...Function The functions to test.
Examples
nor(isString, isNumber)(true); // => true
nor(isString, isNumber)("abc"); // => false
- Throws TypeError if any of the given values is not a function, null or undefined.
Returns Function The new function that tests if all the given functions return a falsy value.
Meta
- since: 1.0.0
not
Creates a function that negates the result of the given function. The parameters you pass when invoking the created function will be passed down to the given function with the this binding of the given function.
Parameters
-
fn
Function The function to negate.
Examples
not(isString)(1); // => true
not(isString)("abc"); // => false
- Throws TypeError if the given value is not a function.
Returns Function The new function that negates the result of the given function.
Meta
- since: 1.0.0