https://github.com/f1lt3r/magic-params
🐇 magically pass function parameters in any order
https://github.com/f1lt3r/magic-params
function magic order params pass
Last synced: 4 months ago
JSON representation
🐇 magically pass function parameters in any order
- Host: GitHub
- URL: https://github.com/f1lt3r/magic-params
- Owner: F1LT3R
- Created: 2018-03-14T05:51:47.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-22T14:12:42.000Z (almost 8 years ago)
- Last Synced: 2025-10-20T05:59:09.473Z (4 months ago)
- Topics: function, magic, order, params, pass
- Language: JavaScript
- Homepage: https://f1lt3r.io
- Size: 201 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Magic Params
> 🐇 magically pass function parameters in any order
[](https://travis-ci.org/F1LT3R/magic-params)
[](https://coveralls.io/github/F1LT3R/magic-params?branch=master)
[](https://www.npmjs.com/package/magic-params)
[](https://github.com/sindresorhus/xo)

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...
```javascript
const fn = (args, in, any, order) => {}
```
... will work exactly the same if you re-order the arguments:
```javascript
const fn = (any, order, in, args) => {}
```
Just pass your params object and function to Magic-Params:
```javascript
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
```javascript
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:
```javascript
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:
```javascript
const magicParams = require('magic-params')
const display = function (a, b) {
...
}
magicParams.list(display)
// Returns array: ['a', 'b']
```
## Installation
```shell
yarn add magic-params
```
## Testing
```shell
yarn test
```
## Credits
Thanks to [Ben Iconator](https://thenounproject.com/mr.iconator/) and [Anbileru Adaleru](https://thenounproject.com/pronoun/) from NounProject for the rabbit and magic wand vectors used in the magic params logo.