Extends a function object with configured versions of itself.
Requires Node.js 7.0.0 or above.
npm i xfn
xfn
simplifies the process of creating singular and plural versions of the same function (e.g. get()
and get.all()
). The function that you write is the plural version, and xfn
creates the singular version. The following example uses each
as the name of the plural function:
const xfn = require('xfn')
const getFirstOf = xfn({
pluralProp: 'each',
pluralReturn: true,
}, arrs => arrs.map(arr => arr[0]))
const arr = [[1, 2], ['a', 'b']]
getFirstOf(arr) // [1, 2]
getFirstOf.each(arr) // [[1], ['a']]
The first call treats arr
as a single array, while the second call treats arr
as an array of arrays.
xfn
lets you create subfunctions that have certain options preconfigured. For example, you can replace fn(arg, {option: true})
with fn.option(arg)
.
const getOwnProperty = require('get-own-property')
const xfn = require('xfn')
const get = xfn({
optionArg: 2, // The index of the parameter that contains the options
optionProps: {own: {own: true}},
}, (obj, key, {own} = {}) => own ? getOwnProperty(obj, key) : obj[key])
class Cls {
get inherited () { return 123 }
}
const obj = new Cls()
obj.mine = 456
get(obj, 'mine') // 456
get(obj, 'inherited') // 123
get.own(obj, 'mine') // 456
get.own(obj, 'inherited') // undefined
The module exports a single function.
- Object argument:
- Optional:
pluralArg
(positive integer): The zero-based index of the argument that should be singularized ifpluralProp
is set. Defaults to0
. - Optional:
pluralFirst
(boolean): Iftrue
, thepluralProp
comes before theoptionProps
when they are both set. Defaults tofalse
. (For example: apluralProp
ofall
and anoptionProps
key ofin
will result in a property chain ofall.in()
iftrue
, orin.all()
iffalse
.) - Optional:
pluralProp
(string or symbol): If set, the returned function will be a singularized version offn
, and the originalfn
will be attached to thepluralProp
property of the returned function (e.g. ifpluralProp
is set to'all'
and the returned function is assigned to the variableget
, the available functions will beget()
andget.all()
). - Optional:
pluralReturn
(boolean): Only applies ifpluralProp
is set. Set totrue
iffn
returns an array of one result per argument. (This will cause the one-element result array to be unwrapped when the singularized function is called.) Set tofalse
iffn
returns a result that does not correspond to the number of arguments. Defaults tofalse
. - Required if
optionProps
is set:optionArg
(positive integer): The zero-based index of the argument into which the preconfigured options ofoptionProps
should be inserted. - Optional:
optionProps
(object or Map): A collection whose keys are the desiredfn
property keys and whose values are the option objects that should be merged into the plain object argument at indexoptionArg
. - Optional:
sbo
(object or false): Options to be passed to thesbo
module (which adds support for the bind operator), orfalse
if you do not want thesbo
module applied.
- Optional:
fn
(function): The main function that should be extended on the basis of the arguments in the first parameter.
A function object with pluralProp
and/or optionProps
properties.
This module is part of the fn
family of modules.