From 6e8c9842501e1fbcf8423bc5609ea28a3797af87 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 18 Apr 2016 21:33:32 -0700 Subject: [PATCH] Use `toKey` in more functions. --- lodash.js | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/lodash.js b/lodash.js index 7b3cdbe0c..2e61502b0 100644 --- a/lodash.js +++ b/lodash.js @@ -2662,7 +2662,7 @@ length = path.length; while (object != null && index < length) { - object = object[path[index++]]; + object = object[toKey(path[index++])]; } return (index && index == length) ? object : undefined; } @@ -2835,7 +2835,7 @@ object = parent(object, path); path = last(path); } - var func = object == null ? object : object[path]; + var func = object == null ? object : object[toKey(path)]; return func == null ? undefined : apply(func, object, args); } @@ -3095,7 +3095,7 @@ */ function baseMatchesProperty(path, srcValue) { if (isKey(path) && isStrictComparable(srcValue)) { - return matchesStrictComparable(path, srcValue); + return matchesStrictComparable(toKey(path), srcValue); } return function(object) { var objValue = get(object, path); @@ -3309,6 +3309,7 @@ * @returns {Function} Returns the new function. */ function baseProperty(key) { + key = toKey(key); return function(object) { return object == null ? undefined : object[key]; }; @@ -3387,11 +3388,11 @@ object = parent(array, path); if (object != null) { - delete object[last(path)]; + delete object[toKey(last(path))]; } } else { - delete array[index]; + delete array[toKey(index)]; } } } @@ -3481,7 +3482,7 @@ nested = object; while (nested != null && ++index < length) { - var key = path[index]; + var key = toKey(path[index]); if (isObject(nested)) { var newValue = value; if (index != lastIndex) { @@ -3709,7 +3710,7 @@ /** * The base implementation of `_.toString` which doesn't convert nullish - * values to empty strings or preserve the sign of `-0`. + * values to empty strings. * * @private * @param {*} value The value to process. @@ -3723,7 +3724,8 @@ if (isSymbol(value)) { return symbolToString ? symbolToString.call(value) : ''; } - return (value + ''); + var result = (value + ''); + return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result; } /** @@ -3797,8 +3799,9 @@ function baseUnset(object, path) { path = isKey(path, object) ? [path] : castPath(path); object = parent(object, path); - var key = last(path); - return (object != null && has(object, key)) ? delete object[key] : true; + + var key = toKey(last(path)); + return !(object != null && baseHas(object, key)) || delete object[key]; } /** @@ -5463,7 +5466,7 @@ length = path.length; while (++index < length) { - var key = path[index]; + var key = toKey(path[index]); if (!(result = object != null && hasFunc(object, key))) { break; } @@ -5921,7 +5924,11 @@ * @returns {string|symbol} Returns the key. */ function toKey(value) { - return (typeof value == 'string' || isSymbol(value)) ? value : (value + ''); + if (typeof value == 'string' || isSymbol(value)) { + return value; + } + var result = (value + ''); + return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result; } /** @@ -11717,11 +11724,7 @@ * // => '1,2,3' */ function toString(value) { - if (value == null) { - return ''; - } - var result = baseToString(value); - return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result; + return value == null ? '' : baseToString(value); } /*------------------------------------------------------------------------*/ @@ -12730,7 +12733,7 @@ * // => { 'a': 1, 'c': 3 } */ var pick = rest(function(object, props) { - return object == null ? {} : basePick(object, baseFlatten(props, 1)); + return object == null ? {} : basePick(object, arrayMap(baseFlatten(props, 1), toKey)); }); /** @@ -12797,7 +12800,7 @@ length = 1; } while (++index < length) { - var value = object == null ? undefined : object[path[index]]; + var value = object == null ? undefined : object[toKey(path[index])]; if (value === undefined) { index = length; value = defaultValue; @@ -14447,6 +14450,7 @@ */ var bindAll = rest(function(object, methodNames) { arrayEach(baseFlatten(methodNames, 1), function(key) { + key = toKey(key); object[key] = bind(object[key], object); }); return object;