1.1.2 • Published 6 years ago

magic-params v1.1.2

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

Magic Params

🐇 magically pass function parameters in any order

Build Status Coverage Status Npm Version XO code style

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

Passing Arguments

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!'

Passing Context

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!'

Listing Params

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']

Installation

yarn add magic-params

Testing

yarn test

Credits

Thanks to Ben Iconator and Anbileru Adaleru from NounProject for the rabbit and magic wand vectors used in the magic params logo.