A JavaScript utility library to manipulate Function.arguments
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:
{ var args = ; args; // => [1, 'str', 2, true] args; // => [1, 2] args; // => [1, true, 'str'] } ;
Second example :
{ var foo bar foobar = ; console; } ; // ==> {}, undefined, () => {} ; // ==> {}, '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 = ; { var wrapper = ; }
/!\ 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
{ return ; } ; // ==> 1 ; // ==> undefined
.last( )
Returns the last argument
{ return ; } ; // ==> 4 ; // ==> undefined
.get( )
.get(position, [constructor = undefined])
Returns element with the constructor and the position specified
{ return ; } ; // ==> 2 ; // ==> undefined { return ; } ; // ==> 3
.getRight( )
.getRight(position, [constructor = undefined])
Returns element with the constructor and the position from the right specified
{ return ; } ; // ==> 3 ; // ==> undefined { return ; } ; // ==> 4
.all( )
.all([constructor = undefined])
Returns elements with the constructor specified
{ return allNumber; } ; // ==> [2, 3, 4] ; // ==> []
.toArray( )
Converts arguments object to array
{ return ; } ; // ==> [1, 2, 3, 4] ; // ==> []
.forEach( )
.forEach(iteratee)
Iterates over the arguments
{ ; } ; // ==> prints 1 2 3 4
The iteratee
takes 3 arguments :
iteratee(item, index, array)
item
: The elementindex
: The element index on the argumentsarray
: The arguments as array
{ ; } ; // 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.
{ return ; } ; // ==> 1 2 4
The predicate
takes 3 arguments :
predicate(item, index, array)
item
: The elementindex
: The element index on the argumentsarray
: The arguments as array
.map( )
.map(predicate)
Returns an array containing the result of the predicate for each element
{ return ; } ; // ==> 2 4 6 8
The iteratee
takes 3 arguments :
iteratee(item, index, array)
item
: The elementindex
: The element index on the argumentsarray
: The arguments as array
.pick( )
.pick(contructor[, constructor[, ...]])
Returns an array of elements with the constructors specified
{ return } ; // ==> [1, 2] { return } ; // ==> [1, 'str']
.omit( )
.omit(contructor[, constructor[, ...]])
Returns an array of elements without those with the constructors specified
{ return } ; // ==> [true, 'str'] { return } ; // ==> [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.
{ return ; } ; // ==> [1, undefined, () => {}] ; // ==> [1, 'value', () => {}] { return ; } // ==> [ [], 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.
{ return ; } ; // ==> [undefined, 1, () => {}] ; // ==> [1, 'value', () => {}] { return ; } // ==> [ [], 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
{ return length } ; // ==> 0 ; // ==> 2 ; // ==> 3
License
MIT