You can easily handle arguments of your own function.
npm install args2
This Document is written in coffeeScript, but it also works with javascript.
sampleFunction = (uri)->
if typeof uri is 'undefined'
throw new Error('undefined is not a valid uri or options object.')
sampleFunction = ->
args = new args2(arguments)
uri = args.str(true,'undefined is not a valid uri or options object.')
sampleFunction = ->
args = Array::slice.call(arguments, 0)
callback = args.pop()
if typeof callback isnt 'function'
args.push callback
options = if args.length then args.shift() else {}
options = if typeof callback is 'function' then options else callback
options = if options == null then {} else options
if typeof callback is 'function'
callback(null,options)
return
else
return options
sampleFunction = ->
args = new args2(arguments)
callback = args.func()
options = args.obj(false,{})
if callback
callback(null,options)
return
else
return options
args = new args2(arguments)
Always use "arguments" as argument
fn = ->
args = new args2(arguments)
text1 = args.str(true)
text2 = args.str(false,'default_value ')
text3 = args.str()
return text1 + text2 + text3
console.log fn('hello ','world ','and you')
# result: hello world and you
console.log fn('hello ','world ')
# result: hello world undefined
console.log fn('hello ')
# result: hello default_value undefined
console.log fn()
# throw error: String argument required
When required is true The second argument becomes an error message
sampleFunction = ->
args = new args2(arguments)
text1 = args.str(true,'custom message')
sampleFunction()
# throw error: custom message
There are various similar methods
There are various alias too
Method with "r" at the head gets the last argument
fn = ->
args = new args2(arguments)
text2 = args.rStr(true)
text1 = args.str()
return text1 + text2
console.log fn('hello ','world ','and you')
# result: 'hello and you'
console.log fn('hello ','world ')
# result: 'hello world '
console.log fn('hello ')
# result: 'undefinedhello '
console.log fn()
# throw error: String argument required
There are various similar methods
There are various alias too
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.
fn = ->
args = new args2(arguments)
text = args.shift()
return text
console.log fn('one','two','three') # one
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.
fn = ->
args = new args2(arguments)
text = args.pop()
return text
console.log fn('one','two','three') # three
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
{bridge} = require 'args2'
# or
args2.bridge
sum = ->
args = new args2(arguments)
return args.nums.reduce (p,c)-> p + c
console.log sum(1,2,3) #6
bridgeFunction1 = ->
args2.bridge(sum,arguments)
console.log bridgeFunction1(2,3,4)# 9
bridgeFunction2 = ->
args2.bridge(sum,arguments,1)
console.log bridgeFunction2(2,3,4)# 10
bridgeFunction3 = ->
args2.bridge(sum,arguments,10,100,1000)
console.log bridgeFunction3(2,3,4)# 1119
'pass' is alias for 'bridge'
This is a class method
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
fn = ->
args = new args2(arguments)
args.shift()
args.bridge(console.log)
fn('one','two','three') # two three
fn = ->
args = new args2(arguments)
args.pop()
args.bridge(console.log)
fn('one','two','three') # one two
fn = ->
args = new args2(arguments)
args.str()
args.bridge(console.log)
fn({a:'a'},'one',true) # {a:'a'} true
New in version 1.1.
'pass' is alias for 'bridge'