mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
Ensure _.has returns false for nullish objects. [closes #2190]
This commit is contained in:
@@ -5353,9 +5353,9 @@
|
||||
var index = -1,
|
||||
length = path.length;
|
||||
|
||||
while (object != null && ++index < length) {
|
||||
while (++index < length) {
|
||||
var key = path[index];
|
||||
if (!(result = hasFunc(object, key))) {
|
||||
if (!(result = object != null && hasFunc(object, key))) {
|
||||
break;
|
||||
}
|
||||
object = object[key];
|
||||
|
||||
21
test/test.js
21
test/test.js
@@ -7128,13 +7128,17 @@
|
||||
});
|
||||
|
||||
QUnit.test('`_.' + methodName + '` should support deep paths', function(assert) {
|
||||
assert.expect(2);
|
||||
assert.expect(4);
|
||||
|
||||
var object = { 'a': { 'b': { 'c': 3 } } };
|
||||
|
||||
lodashStable.each(['a.b.c', ['a', 'b', 'c']], function(path) {
|
||||
assert.strictEqual(func(object, path), true);
|
||||
});
|
||||
|
||||
lodashStable.each(['a.c.b', ['a', 'c', 'b']], function(path) {
|
||||
assert.strictEqual(func(object, path), false);
|
||||
});
|
||||
});
|
||||
|
||||
QUnit.test('`_.' + methodName + '` should coerce `path` to a string', function(assert) {
|
||||
@@ -7271,6 +7275,21 @@
|
||||
});
|
||||
});
|
||||
|
||||
QUnit.test('`_.' + methodName + '` should return `false` when nested `object` is nullish', function(assert) {
|
||||
assert.expect(2);
|
||||
|
||||
var values = [null, undefined],
|
||||
expected = lodashStable.map(values, alwaysFalse);
|
||||
|
||||
lodashStable.each(['a.b.c', ['a', 'b', 'c']], function(path) {
|
||||
var actual = lodashStable.map(values, function(value) {
|
||||
return func({ 'a': value }, path);
|
||||
});
|
||||
|
||||
assert.deepEqual(actual, expected);
|
||||
});
|
||||
});
|
||||
|
||||
QUnit.test('`_.' + methodName + '` should return `false` with deep paths when `object` is nullish', function(assert) {
|
||||
assert.expect(2);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user