From 4a0897c73438534668cccb799e6a3c1da51cc06e Mon Sep 17 00:00:00 2001 From: Dan Heberden Date: Tue, 18 Dec 2012 15:09:57 -0800 Subject: [PATCH] build in functionality to at, add string support, optimize, and add more tests Former-commit-id: 951ef27e55fff5a70d09916b55b85f9e725f751a --- lodash.js | 16 ++++++++++++++-- test/test.js | 6 ++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lodash.js b/lodash.js index 26d737388..6449dad27 100644 --- a/lodash.js +++ b/lodash.js @@ -1956,8 +1956,20 @@ * _.at( ['lodash', 'javascript', 'fast'], 0, 2 ); * // => ['lodash, 'fast'] */ - function at(collection, list) { - return values(pick(collection || [], concat.apply(arguments, slice(arguments, 1)))); + 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; } /** diff --git a/test/test.js b/test/test.js index 4bb64da7d..95375298d 100644 --- a/test/test.js +++ b/test/test.js @@ -681,6 +681,12 @@ 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']); + }); }()); /*--------------------------------------------------------------------------*/