mutablify-args

1.0.1 • Public • Published

Mutablify Arguments

A simple function wrapper to allow your arguments to be modified via functions.

Installation

Yarn

yarn add mutablify-args

NPM

npm i mutablify-args

Usage

The exported function wraps the provided function. The wrapper function will use the first argument as mutators.

Mutators are functions which changes the value of the arguments. Arguments are passed as an array to the mutator function, you must return an array, which will be used as arguments.

Mutator functions are run in sequence.

mutablifyArgs(fn, strict)
  • fn this function will be wrapped
  • strict you can use this argument to disable strict mode (which is enabled by default). In strict mode, the wrapper function will validate the passed mutator functions. If the mutator is not a function, it will throw an error in strict mode. In non-strict mode it continues on.

Mutator function example:

(args) => args.map(arg => arg + 5)

This mutator function will add 5 to all arguments.

Example

const sum = (a, b) => a + b
const mutablifiedSum = mutablifyArgs(sum)

const mutatorAddThreeToA = (args) => [ args[0] + 3, args[1] ]

const a = 5
const b = 5
const originalResult = sum(a, b)
const mutablifiedResult = mutablifiedSum([ mutatorAddThreeToA ], a, b) // 'a' becomes 8

console.log(`Original result: ${originalResult}`) // 10
console.log(`Mutablified result using 'mutatorAddThreeToA': ${mutablifiedResult}`) // 13

Tests

npm test

Package Sidebar

Install

npm i mutablify-args

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

58.6 kB

Total Files

6

Last publish

Collaborators

  • david.szabo97