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:
const 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' ; { var args = ; a = argsa; b = argsb; c = argsc; // ... return args;} { var argsArray = ; return argsArray;} // --- test --- // => Error: 'a' is required // => Error: no such combination // => { a: '123', b: { a: 0 }, c: true } // => { a: '123', b: { a: 1 }, c: false, d: function D() {} } // --- testArray --- // => [ 123, '456' ] // --- helpers --- { return a: someVar++ }
Advanced usage:
var context1 = msg: 'Hello,' context2 = msg: ' world!' ; var argsConfig = str: type: String default: '' handlers: updateStr updateStr action: type: Function String Array default: null ; { // here we must define context for handlers if they use 'this' and are not binded explicitly var args = ; // ... return args;} { // if we forgot to set context the result could be magical) var args = ; // ... return args;} // --- test --- // => { str: '', action: null } // => { str: 'Hello, world!', action: null } // --- test2 --- // => { str: '', action: null } // => { str: 'undefined world!', action: null } // --- handlers --- { result name = value + thismsg; // same as: return value + thismsg;}
Using magic with unordered arguments:
This could be strange so don't surprize. I don't know why somebody may want this)
var argsConfig = a: type: String b: type: Function String Array c: type: Object d: type: Boolean String ; { // true on end means that order of arguments can be magical) var args = ; // ... return args;} // --- test ---// => { a: '123', b: function () {}, c: { a: 1 }, d: true }