diff --git a/lodash.js b/lodash.js index 6b8a173b7..aee504712 100644 --- a/lodash.js +++ b/lodash.js @@ -3231,7 +3231,7 @@ return nativeKeys(object); } var result = []; - for (var key in object) { + for (var key in Object(object)) { if (hasOwnProperty.call(object, key) && key != 'constructor') { result.push(key); } @@ -3247,7 +3247,7 @@ * @returns {Array} Returns the array of property names. */ function baseKeysIn(object) { - if (object == null || !('constructor' in Object(object))) { + if (!isObject(object)) { return nativeKeysIn(object); } var isProto = isPrototype(object), @@ -6127,8 +6127,10 @@ */ function nativeKeysIn(object) { var result = []; - for (var key in object) { - result.push(key); + if (object != null) { + for (var key in Object(object)) { + result.push(key); + } } return result; } diff --git a/test/test.js b/test/test.js index 1b74f6611..f513714fa 100644 --- a/test/test.js +++ b/test/test.js @@ -12929,16 +12929,6 @@ delete numberProto.a; }); - QUnit.test('`_.' + methodName + '` should not coerce nullish values to objects', function(assert) { - assert.expect(2); - - objectProto.a = 1; - lodashStable.each([null, undefined], function(value) { - assert.deepEqual(func(value), []); - }); - delete objectProto.a; - }); - QUnit.test('`_.' + methodName + '` skips the `constructor` property on prototype objects', function(assert) { assert.expect(3); @@ -12955,6 +12945,20 @@ Fake.prototype.constructor = Fake; assert.deepEqual(func(Fake.prototype), ['constructor']); }); + + QUnit.test('`_.' + methodName + '` should return an empty array when `object` is nullish', function(assert) { + var values = [, null, undefined], + expected = lodashStable.map(values, stubArray); + + var actual = lodashStable.map(values, function(value, index) { + objectProto.a = 1; + var result = index ? func(value) : func(); + delete objectProto.a; + return result; + }); + + assert.deepEqual(actual, expected); + }); }); /*--------------------------------------------------------------------------*/ @@ -14032,14 +14036,22 @@ }); }); - QUnit.test('should return `false` if parts of `path` are missing', function(assert) { - assert.expect(4); + QUnit.test('should return `false` when `object` is nullish', function(assert) { + assert.expect(2); - var object = {}; + var values = [, null, undefined], + expected = lodashStable.map(values, stubFalse); - lodashStable.each(['a', 'a[1].b.c', ['a'], ['a', '1', 'b', 'c']], function(path) { + lodashStable.each(['constructor', ['constructor']], function(path) { var matches = _.matchesProperty(path, 1); - assert.strictEqual(matches(object), false); + + var actual = lodashStable.map(values, function(value, index) { + try { + return index ? matches(value) : matches(); + } catch (e) {} + }); + + assert.deepEqual(actual, expected); }); }); @@ -14062,6 +14074,17 @@ }); }); + QUnit.test('should return `false` if parts of `path` are missing', function(assert) { + assert.expect(4); + + var object = {}; + + lodashStable.each(['a', 'a[1].b.c', ['a'], ['a', '1', 'b', 'c']], function(path) { + var matches = _.matchesProperty(path, 1); + assert.strictEqual(matches(object), false); + }); + }); + QUnit.test('should match inherited string keyed `srcValue` properties', function(assert) { assert.expect(2); @@ -14300,25 +14323,6 @@ delete numberProto.b; }); - QUnit.test('should return `false` when `object` is nullish', function(assert) { - assert.expect(2); - - var values = [, null, undefined], - expected = lodashStable.map(values, stubFalse); - - lodashStable.each(['constructor', ['constructor']], function(path) { - var matches = _.matchesProperty(path, 1); - - var actual = lodashStable.map(values, function(value, index) { - try { - return index ? matches(value) : matches(); - } catch (e) {} - }); - - assert.deepEqual(actual, expected); - }); - }); - QUnit.test('should return `true` when comparing a `srcValue` of empty arrays and objects', function(assert) { assert.expect(1); @@ -16212,11 +16216,12 @@ QUnit.test('should return an empty object when `object` is nullish', function(assert) { assert.expect(2); - objectProto.a = 1; lodashStable.each([null, undefined], function(value) { - assert.deepEqual(_.omit(value, 'valueOf'), {}); + objectProto.a = 1; + var actual = _.omit(value, 'valueOf'); + delete objectProto.a; + assert.deepEqual(actual, {}); }); - delete objectProto.a; }); QUnit.test('should work with `arguments` objects as secondary arguments', function(assert) { @@ -19818,10 +19823,10 @@ QUnit.test('`_.' + methodName + '` should overwrite primitives in the path', function(assert) { assert.expect(2); - + lodashStable.each(['a.b', ['a', 'b']], function(path) { var object = { 'a': '' }; - + func(object, path, updater); assert.deepEqual(object, { 'a': { 'b': 2 } }); });;