mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 20:07:49 +00:00
Add support for comparing symbol properties to _.isEqual. [closes #2840]
This commit is contained in:
20
lodash.js
20
lodash.js
@@ -3304,16 +3304,16 @@
|
|||||||
function baseIsEqualDeep(object, other, bitmask, customizer, equalFunc, stack) {
|
function baseIsEqualDeep(object, other, bitmask, customizer, equalFunc, stack) {
|
||||||
var objIsArr = isArray(object),
|
var objIsArr = isArray(object),
|
||||||
othIsArr = isArray(other),
|
othIsArr = isArray(other),
|
||||||
objTag = arrayTag,
|
objTag = objIsArr ? arrayTag : getTag(object),
|
||||||
othTag = arrayTag;
|
othTag = othIsArr ? arrayTag : getTag(other);
|
||||||
|
|
||||||
if (!objIsArr) {
|
if (objTag == argsTag) {
|
||||||
objTag = getTag(object);
|
objTag = objectTag;
|
||||||
objTag = objTag == argsTag ? objectTag : objTag;
|
object = copyObject(object, keys(object));
|
||||||
}
|
}
|
||||||
if (!othIsArr) {
|
if (othTag == argsTag) {
|
||||||
othTag = getTag(other);
|
othTag = objectTag;
|
||||||
othTag = othTag == argsTag ? objectTag : othTag;
|
other = copyObject(other, keys(other));
|
||||||
}
|
}
|
||||||
var objIsObj = objTag == objectTag,
|
var objIsObj = objTag == objectTag,
|
||||||
othIsObj = othTag == objectTag,
|
othIsObj = othTag == objectTag,
|
||||||
@@ -5816,9 +5816,9 @@
|
|||||||
*/
|
*/
|
||||||
function equalObjects(object, other, bitmask, customizer, equalFunc, stack) {
|
function equalObjects(object, other, bitmask, customizer, equalFunc, stack) {
|
||||||
var isPartial = bitmask & COMPARE_PARTIAL_FLAG,
|
var isPartial = bitmask & COMPARE_PARTIAL_FLAG,
|
||||||
objProps = keys(object),
|
objProps = getAllKeys(object),
|
||||||
objLength = objProps.length,
|
objLength = objProps.length,
|
||||||
othProps = keys(other),
|
othProps = getAllKeys(other),
|
||||||
othLength = othProps.length;
|
othLength = othProps.length;
|
||||||
|
|
||||||
if (objLength != othLength && !isPartial) {
|
if (objLength != othLength && !isPartial) {
|
||||||
|
|||||||
118
test/test.js
118
test/test.js
@@ -9815,7 +9815,7 @@
|
|||||||
assert.strictEqual(_.isEqual(object1, object2), true);
|
assert.strictEqual(_.isEqual(object1, object2), true);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should treat objects created by `Object.create(null)` like a plain object', function(assert) {
|
QUnit.test('should treat objects created by `Object.create(null)` like plain objects', function(assert) {
|
||||||
assert.expect(2);
|
assert.expect(2);
|
||||||
|
|
||||||
function Foo() {
|
function Foo() {
|
||||||
@@ -9832,22 +9832,6 @@
|
|||||||
assert.strictEqual(_.isEqual(new Foo, object2), false);
|
assert.strictEqual(_.isEqual(new Foo, object2), false);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should return `false` for objects with custom `toString` methods', function(assert) {
|
|
||||||
assert.expect(1);
|
|
||||||
|
|
||||||
var primitive,
|
|
||||||
object = { 'toString': function() { return primitive; } },
|
|
||||||
values = [true, null, 1, 'a', undefined],
|
|
||||||
expected = lodashStable.map(values, stubFalse);
|
|
||||||
|
|
||||||
var actual = lodashStable.map(values, function(value) {
|
|
||||||
primitive = value;
|
|
||||||
return _.isEqual(object, value);
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.deepEqual(actual, expected);
|
|
||||||
});
|
|
||||||
|
|
||||||
QUnit.test('should avoid common type coercions', function(assert) {
|
QUnit.test('should avoid common type coercions', function(assert) {
|
||||||
assert.expect(9);
|
assert.expect(9);
|
||||||
|
|
||||||
@@ -10123,42 +10107,25 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should work as an iteratee for `_.every`', function(assert) {
|
QUnit.test('should compare symbol properties', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(3);
|
||||||
|
|
||||||
var actual = lodashStable.every([1, 1, 1], lodashStable.partial(_.isEqual, 1));
|
if (Symbol) {
|
||||||
assert.ok(actual);
|
var object1 = { 'a': 1 },
|
||||||
});
|
object2 = { 'a': 1 };
|
||||||
|
|
||||||
QUnit.test('should return `true` for like-objects from different documents', function(assert) {
|
object1[symbol] = object2[symbol] = 2;
|
||||||
assert.expect(4);
|
assert.strictEqual(_.isEqual(object1, object2), true);
|
||||||
|
|
||||||
if (realm.object) {
|
object2[symbol] = 3;
|
||||||
assert.strictEqual(_.isEqual([1], realm.array), true);
|
assert.strictEqual(_.isEqual(object1, object2), false);
|
||||||
assert.strictEqual(_.isEqual([2], realm.array), false);
|
|
||||||
assert.strictEqual(_.isEqual({ 'a': 1 }, realm.object), true);
|
delete object2[symbol];
|
||||||
assert.strictEqual(_.isEqual({ 'a': 2 }, realm.object), false);
|
object2[Symbol('a')] = 2;
|
||||||
|
assert.strictEqual(_.isEqual(object1, object2), false);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
skipAssert(assert, 4);
|
skipAssert(assert, 3);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
QUnit.test('should not error on DOM elements', function(assert) {
|
|
||||||
assert.expect(1);
|
|
||||||
|
|
||||||
if (document) {
|
|
||||||
var element1 = document.createElement('div'),
|
|
||||||
element2 = element1.cloneNode(true);
|
|
||||||
|
|
||||||
try {
|
|
||||||
assert.strictEqual(_.isEqual(element1, element2), false);
|
|
||||||
} catch (e) {
|
|
||||||
assert.ok(false, e.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
skipAssert(assert);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -10221,6 +10188,61 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should work as an iteratee for `_.every`', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var actual = lodashStable.every([1, 1, 1], lodashStable.partial(_.isEqual, 1));
|
||||||
|
assert.ok(actual);
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test('should not error on DOM elements', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
if (document) {
|
||||||
|
var element1 = document.createElement('div'),
|
||||||
|
element2 = element1.cloneNode(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert.strictEqual(_.isEqual(element1, element2), false);
|
||||||
|
} catch (e) {
|
||||||
|
assert.ok(false, e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipAssert(assert);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test('should return `true` for like-objects from different documents', function(assert) {
|
||||||
|
assert.expect(4);
|
||||||
|
|
||||||
|
if (realm.object) {
|
||||||
|
assert.strictEqual(_.isEqual([1], realm.array), true);
|
||||||
|
assert.strictEqual(_.isEqual([2], realm.array), false);
|
||||||
|
assert.strictEqual(_.isEqual({ 'a': 1 }, realm.object), true);
|
||||||
|
assert.strictEqual(_.isEqual({ 'a': 2 }, realm.object), false);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipAssert(assert, 4);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test('should return `false` for objects with custom `toString` methods', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var primitive,
|
||||||
|
object = { 'toString': function() { return primitive; } },
|
||||||
|
values = [true, null, 1, 'a', undefined],
|
||||||
|
expected = lodashStable.map(values, stubFalse);
|
||||||
|
|
||||||
|
var actual = lodashStable.map(values, function(value) {
|
||||||
|
primitive = value;
|
||||||
|
return _.isEqual(object, value);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
QUnit.test('should return an unwrapped value when implicitly chaining', function(assert) {
|
QUnit.test('should return an unwrapped value when implicitly chaining', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user