This library helps you to make your methods more flexible by managing arguments list. Just use one of wrappers to define your method. Wrapper prepares arguments list due to described pattern and calls your original method.
If you define method, which takes list of arguments of different types, you can let caller to pass them in accidental order. Use wrapper reorder to define such method. It reorders arguments for you before call original method.
You have to get three arguments of different types: number, string and boolean.
var myMethod = rearg;
Now you can call your method in any way. Original method will be called with arguments list ordered in right way.
;// [5, "aa", false];// [7, "aa", true];// [undefined, "456", undefined]
If you use type "O" (any Object), you can't use types "d" (Date), "r" (RegExp), "a" (Array) or "o" (other Object) in pattern, because they might be also interpreted as "O" (any Object). But you may use them with "o" (other Object) there. Anyway, you caught the error if you try to define method with collisions in pattern. (see Symbols in patterns)
Extend wrapper is used when you want to keep original order of arguments, but you want let caller to miss some of them. Just use question mark after optional params in pattern. This wrapper throws error, if passed arguments couldn't be interpreted due to pattern.
var myMethod = rearg;
In this example third and fourth arguments (s2 and s3) aren't required and caller might miss them. Wrapper will replace them by undefined in that case.
;// [2, "a", "b", "c", function];// [2, "a", "b", undefined, function];// [2, "a", undefined, undefined, function];// throws error as second argument is required by pattern and should be string
You can define optional arguments in sequence with required arguments of the same type only when optional arguments placed in the end of this sequence. It means you can't define pattern like "s?ssn" or "ss?sn". You can define it like "sss?n" and let only last string argument be optional. The same story with type "O" (any Object) in conjunction with any of types "d" (Date), "r" (RegExp), "a" (Array) or "o" (other Object), if you use them in sequence. But it's ok with type "o" (other Object). (see Symbols in patterns)
n - numbers - stringb - booleanf - functionO - any Objecta - Arrayd - Dater - RegExpo - other Object (object which isn't Array, Date or RegExp)
npm install rearg
var rearg = ;