args2
You can easily handle arguments of your own function.
install
npm install args2
use case
This Document is written in coffeeScript, but it also works with javascript.
case A
before
= if typeof uri is 'undefined' throw 'undefined is not a valid uri or options object.'
after
= -> args = arguments uri = argsstrtrue'undefined is not a valid uri or options object.'
case B
before
= -> args = Array::slicecallarguments0 callback = argspop if typeof callback isnt 'function' argspush callback options = if argslength then argsshift else options = if typeof callback is 'function' then options else callback options = if options == null then else options if typeof callback is 'function' callbacknulloptions return else return options
after
= -> args = arguments callback = argsfunc options = argsobjfalse if callback callbacknulloptions return else return options
Document
constructor
args = arguments
Always use "arguments" as argument
str(required,default_value)
= -> args = arguments text1 = argsstrtrue text2 = argsstrfalse'default_value ' text3 = argsstr return text1 + text2 + text3 consolelog fn'hello ''world ''and you'# result: hello world and you consolelog fn'hello ''world '# result: hello world undefined consolelog fn'hello '# result: hello default_value undefined consolelog fn# throw error: String argument required
When required is true The second argument becomes an error message
= -> args = arguments text1 = argsstrtrue'custom message'sampleFunction# throw error: custom message
There are various similar methods
- ### num(required,default_value)
- ### obj(required,default_value)
- ### array(required,default_value)
- ### bool(required,default_value)
- ### func(required,default_value)
- ### other(required,default_value)
There are various alias too
- ### string(required,default_value)
- ### number(required,default_value)
- ### object(required,default_value)
- ### arr(required,default_value)
- ### boolean(required,default_value)
- ### function(required,default_value)
rStr(required,default_value)
Method with "r" at the head gets the last argument
= -> args = arguments text2 = argsrStrtrue text1 = argsstr return text1 + text2consolelog fn'hello ''world ''and you'# result: 'hello and you' consolelog fn'hello ''world '# result: 'hello world ' consolelog fn'hello '# result: 'undefinedhello ' consolelog fn# throw error: String argument required
There are various similar methods
- ### rNum(required,default_value)
- ### rObj(required,default_value)
- ### rAarray(required,default_value)
- ### rBool(required,default_value)
- ### rFunc(required,default_value)
- ### rOther(required,default_value)
There are various alias too
- ### rNumber(required,default_value)
- ### rObject(required,default_value)
- ### rArr(required,default_value)
- ### rBoolean(required,default_value)
- ### rFunction(required,default_value)
- ### rCallback(required,default_value)
shift
New in version 1.1.
It is a method similar to Array.prototype.shift ()
Removes the first element from the array and returns that element.
= -> args = arguments text = argsshift return textconsolelog fn'one''two''three' # one
pop
New in version 1.1.
It is a method similar to Array.prototype.pop ()
Removes the last element from the array and returns that element.
= -> args = arguments text = argspop return textconsolelog fn'one''two''three' # three
args2.bridge(fn, arguments [,argument_to_add...])
When you are creating classes and functions
I want to pass the argument as it is to another function
In that case, please use "bridge"
This is a class method
require 'args2'# or args2bridge
= -> args = arguments return argsnumsreduce p + cconsolelog sum123 #6 = -> args2bridgesumargumentsconsolelog bridgeFunction1234# 9 = -> args2bridgesumarguments1consolelog bridgeFunction2234# 10 = -> args2bridgesumarguments101001000consolelog bridgeFunction3234# 1119
args2.pass(fn, arguments [,argument_to_add...])
'pass' is alias for 'bridge'
This is a class method
bridge(fn)
New in version 1.1.
This is the instance version of args2.bridge. After manipulating the arguments you can pass it to the next method
= -> args = arguments argsshift argsbridgeconsolelog fn'one''two''three' # two three
= -> args = arguments argspop argsbridgeconsolelog fn'one''two''three' # one two
= -> args = arguments argsstr argsbridgeconsolelog fna:'a''one'true # {a:'a'} true
pass(fn)
New in version 1.1.
'pass' is alias for 'bridge'