Align _.keys with ES6 and coerce primitives to objects.

This commit is contained in:
John-David Dalton
2014-06-16 12:27:21 -07:00
parent 4f9da35b76
commit 73f6fd49d0
4 changed files with 26 additions and 18 deletions

17
dist/lodash.js vendored
View File

@@ -6799,14 +6799,16 @@
* // => ['x', 'y'] (property order is not guaranteed across environments) * // => ['x', 'y'] (property order is not guaranteed across environments)
*/ */
var keys = !nativeKeys ? shimKeys : function(object) { var keys = !nativeKeys ? shimKeys : function(object) {
var Ctor = object && object.constructor, object = Object(object);
length = object ? object.length : 0;
var Ctor = object.constructor,
length = object.length;
if ((Ctor && object === Ctor.prototype) || if ((Ctor && object === Ctor.prototype) ||
(typeof length == 'number' && length > 0)) { (typeof length == 'number' && length > 0)) {
return shimKeys(object); return shimKeys(object);
} }
return isObject(object) ? nativeKeys(object) : []; return nativeKeys(object);
}; };
/** /**
@@ -6830,9 +6832,8 @@
* // => ['x', 'y', 'z'] (property order is not guaranteed across environments) * // => ['x', 'y', 'z'] (property order is not guaranteed across environments)
*/ */
function keysIn(object) { function keysIn(object) {
if (!isObject(object)) { object = Object(object);
return [];
}
var length = object.length; var length = object.length;
length = (typeof length == 'number' && length > 0 && length = (typeof length == 'number' && length > 0 &&
(isArray(object) || (support.nonEnumArgs && isArguments(object))) && length) >>> 0; (isArray(object) || (support.nonEnumArgs && isArguments(object))) && length) >>> 0;
@@ -8461,10 +8462,8 @@
lodash.map = map; lodash.map = map;
lodash.mapValues = mapValues; lodash.mapValues = mapValues;
lodash.matches = matches; lodash.matches = matches;
lodash.max = max;
lodash.memoize = memoize; lodash.memoize = memoize;
lodash.merge = merge; lodash.merge = merge;
lodash.min = min;
lodash.mixin = mixin; lodash.mixin = mixin;
lodash.negate = negate; lodash.negate = negate;
lodash.omit = omit; lodash.omit = omit;
@@ -8559,6 +8558,8 @@
lodash.isUndefined = isUndefined; lodash.isUndefined = isUndefined;
lodash.kebabCase = kebabCase; lodash.kebabCase = kebabCase;
lodash.lastIndexOf = lastIndexOf; lodash.lastIndexOf = lastIndexOf;
lodash.max = max;
lodash.min = min;
lodash.noConflict = noConflict; lodash.noConflict = noConflict;
lodash.noop = noop; lodash.noop = noop;
lodash.now = now; lodash.now = now;

View File

@@ -6990,15 +6990,17 @@
* // => ['x', 'y'] (property order is not guaranteed across environments) * // => ['x', 'y'] (property order is not guaranteed across environments)
*/ */
var keys = !nativeKeys ? shimKeys : function(object) { var keys = !nativeKeys ? shimKeys : function(object) {
var Ctor = object && object.constructor, object = Object(object);
length = object ? object.length : 0;
var Ctor = object.constructor,
length = object.length;
if ((Ctor && object === Ctor.prototype) || if ((Ctor && object === Ctor.prototype) ||
(typeof length == 'number' && length > 0) || (typeof length == 'number' && length > 0) ||
(support.enumPrototypes && typeof object == 'function')) { (support.enumPrototypes && typeof object == 'function')) {
return shimKeys(object); return shimKeys(object);
} }
return isObject(object) ? nativeKeys(object) : []; return nativeKeys(object);
}; };
/** /**
@@ -7022,9 +7024,8 @@
* // => ['x', 'y', 'z'] (property order is not guaranteed across environments) * // => ['x', 'y', 'z'] (property order is not guaranteed across environments)
*/ */
function keysIn(object) { function keysIn(object) {
if (!isObject(object)) { object = Object(object);
return [];
}
var length = object.length; var length = object.length;
length = (typeof length == 'number' && length > 0 && length = (typeof length == 'number' && length > 0 &&
(isArray(object) || (support.nonEnumStrings && isString(object)) || (isArray(object) || (support.nonEnumStrings && isString(object)) ||

View File

@@ -5721,6 +5721,11 @@
deepEqual(actual.sort(), ['a', 'b']); deepEqual(actual.sort(), ['a', 'b']);
}); });
test('`_.' + methodName + '` should coerce primitives to objects', 1, function() {
var actual = func('abc');
deepEqual(actual.sort(), ['0', '1', '2']);
});
test('`_.' + methodName + '` should treat sparse arrays as dense', 1, function() { test('`_.' + methodName + '` should treat sparse arrays as dense', 1, function() {
var array = [1]; var array = [1];
array[2] = 3; array[2] = 3;
@@ -5729,7 +5734,7 @@
deepEqual(actual.sort(), ['0', '1', '2']); deepEqual(actual.sort(), ['0', '1', '2']);
}); });
test('`_.' + methodName + '` should custom properties on arrays', 1, function() { test('`_.' + methodName + '` should return keys for custom properties on arrays', 1, function() {
var array = [1]; var array = [1];
array.a = 1; array.a = 1;
@@ -5755,7 +5760,7 @@
} }
}); });
test('`_.' + methodName + '` should custom properties on `arguments` objects', 1, function() { test('`_.' + methodName + '` should return keys for custom properties on `arguments` objects', 1, function() {
if (!(isPhantom || isStrict)) { if (!(isPhantom || isStrict)) {
args.a = 1; args.a = 1;
var actual = func(args); var actual = func(args);
@@ -5785,7 +5790,7 @@
deepEqual(actual.sort(), ['0', '1', '2']); deepEqual(actual.sort(), ['0', '1', '2']);
}); });
test('`_.' + methodName + '` should custom properties on string objects', 1, function() { test('`_.' + methodName + '` should return keys for custom properties on string objects', 1, function() {
var object = Object('a'); var object = Object('a');
object.a = 1; object.a = 1;

View File

@@ -110,7 +110,8 @@
'Died on test #63' 'Died on test #63'
], ],
'keys': [ 'keys': [
'is not fooled by sparse arrays; see issue #95' 'is not fooled by sparse arrays; see issue #95',
'[]'
], ],
'omit': [ 'omit': [
'can accept a predicate' 'can accept a predicate'