A very lightweight underscore, for browser apps that like to watch their weight.
Note, this is a very incomplete implementation of underscore, partly because many of underscore's functions can be found on Array
and Object
now, and also partly because I'm lazy. Pull requests for missing functions will be accepted gladly.
You can either import all of @eraa-in/utils, or just the parts you need.
import _ from "@eraa-in/utils"
_.groupBy(...);
_.indexBy(...);
var found = _.find(list, predicate);
Looks through each value in the list, returning the first one that passes a truth test (predicate), or undefined if no value passes the test. The function returns as soon as it finds an acceptable element, and doesn't traverse the entire list.
var even = find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
=> 2
var foundIndex = _.findIndex(list, predicate);
Returns the first index where the predicate truth test passes; otherwise returns -1. Predicate is passed the item and item index.
findIndex([4, 6, 8, 12], isPrime);
=> -1 // not found
findIndex([4, 6, 7, 12], isPrime);
=> 2
var sorted = _.sortBy(list, iteratee);
Returns a (stably) sorted copy of list, ranked in ascending order by the results of running each value through iteratee. iteratee may also be the string name of the property to sort by (eg. length).
sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
=> [5, 4, 6, 3, 1, 2]
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
sortBy(stooges, 'name');
=> [{name: 'curly', age: 60}, {name: 'larry', age: 50}, {name: 'moe', age: 40}];
var groups = _.groupBy(list, iteratee);
Splits a collection into sets, grouped by the result of running each value through iteratee. If iteratee is a string instead of a function, groups by the property named by iteratee on each of the values.
groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
=> {1: [1.3], 2: [2.1, 2.4]}
groupBy(['one', 'two', 'three'], 'length');
=> {3: ["one", "two"], 5: ["three"]}
var index = _.indexBy(list, iteratee);
Given a list, and an iteratee function that returns a key for each element in the list (or a property name), returns an object with an index of each item. Just like groupBy, but for when you know your keys are unique.
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
indexBy(stooges, 'age');
=> {
"40": {name: 'moe', age: 40},
"50": {name: 'larry', age: 50},
"60": {name: 'curly', age: 60}
}
_.pick(object, key1, key2, ...);
_.pick(object, [key1, key2, ...]);
_.pick(object, function (value, key, object) { return key == key1; });
Return a copy of the object, filtered to only have values for the whitelisted keys (or array of valid keys). Alternatively accepts a predicate indicating which keys to pick.
_.pick({name: 'moe', age: 50, userid: 'moe1'}, 'name', 'age');
=> {name: 'moe', age: 50}
_.pick({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object) {
return !isNaN(Number(value));
});
=> {age: 50}
_.omit(object, key1, key2, ...);
_.omit(object, [key1, key2, ...]);
_.omit(object, function (value, key, object) { return key == key1; });
Return a copy of the object, filtered to omit the blacklisted keys (or array of keys). Alternatively accepts a predicate indicating which keys to omit.
_.omit({name: 'moe', age: 50, userid: 'moe1'}, 'userid');
=> {name: 'moe', age: 50}
_.omit({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object) {
return !isNaN(Number(value));
});
=> {name: 'moe', userid: 'moe1'}
_.mapObject(object, iteratee);
Like map, but for objects. Transform the value of each property in turn.
_.mapObject({start: 5, end: 12}, function(val, key, object) {
return val + 5;
});
=> {start: 10, end: 17}
_.object(list, [values]);
Converts arrays into objects. Pass either a single list of [key, value]
pairs, or a list of keys, and a list of values. If duplicate keys exist, the last value wins.
_.object(['moe', 'larry', 'curly'], [30, 40, 50]);
=> {moe: 30, larry: 40, curly: 50}
_.object([['moe', 30], ['larry', 40], ['curly', 50]]);
=> {moe: 30, larry: 40, curly: 50}
var r = _.flatten(array, [shallow]);
Flattens a nested array (the nesting can be to any depth). If you pass shallow, the array will only be flattened a single level.
_.flatten([1, [2], [3, [[4]]]]);
=> [1, 2, 3, 4];
_.flatten([1, [2], [3, [[4]]]], true);
=> [1, 2, 3, [[4]]];
var smallestItem = _.max(items, [iteratee]);
Returns the maximum value in list. If an iteratee function is provided, it will be used on each value to generate the criterion by which the value is ranked. -Infinity is returned if list is empty, so an isEmpty guard may be required. Non-numerical values in list will be ignored.
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
_.max(stooges, function(stooge){ return stooge.age; });
=> {name: 'curly', age: 60};
var smallestItem = _.min(items, [iteratee]);
Returns the minimum value in list. If an iteratee function is provided, it will be used on each value to generate the criterion by which the value is ranked. Infinity is returned if list is empty, so an isEmpty guard may be required. Non-numerical values in list will be ignored.
var numbers = [10, 5, 100, 2, 1000];
_.min(numbers);
=> 2
var a = _.compact(array);
Returns a copy of the array with all falsy values removed. In JavaScript, false, null, 0, "", undefined and NaN are all falsy.
_.compact([0, 1, false, 2, '', 3]);
=> [1, 2, 3]
var a = _.without(array, *values);
Returns a copy of the array with all instances of the values removed.
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
=> [2, 3, 4]
var r = _.range([start], stop, [step]);
A function to create flexibly-numbered lists of integers, handy for each and map loops. start, if omitted, defaults to 0; step defaults to 1. Returns a list of integers from start (inclusive) to stop (exclusive), incremented (or decremented) by step, exclusive. Note that ranges that stop before they start are considered to be zero-length instead of negative — if you'd like a negative range, use a negative step.
range(10);
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(1, 11);
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
range(0, 30, 5);
=> [0, 5, 10, 15, 20, 25]
range(0, -10, -1);
=> [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
range(0);
=> []
_.repeat(3, 'stuff')
=> ['stuff', 'stuff', 'stuff']
var destination = _.extend(destination, *sources);
Copy all of the properties in the source objects over to the destination object, and return the destination object. It's in-order, so the last source will override properties of the same name in previous arguments.
_.extend({name: 'moe'}, {age: 50});
=> {name: 'moe', age: 50}
var t = _.times(n, iteratee);
Invokes the given iteratee function n times. Each invocation of iteratee is called with an index argument. Produces an array of the returned values.
_.times(3, function(n){ return "item: " + n; });
=> ['item: 0', 'item: 1', 'item: 2']
var t = _.values(n, iteratee);
Return all of the values of the object's own properties.
_.values({one: 1, two: 2, three: 3});
=> [1, 2, 3]
var t = _.uniq(array, [iteratee]);
Remove duplicate entries from the array. If iteratee is given then the entries uniqueness is based on the result returned by the iteratee. If iteratee is a string instead of a function, uniqueness is based on the property named by iteratee on each of the values.
uniq([1, 2, 2, 3, 1, 2]);
=> [1, 2, 3]
var zipped = _.zip(...arrays);
Merges together the values of each of the arrays with the values at the corresponding position. Useful when you have separate data sources that are coordinated through matching array indexes. Use with apply to pass in an array of arrays. If you're working with a matrix of nested arrays, this can be used to transpose the matrix.
zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
=> [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]