diff --git a/@vates/compose/README.md b/@vates/compose/README.md index 92a30d0ef..d43b8a3b7 100644 --- a/@vates/compose/README.md +++ b/@vates/compose/README.md @@ -65,6 +65,23 @@ const f = compose( ) ``` +Functions can receive extra parameters: + +```js +const isIn = (value, min, max) => min <= value && value <= max + +// Only compatible when `fns` is passed as an array! +const f = compose([ + [add, 2], + [isIn, 3, 10], +]) + +console.log(f(1)) +// → true +``` + +> Note: if the first function is defined with extra parameters, it will only receive the first value passed to the composed function, instead of all the parameters. + ## Contributions Contributions are _very_ welcomed, either on the documentation or on diff --git a/@vates/compose/USAGE.md b/@vates/compose/USAGE.md index eea802450..ba7caffd4 100644 --- a/@vates/compose/USAGE.md +++ b/@vates/compose/USAGE.md @@ -46,3 +46,20 @@ const f = compose( [add2, mul3] ) ``` + +Functions can receive extra parameters: + +```js +const isIn = (value, min, max) => min <= value && value <= max + +// Only compatible when `fns` is passed as an array! +const f = compose([ + [add, 2], + [isIn, 3, 10], +]) + +console.log(f(1)) +// → true +``` + +> Note: if the first function is defined with extra parameters, it will only receive the first value passed to the composed function, instead of all the parameters. diff --git a/@vates/compose/index.js b/@vates/compose/index.js index 14dfe2b01..e09f04bdc 100644 --- a/@vates/compose/index.js +++ b/@vates/compose/index.js @@ -22,6 +22,24 @@ exports.compose = function compose(opts, fns) { if (n === 0) { throw new TypeError('at least one function must be passed') } + + for (let i = 0; i < n; ++i) { + const entry = fns[i] + if (Array.isArray(entry)) { + const fn = entry[0] + const args = entry.slice() + args[0] = undefined + fns[i] = function composeWithArgs(value) { + args[0] = value + try { + return fn.apply(this, args) + } finally { + args[0] = undefined + } + } + } + } + if (n === 1) { return fns[0] } diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 1901540c4..8c2c64cfa 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -34,6 +34,7 @@ > > In case of conflict, the highest (lowest in previous list) `$version` wins. +- @vates/compose minor - xo-vmdk-to-vhd patch - vhd-lib patch - @xen-orchestra/backups patch