From f182675a6e0303da05c10766e5831c12b6125706 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 6 Nov 2014 20:43:34 -0800 Subject: [PATCH] Ensure `baseAt` uses `undefined` for non-index values for array-like collections. --- lodash.js | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/lodash.js b/lodash.js index a0d546b29..1fceb973b 100644 --- a/lodash.js +++ b/lodash.js @@ -473,26 +473,6 @@ return false; } - /** - * The base implementation of `_.at` without support for strings and individual - * key arguments. - * - * @private - * @param {Array|Object} collection The collection to iterate over. - * @param {number[]|string[]} [props] The property names or indexes of elements to pick. - * @returns {Array} Returns the new array of picked elements. - */ - function baseAt(collection, props) { - var index = -1, - length = props.length, - result = Array(length); - - while(++index < length) { - result[index] = collection[props[index]]; - } - return result; - } - /** * The base implementation of `compareAscending` which compares values and * sorts them in ascending order without guaranteeing a stable sort. @@ -1423,6 +1403,34 @@ return object; } + /** + * The base implementation of `_.at` without support for strings and individual + * key arguments. + * + * @private + * @param {Array|Object} collection The collection to iterate over. + * @param {number[]|string[]} [props] The property names or indexes of elements to pick. + * @returns {Array} Returns the new array of picked elements. + */ + function baseAt(collection, props) { + var index = -1, + length = collection ? collection.length : 0, + isArr = isLength(length), + propsLength = props.length, + result = Array(propsLength); + + while(++index < propsLength) { + var key = props[index]; + if (isArr) { + key = parseFloat(key); + result[index] = (key > -1 && key < length && key % 1 == 0) ? collection[key] : undefined; + } else { + result[index] = collection[key]; + } + } + return result; + } + /** * The base implementation of `_.bindAll` without support for individual * method name arguments.