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:
function fn () {
var args = arget(arguments);
args.toArray();
args.pick(Number);
args.match(null, Boolean, String);
}
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({}, () => {});
fn({}, '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);
fn();
.last( )
Returns the last argument
function fn () {
return arget(arguments).last();
}
fn(1, 2, 3, 4);
fn();
.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);
fn();
function fn () {
return arget(arguments).get(1, Number);
}
fn(true, 2, 3, 4);
.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);
fn();
function fn () {
return arget(arguments).get(0, Number);
}
fn(true, 2, 3, 4);
.all( )
.all([constructor = undefined])
Returns elements with the constructor specified
function fn () {
return arget(arguments).all(Number);
}
fn(true, 2, 3, 4);
fn();
.toArray( )
Converts arguments object to array
function fn () {
return arget(arguments).toArray();
}
fn(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);
The iteratee
takes 3 arguments :
iteratee(item, index, array)
item
: The elementindex
: The element index on the argumentsarray
: The arguments as array
function fn () {
arget(arguments).forEach((item, index, array) => {
console.log(item, index, array)
});
}
fn(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);
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
function fn () {
return arget(arguments).map(e => e * 2);
}
fn(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
.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');
function fn () {
return arget(arguments).pick(Number, String)
}
fn(true, 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');
function fn () {
return arget(arguments).omit(Number, String)
}
fn(true, 1, {}, 'str');
.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, () => {});
fn(1, 'value', () => {});
function fn () {
return arget(arguments).match(Array, null, Number, null, Function);
}
fn(1, 2, 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, () => {});
fn(1, 'value', () => {});
function fn () {
return arget(arguments).matchRight(Array, null, Number, null, Function);
}
fn(1, 2, 3, () => {}, [])
/!\ : 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();
fn(1, () => {});
fn(1, 'value', () => {});
License
MIT