Turns arrays and values into Filtering Functions.
Many projects use callbacks to filter values. Sometimes this filtering involves complex conditions that need a function. Other times one just wants to test if the value equals another, or if the value appears in an array of acceptable values. Instead of making your project’s end-users write filters like x => x === 'value'
or x => ['ok', 'also ok'].includes(x)
, let them provide the value or the array directly, and ffn
will turn it into a filtering function.
Requires Node.js 6.0.0 or above.
npm i ffn
The module exports a single function.
f
(any): A Function, an Array, or some other value.- Object argument:
- Optional:
blacklist
(bool): Set totrue
ifffn
is being used to create a blacklist. This will make falsey values test true whenf
is undefined. Defaults tofalse
.
- Optional:
- If
f
is a Function: returnsf
- If
f
is omitted or otherwise undefined: returns a function that tests whether its argument is truthy (unlessblacklist
istrue
) - If
f
is an Array: returns a function that tests whether its argument is included inf
- Otherwise: returns a function that tests whether its argument is strictly equal to
f
const ffn = require('ffn')
const arr = [1, 2, 3, 4, 5]
// ffn returns functions as-is
arr.find(ffn(x => x % 2 === 0)) // 2
// ffn creates a function which checks for inclusion in the array
arr.find(ffn([4, 5])) // 4
// ffn creates a function which checks for equality with the value
arr.find(ffn(3)) // 3
For more projects like this, check out the xfn family of modules.