diff --git a/build.js b/build.js index 0f42a66cf..3f699546a 100755 --- a/build.js +++ b/build.js @@ -67,6 +67,7 @@ var dependencyMap = { 'after': [], 'assign': ['isArguments'], + 'at': ['isString'], 'bind': ['isFunction', 'isObject'], 'bindAll': ['bind', 'functions'], 'bindKey': ['isFunction', 'isObject'], diff --git a/lodash.js b/lodash.js index 7e659648c..6449dad27 100644 --- a/lodash.js +++ b/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; diff --git a/test/test.js b/test/test.js index c1b12c470..95375298d 100644 --- a/test/test.js +++ b/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() {