npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

args2

v1.3.0

Published

You can easily handle arguments of your own function.

Downloads

3

Readme

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(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

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'