var utilMethods = module.exports = {
times: times,
repeat: repeat,
tap: tap,
result: result,
identity: identity,
property: property,
compareProperty: compareProperty,
noop: noop
};
Option name | Type | Description |
---|---|---|
self | Integer | |
callback | Function | |
thisArg | Any | |
return | Array |
Calls callback
self
times with thisArg
as context. Callback is passed iteration index from 0 to self-1
function times(callback, thisArg) {
var arr = Array(Math.max(0, this));
for (var i = 0; i < this; i++)
arr[i] = callback.call(thisArg, i);
return arr;
}
Option name | Type | Description |
---|---|---|
self | Any | |
times | Integer | |
return | Array |
Returns array with the first argument repeated times
times
function repeat(times) {
var arr = Array(Math.max(0, times));;
for (var i = 0; i < times; i++)
arr[i] = this;
return arr;
}
Option name | Type | Description |
---|---|---|
self | Any | value that's passed between chained methods |
func | Function | function that will be called with the value (both as context and as the first parameter) |
return | Any |
Function to tap into chained methods and to inspect intermediary result
function tap(func) {
func.call(this, this);
return this;
};
Option name | Type | Description |
---|---|---|
self | Function, Any | |
thisArg | Any | context |
arguments | List | extra arguments |
return | Any |
Calls function self
(first parameter of _.result) with given context and arguments
function result(thisArg) { //, arguments
var args = Array.prototype.slice.call(arguments, 1);
return typeof this == 'function'
? this.apply(thisArg, args)
: this;
}
Option name | Type | Description |
---|---|---|
self | Any | |
return | Any |
Returns self. Useful for using as an iterator if the actual value needs to be returned. Unlike in underscore and lodash, this function is NOT used as default iterator.
function identity() {
return this;
}
Option name | Type | Description |
---|---|---|
self | String | |
return | Function |
Returns function that picks the property from the object
function property() {
var key = this;
return function(obj) {
return obj[key];
};
}
Option name | Type | Description |
---|---|---|
self | String | |
return | Function |
Returns function that can be used in array sort to sort by a given property
function compareProperty() {
var key = this;
return function(a, b) {
return a[key] < b[key]
? -1
: a[key] > b[key]
? 1
: 0;
};
}
Function that does nothing
function noop() {}