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

arget

v1.0.0

Published

manipulate arguments properly

Downloads

3

Readme

Arget

A JavaScript utility library to manipulate Function.arguments

Arget

A nice library to deal with most of arguments annoying usecases. A first Error object, a second optional argument or a last function as a callback... Arget helps solve all these issues in one line of code. Ex:

  function fn () {
    var args = arget(arguments);
    
    args.toArray(); // => [1, 'str', 2, true]
    args.pick(Number); // => [1, 2]
    args.match(null, Boolean, String); // => [1, true, 'str']
  }

  fn(1, 'str', 2, true);

Second example :

  function fn () {
    var [foo, bar, foobar] = arget(arguments).match(null, null, Function);
    console.log(foo, bar, foobar);
  }

  fn({}, () => {}); // ==> {}, undefined, () => {}
  fn({}, 'value', () => {}); // ==> {}, 'value', () => {}

Content

Install

npm install --save arget

Usage

When requiring the arget module, you'll get a function that instanciates the Arget wrapper using the arguments you are passing.

  var arget = require('arget');

  function fn () {
    var wrapper = arget(arguments);
  }

/!\ arget instanciates the wrapper whether you pass arguments as argument to arget or not, but you need to keep in mind that there is a huge performance issue if you don't.

Arget wrapper

.first( )

Returns the first argument

  function fn () {
    return arget(arguments).first();
  }

  fn(1, 2, 3, 4); // ==> 1
  fn(); // ==> undefined

.last( )

Returns the last argument

  function fn () {
    return arget(arguments).last();
  }

  fn(1, 2, 3, 4); // ==> 4
  fn(); // ==> undefined

.get( )

.get(position, [constructor = undefined])

Returns element with the constructor and the position specified

  function fn () {
    return arget(arguments).get(1);
  }

  fn(true, 2, 3, 4); // ==> 2
  fn(); // ==> undefined



  function fn () {
    return arget(arguments).get(1, Number);
  }

  fn(true, 2, 3, 4); // ==> 3

.getRight( )

.getRight(position, [constructor = undefined])

Returns element with the constructor and the position from the right specified

  function fn () {
    return arget(arguments).get(1);
  }

  fn(true, 2, 3, 4); // ==> 3
  fn(); // ==> undefined



  function fn () {
    return arget(arguments).get(0, Number);
  }

  fn(true, 2, 3, 4); // ==> 4

.all( )

.all([constructor = undefined])

Returns elements with the constructor specified

  function fn () {
    return arget(arguments).all(Number);
  }

  fn(true, 2, 3, 4); // ==> [2, 3, 4]
  fn(); // ==> []

.toArray( )

Converts arguments object to array

  function fn () {
    return arget(arguments).toArray();
  }

  fn(1, 2, 3, 4); // ==> [1, 2, 3, 4]
  fn(); // ==> []

.forEach( )

.forEach(iteratee)

Iterates over the arguments

  function fn () {
    arget(arguments).forEach(e => console.log(e));
  }

  fn(1, 2, 3, 4); // ==> prints 1 2 3 4

The iteratee takes 3 arguments :

iteratee(item, index, array)

  • item : The element
  • index : The element index on the arguments
  • array : The arguments as array
  function fn () {
    arget(arguments).forEach((item, index, array) => {
      console.log(item, index, array)
    });
  }

  fn(1, 2, 3);

  // prints :
  // 1 0 [1, 2, 3]
  // 2 1 [1, 2, 3]
  // 3 2 [1, 2, 3]

.each( )

Alias of .forEach( )

.filter( )

.filter(predicate)

Returns an array filtred depending on the returned value of the predicate for each item. The result contains items that the predicate returned a truthy value for.

  function fn () {
    return arget(arguments).filter(e => e != 3);
  }

  fn(1, 2, 3, 4); // ==> 1 2 4

The predicate takes 3 arguments :

predicate(item, index, array)

  • item : The element
  • index : The element index on the arguments
  • array : The arguments as array

.map( )

.map(predicate)

Returns an array containing the result of the predicate for each element

  function fn () {
    return arget(arguments).map(e => e * 2);
  }

  fn(1, 2, 3, 4); // ==> 2 4 6 8

The iteratee takes 3 arguments :

iteratee(item, index, array)

  • item : The element
  • index : The element index on the arguments
  • array : The arguments as array

.pick( )

.pick(contructor[, constructor[, ...]])

Returns an array of elements with the constructors specified

  function fn () {
    return arget(arguments).pick(Number)
  }

  fn(true, 1, 2, 'str'); // ==> [1, 2]


  function fn () {
    return arget(arguments).pick(Number, String)
  }

  fn(true, 1, {}, 'str'); // ==> [1, 'str']

.omit( )

.omit(contructor[, constructor[, ...]])

Returns an array of elements without those with the constructors specified

  function fn () {
    return arget(arguments).omit(Number)
  }

  fn(true, 1, 2, 'str'); // ==> [true, 'str']


  function fn () {
    return arget(arguments).omit(Number, String)
  }

  fn(true, 1, {}, 'str'); // ==> [true, {}]

.match( )

.match(contructor[, constructor[, ...]])

Returns an array of elements depending in the pattern of constructors specified. When a falsy value is given instead of a constructor, the position is filled with an elements not matched yet.

  function fn () {
    return arget(arguments).match(null, null, Function);
  }

  fn(1, () => {}); // ==> [1, undefined, () => {}]
  fn(1, 'value', () => {}); // ==> [1, 'value', () => {}]



  function fn () {
    return arget(arguments).match(Array, null, Number, null, Function);
  }

  fn(1, 2, 3, () => {}, []) // ==> [ [], 2, 1, 3, () => {} ]

/!\ : Note that the match method starts first by putting the items with the constructors specified on their position, then it fills the falsy positions with the rest.

.matchRight( )

.matchRight(contructor[, constructor[, ...]])

Similar to .match( ) but it loops from right to left.

Returns an array of elements depending in the pattern of constructors specified. When a falsy value is given instead of a constructor, the position is filled with an elements not matched yet.

  function fn () {
    return arget(arguments).matchRight(null, null, Function);
  }

  fn(1, () => {}); // ==> [undefined, 1, () => {}]
  fn(1, 'value', () => {}); // ==> [1, 'value', () => {}]



  function fn () {
    return arget(arguments).matchRight(Array, null, Number, null, Function);
  }

  fn(1, 2, 3, () => {}, []) // ==> [ [], 1, 3, 2, () => {} ]

/!\ : Note that the matchRight method starts first by putting the items with the constructors specified on their position, then it fills the falsy positions with the rest.

.length

Returns the number of elements

  function fn () {
    return arget(arguments).length
  }

  fn(); // ==> 0
  fn(1, () => {}); // ==> 2
  fn(1, 'value', () => {}); // ==> 3

License

MIT