JSPM

ffn

2.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3712
  • Score
    100M100P100Q119435F
  • License MIT

Turns arrays and values into Filtering Functions.

Package Exports

  • ffn

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ffn) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ffn

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.

Installation

Requires Node.js 6.0.0 or above.

npm i ffn

API

The module exports a single function.

Parameters

  1. f (any): A Function, an Array, or some other value.
  2. Object argument:
    • Optional: blacklist (bool): Set to true if ffn is being used to create a blacklist. This will make falsey values test true when f is undefined. Defaults to false.

Return Values

  • If f is a Function: returns f
  • If f is omitted or otherwise undefined: returns a function that tests whether its argument is truthy (unless blacklist is true)
  • If f is an Array: returns a function that tests whether its argument is included in f
  • Otherwise: returns a function that tests whether its argument is strictly equal to f

Example

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.