mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 07:47:49 +00:00
change .grab to .at, add unlimited args or numbers or arrays and simplify function call to use values and pick
Former-commit-id: 3deb82ad9f55cd7261453a40bb0f046a5340790d
This commit is contained in:
44
lodash.js
44
lodash.js
@@ -1937,6 +1937,29 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* Retrieves the elements in the `collection` at specified indexes. Indexes may
|
||||
* be specified as individual arguments or as arrays of indexes.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collections
|
||||
* @param {Array|Object|String} collection The collection to iterate over.
|
||||
* @param {Number|Array} number|[index1, index2, ...] The index(es) of `collection`
|
||||
* to retrieve, either as individual arguments or arrays.
|
||||
* @returns {Array} Returns a new array of elements that matched the provided indexes.
|
||||
* @example
|
||||
*
|
||||
* _.at( ['a', 'b', 'c', 'd', 'e', 'f'], [0, 2, 5] );
|
||||
* // => ['a', 'c', 'f']
|
||||
*
|
||||
* _.at( ['lodash', 'javascript', 'fast'], 0, 2 );
|
||||
* // => ['lodash, 'fast']
|
||||
*/
|
||||
function at(collection, list) {
|
||||
return values(pick(collection || [], concat.apply(arguments, slice(arguments, 1))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given `target` element is present in a `collection` using strict
|
||||
* equality for comparisons, i.e. `===`. If `fromIndex` is negative, it is used
|
||||
@@ -2175,25 +2198,6 @@
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Grabs the elements in the `collection` using the specified indexes
|
||||
* in the `list` array.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collections
|
||||
* @param {Array|Object|String} collection The collection to iterate over.
|
||||
* @param {Array} list The array of indexes to grab.
|
||||
* @ returns {Array} Returns a new array of elements that matched the list array.
|
||||
* @example
|
||||
*
|
||||
* _.grab( ['a', 'b', 'c', 'd', 'e', 'f'], [0, 2, 5] );
|
||||
* // => ['a', 'c', 'f']
|
||||
*/
|
||||
function grab(collection, list) {
|
||||
return invoke(list, function(a){ return a[this]; }, collection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an object composed of keys returned from running each element of
|
||||
* `collection` through a `callback`. The corresponding value of each key is an
|
||||
@@ -4241,6 +4245,7 @@
|
||||
// add functions that return wrapped values when chaining
|
||||
lodash.after = after;
|
||||
lodash.assign = assign;
|
||||
lodash.at = at;
|
||||
lodash.bind = bind;
|
||||
lodash.bindAll = bindAll;
|
||||
lodash.bindKey = bindKey;
|
||||
@@ -4258,7 +4263,6 @@
|
||||
lodash.forIn = forIn;
|
||||
lodash.forOwn = forOwn;
|
||||
lodash.functions = functions;
|
||||
lodash.grab = grab;
|
||||
lodash.groupBy = groupBy;
|
||||
lodash.initial = initial;
|
||||
lodash.intersection = intersection;
|
||||
|
||||
12
test/test.js
12
test/test.js
@@ -667,23 +667,19 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('lodash.grab');
|
||||
QUnit.module('lodash.at');
|
||||
|
||||
(function() {
|
||||
test('should get items in range', function() {
|
||||
var result = _.grab(['a', 'b', 1.4, 'c', { 'foo': 'bar' }, 'd'], [0, 2, 4]);
|
||||
var result = _.at(['a', 'b', 1.4, 'c', { 'foo': 'bar' }, 'd'], [0, 2, 4]);
|
||||
deepEqual( result, ['a', 1.4, { 'foo': 'bar' } ]);
|
||||
});
|
||||
test('should work with an object for `collection`', function() {
|
||||
var result = _.grab({ 'a': 'apple', 'b': 'ball', 'c': 'count' }, ['a', 'c']);
|
||||
var result = _.at({ 'a': 'apple', 'b': 'ball', 'c': 'count' }, ['a', 'c']);
|
||||
deepEqual(result, ['apple', 'count']);
|
||||
});
|
||||
test('no list should return an empty array', function() {
|
||||
deepEqual(_.grab(['a', 'b', 'c']), [] );
|
||||
});
|
||||
test('out of range selections should return undefined', function() {
|
||||
var result = _.grab(['a', 'b', 'c'], [1, 9001]);
|
||||
deepEqual( result, ['b', undefined]);
|
||||
deepEqual(_.at(['a', 'b', 'c']), [] );
|
||||
});
|
||||
}());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user