mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 15:57:48 +00:00
Merge pull request #142 from danheberden/pluckByArray
Add `at` method to get elements from a collection. Former-commit-id: 99e4d651afe760f952398e5119d03c6caff0fc44
This commit is contained in:
1
build.js
1
build.js
@@ -67,6 +67,7 @@
|
||||
var dependencyMap = {
|
||||
'after': [],
|
||||
'assign': ['isArguments'],
|
||||
'at': ['isString'],
|
||||
'bind': ['isFunction', 'isObject'],
|
||||
'bindAll': ['bind', 'functions'],
|
||||
'bindKey': ['isFunction', 'isObject'],
|
||||
|
||||
36
lodash.js
36
lodash.js
@@ -1937,6 +1937,41 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
var index = -1,
|
||||
props = concat.apply(arrayRef, slice(arguments, 1)),
|
||||
length = props.length,
|
||||
result = Array(length);
|
||||
|
||||
if (noCharByIndex && isString(collection)) {
|
||||
collection = collection.split('');
|
||||
}
|
||||
|
||||
while(++index < length) {
|
||||
result[index] = collection[props[index]];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -4222,6 +4257,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;
|
||||
|
||||
24
test/test.js
24
test/test.js
@@ -667,6 +667,30 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('lodash.at');
|
||||
|
||||
(function() {
|
||||
test('should get items in range', function() {
|
||||
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 = _.at({ 'a': 'apple', 'b': 'ball', 'c': 'count' }, ['a', 'c']);
|
||||
deepEqual(result, ['apple', 'count']);
|
||||
});
|
||||
test('no list should return an empty array', function() {
|
||||
deepEqual(_.at(['a', 'b', 'c']), [] );
|
||||
});
|
||||
test('should work on strings', function() {
|
||||
deepEqual(_.at("helio", [0,3]), ['h', 'i']);
|
||||
});
|
||||
test('should work with multple args', function() {
|
||||
deepEqual(_.at(['a','b','c','d'], 0, 2, 3), ['a', 'c', 'd']);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('lodash.groupBy');
|
||||
|
||||
(function() {
|
||||
|
||||
Reference in New Issue
Block a user