# magic-arguments

> Brings magic to working with arguments

Latest version **1.1.2** (published 2016-07-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install magic-arguments
pnpm add magic-arguments
yarn add magic-arguments
bun add magic-arguments
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2016-07-14 |
| First published | 2016-04-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | KROT47 |
| Maintainers | krot47 |
| Keywords | arguments, javascript |

## Links

- npm: https://www.npmjs.com/package/magic-arguments
- Repository: https://github.com/KROT47/magic-arguments
- Homepage: https://github.com/KROT47/magic-arguments#readme
- Issues: https://github.com/KROT47/magic-arguments/issues
- npm.io page: https://npm.io/package/magic-arguments

## Dependencies (3)

- [extend](https://npm.io/package/extend.md) ^3.0.0
- [instanceof](https://npm.io/package/instanceof.md) ^1.0.2
- [abstract-value](https://npm.io/package/abstract-value.md) ^2.0.1

## Recent versions

- 1.1.2 (latest) — 2016-07-14
- 1.1.1 — 2016-07-13
- 1.0.8 — 2016-04-09
- 1.0.7 — 2016-04-04
- 1.0.5 — 2016-04-03
- 1.0.4 — 2016-04-03
- 1.0.3 — 2016-04-03
- 1.0.2 — 2016-04-03
- 1.0.1 — 2016-04-03
- 1.0.0 — 2016-04-03

## README

# Magic-Arguments

Transforms "arguments" object to { key: value } object where key is argument name.  
Also can:
- transform "arguments" object to array
- return array of function arguments names  

*As for me it's ennoying to use many if-then to define what arguments the function with possibility to be overriden gets.*  
  
**Basic usage:**

```js
const Arguments = require( 'magic-arguments' );

// Arguments( arguments, argsConfig[, handlersContext, unordered] ) => { key: value }
// Arguments( arguments ).toArray() => Array

var someVar = 0;

var argsConfig = {
        a: { type: String, required: true },
        b: { type: [ Object, Array ], setDefault: generateFunc },
        c: { type: Boolean, default: false },
        d: { type: 'function' }
    };

function test( a, b, c ) {
    var args = Arguments( arguments, argsConfig );
    a = args.a; b = args.b; c = args.c;
    // ...
    return args;
}

function testArray( a, b, c ) {
    var argsArray = Arguments( arguments ).toArray();
    return argsArray;
}

// --- test --- 
test( 123 )                     // => Error: 'a' is required 
test( 123, '456', [] )          // => Error: no such combination

test( '123', true )             // => { a: '123', b: { a: 0 }, c: true } 
test( '123', function D() {} )  // => { a: '123', b: { a: 1 }, c: false, d: function D() {} }


// --- testArray ---
testArray( 123, '456' ) // => [ 123, '456' ]

// --- helpers ---
function generateFunc() { return { a: someVar++ } }
```
  


**Advanced usage:**
```js
var context1 = { msg: 'Hello,' },
    context2 = { msg: ' world!' };
    
var argsConfig = {
        str: { type: String, default: '', handlers: [ updateStr, updateStr.bind( context2 ) ] },
    	action: { type: [ Function, String, Array ], default: null }
    };

function test( str, action ) {
    // here we must define context for handlers if they use 'this' and are not binded explicitly
    var args = Arguments( arguments, argsConfig, context1 );
    // ...
    return args;
}

function test2( str, action ) {
    // if we forgot to set context the result could be magical)
    var args = Arguments( arguments, argsConfig );
    // ...
    return args;
}

// --- test ---
test()      // => { str: '', action: null }
test( '' )  // => { str: 'Hello, world!', action: null }

// --- test2 ---
test2()     // => { str: '', action: null }
test2( '' ) // => { str: 'undefined world!', action: null }

// --- handlers ---
function updateStr( value, name, result ) {
	result[ name ] = value + this.msg;
	// same as:
	return value + this.msg;
}
```
  

**Using magic with unordered arguments:**  
*This could be strange so don't surprize. I don't know why somebody may want this)*
```js
var argsConfig = {
        a: { type: String },
    	b: { type: [ Function, String, Array ] },
    	c: { type: Object },
    	d: { type: [ Boolean, String ] }
    };

function test() {
    // true on end means that order of arguments can be magical)
    var args = Arguments( arguments, argsConfig/*, handlersCtx*/, true );
    // ...
    return args;
}

// --- test ---
test( true, { a: 1 }, '123', function () {} )
// => { a: '123', b: function () {}, c: { a: 1 }, d: true }
```

---
_Source: https://npm.io/package/magic-arguments · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
