๐ magically pass function parameters in any order
Magic-Params is a small Node modules that lets you re-order of the params in your functions, without chaging their value. Magic-Params was designed to support a simplified plugin architecure.
For example: the following function...
const fn = (args, in, any, order) => {}
... will work exactly the same if you re-order the arguments:
const fn = (any, order, in, args) => {}
Just pass your params object and function to Magic-Params:
const magicParams = require('magic-params')
const fn = (a, b) => {
return a + b
}
const params = {
a: 2,
b: 2
}
const result = magicParms.pass(params, fn)
// Result = 4
const magicParams = require('magic-params')
const params = {
a: 'Hello,',
b: ' world!'
}
// You can switch the order of the arguments
const display1 = (a, b) {
console.log(a + b)
}
const display2 = (b, a) {
console.log(a + b)
}
// The values stay mapped to param names
magicParams.pass(params, display1)
// 'Hello, world!'
magicParams.pass(params, display2)
// 'Hello, world!'
You can pass context with magic params by using the magicParams.apply()
method:
const magicParams = require('magic-params')
const params = {
a: 'Hello,'
}
const context = {
b: ' world!'
}
// The context is available on `this`
const display = function (a) {
console.log(a + this.b)
}
magicParams.apply(params, display, context)
// 'Hello, world!'
You can also list params with the magicParams.list()
method:
const magicParams = require('magic-params')
const display = function (a, b) {
...
}
magicParams.list(display)
// Returns array: ['a', 'b']
yarn add magic-params
yarn test
Thanks to Ben Iconator and Anbileru Adaleru from NounProject for the rabbit and magic wand vectors used in the magic params logo.