mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
Ensure has treats nested sparse arrays consistently.
This commit is contained in:
37
lodash.js
37
lodash.js
@@ -5343,29 +5343,24 @@
|
||||
* @returns {boolean} Returns `true` if `path` exists, else `false`.
|
||||
*/
|
||||
function hasPath(object, path, hasFunc) {
|
||||
if (object == null) {
|
||||
return false;
|
||||
}
|
||||
var result = hasFunc(object, path);
|
||||
if (!result && !isKey(path)) {
|
||||
path = baseCastPath(path);
|
||||
path = isKey(path, object) ? [path] : baseCastPath(path);
|
||||
|
||||
var index = -1,
|
||||
length = path.length;
|
||||
var index = -1,
|
||||
length = path.length;
|
||||
|
||||
while (++index < length) {
|
||||
var key = path[index];
|
||||
if (!(result = object != null && hasFunc(object, key))) {
|
||||
break;
|
||||
}
|
||||
object = object[key];
|
||||
while (++index < length) {
|
||||
var key = path[index];
|
||||
if (!(result = object != null && hasFunc(object, key))) {
|
||||
break;
|
||||
}
|
||||
object = object[key];
|
||||
}
|
||||
var length = object ? object.length : undefined;
|
||||
return result || (
|
||||
!!length && isLength(length) && isIndex(path, length) &&
|
||||
(isArray(object) || isString(object) || isArguments(object))
|
||||
);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
var length = object ? object.length : 0;
|
||||
return !!length && isLength(length) && isIndex(key, length) &&
|
||||
(isArray(object) || isString(object) || isArguments(object));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -12048,7 +12043,7 @@
|
||||
* // => false
|
||||
*/
|
||||
function has(object, path) {
|
||||
return hasPath(object, path, baseHas);
|
||||
return object != null && hasPath(object, path, baseHas);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -12078,7 +12073,7 @@
|
||||
* // => false
|
||||
*/
|
||||
function hasIn(object, path) {
|
||||
return hasPath(object, path, baseHasIn);
|
||||
return object != null && hasPath(object, path, baseHasIn);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
12
test/test.js
12
test/test.js
@@ -7243,7 +7243,7 @@
|
||||
});
|
||||
|
||||
QUnit.test('`_.' + methodName + '` should return `true` for index values within bounds for arrays, `arguments` objects, and strings', function(assert) {
|
||||
assert.expect(1);
|
||||
assert.expect(2);
|
||||
|
||||
var string = Object('abc');
|
||||
delete args[0];
|
||||
@@ -7256,6 +7256,16 @@
|
||||
return func(value, 0);
|
||||
});
|
||||
|
||||
assert.deepEqual(actual, expected);
|
||||
|
||||
expected = lodashStable.map(values, lodashStable.constant([true, true]));
|
||||
|
||||
actual = lodashStable.map(values, function(value) {
|
||||
return lodashStable.map(['a[0]', ['a', '0']], function(path) {
|
||||
return func({ 'a': value }, path);
|
||||
});
|
||||
});
|
||||
|
||||
assert.deepEqual(actual, expected);
|
||||
args[0] = 1;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user