args2

1.3.0 • Public • Published

args2

npm version Build Status Maintainability

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

sampleFunction = (uri)->
  if typeof uri is 'undefined'
    throw new Error('undefined is not a valid uri or options object.')

after

sampleFunction = ->
  args = new args2(arguments)
  uri = args.str(true,'undefined is not a valid uri or options object.')

case B

before

sampleFunction = ->
  args = Array::slice.call(arguments0)
  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

after

sampleFunction = ->
  args = new args2(arguments)
  callback = args.func()
  options = args.obj(false,{})
  if callback
    callback(null,options)
    return
  else
    return options

Document

constructor

args = new args2(arguments)

Always use "arguments" as argument


str(required,default_value)

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

  • ### 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

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

  • ### 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.

fn = ->
  args = new args2(arguments)
  text = args.shift()
  return text
console.log 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.

fn = ->
  args = new args2(arguments)
  text = args.pop()
  return text
console.log 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

{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 

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

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 

pass(fn)

New in version 1.1.

'pass' is alias for 'bridge'

Readme

Keywords

Package Sidebar

Install

npm i args2

Weekly Downloads

2

Version

1.3.0

License

MIT

Unpacked Size

16.1 kB

Total Files

7

Last publish

Collaborators

  • whats