Skip to content

Commit 947a326

Browse files
committedNov 28, 2018
Ignore symbols in deepEqual() by default. Closes #281
1 parent 27ac630 commit 947a326

File tree

3 files changed

+38
-32
lines changed

3 files changed

+38
-32
lines changed
 

‎API.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ var config = Hoek.applyToDefaultsWithShallow(defaults, options, [['db', 'server'
162162
### deepEqual(b, a, [options])
163163

164164
Performs a deep comparison of the two values including support for circular dependencies, prototype, and enumerable properties.
165-
To skip prototype comparisons, use `options.prototype = false`
165+
To skip prototype comparisons, use `options.prototype = false` and to include symbols, used `options.symbols = true`.
166166

167167
```javascript
168168
Hoek.deepEqual({ a: [1, 2], b: 'string', c: { d: true } }, { a: [1, 2], b: 'string', c: { d: true } }); //results in true

‎lib/deep-equal.js

100644100755
+20-18
Original file line numberDiff line numberDiff line change
@@ -228,31 +228,33 @@ internals.isDeepEqualObj = function (instanceType, obj, ref, options, seen) {
228228

229229
// Check symbols
230230

231-
const objSymbols = getOwnPropertySymbols(obj);
232-
const refSymbols = new Set(getOwnPropertySymbols(ref));
231+
if (options.symbols) {
232+
const objSymbols = getOwnPropertySymbols(obj);
233+
const refSymbols = new Set(getOwnPropertySymbols(ref));
233234

234-
for (let i = 0; i < objSymbols.length; ++i) {
235-
const key = objSymbols[i];
235+
for (let i = 0; i < objSymbols.length; ++i) {
236+
const key = objSymbols[i];
236237

237-
if (hasOwnEnumerableProperty(obj, key)) {
238-
if (!hasOwnEnumerableProperty(ref, key)) {
239-
return false;
240-
}
238+
if (hasOwnEnumerableProperty(obj, key)) {
239+
if (!hasOwnEnumerableProperty(ref, key)) {
240+
return false;
241+
}
241242

242-
if (!isDeepEqual(obj[key], ref[key], options, seen)) {
243+
if (!isDeepEqual(obj[key], ref[key], options, seen)) {
244+
return false;
245+
}
246+
}
247+
else if (hasOwnEnumerableProperty(ref, key)) {
243248
return false;
244249
}
245-
}
246-
else if (hasOwnEnumerableProperty(ref, key)) {
247-
return false;
248-
}
249250

250-
refSymbols.delete(key);
251-
}
251+
refSymbols.delete(key);
252+
}
252253

253-
for (const key of refSymbols) {
254-
if (hasOwnEnumerableProperty(ref, key)) {
255-
return false;
254+
for (const key of refSymbols) {
255+
if (hasOwnEnumerableProperty(ref, key)) {
256+
return false;
257+
}
256258
}
257259
}
258260

‎test/index.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ describe('cloneWithShallow()', () => {
750750
c: {
751751
d: 6
752752
},
753-
e() {}
753+
e() { }
754754
};
755755

756756
const copy = Hoek.cloneWithShallow(source, ['c', 'e']);
@@ -1135,18 +1135,22 @@ describe('deepEqual()', () => {
11351135
const ne = {};
11361136
Object.defineProperty(ne, sym, { value: true });
11371137

1138-
expect(Hoek.deepEqual({ [sym]: { c: true } }, { [sym]: { c: true } })).to.be.true();
1139-
expect(Hoek.deepEqual({ [sym]: { c: true } }, { [sym]: { c: false } })).to.be.false();
1140-
expect(Hoek.deepEqual({ [sym]: { c: true } }, { [sym]: true })).to.be.false();
1141-
expect(Hoek.deepEqual({ [sym]: undefined }, { [sym]: undefined })).to.be.true();
1142-
expect(Hoek.deepEqual({ [sym]: undefined }, {})).to.be.false();
1143-
expect(Hoek.deepEqual({}, { [sym]: undefined })).to.be.false();
1138+
expect(Hoek.deepEqual({ [sym]: { c: true } }, { [sym]: { c: true } }, { symbols: true })).to.be.true();
1139+
expect(Hoek.deepEqual({ [sym]: { c: true } }, { [sym]: { c: false } }, { symbols: true })).to.be.false();
1140+
expect(Hoek.deepEqual({ [sym]: { c: true } }, { [sym]: true }, { symbols: true })).to.be.false();
1141+
expect(Hoek.deepEqual({ [sym]: undefined }, { [sym]: undefined }, { symbols: true })).to.be.true();
1142+
expect(Hoek.deepEqual({ [sym]: undefined }, {}, { symbols: true })).to.be.false();
1143+
expect(Hoek.deepEqual({}, { [sym]: undefined }, { symbols: true })).to.be.false();
11441144

1145-
expect(Hoek.deepEqual({}, ne)).to.be.true();
1146-
expect(Hoek.deepEqual(ne, {})).to.be.true();
1147-
expect(Hoek.deepEqual({ [sym]: true }, ne)).to.be.false();
1148-
expect(Hoek.deepEqual(ne, { [sym]: true })).to.be.false();
1149-
expect(Hoek.deepEqual(ne, { [Symbol()]: undefined })).to.be.false();
1145+
expect(Hoek.deepEqual({}, ne, { symbols: true })).to.be.true();
1146+
expect(Hoek.deepEqual(ne, {}, { symbols: true })).to.be.true();
1147+
expect(Hoek.deepEqual({ [sym]: true }, ne, { symbols: true })).to.be.false();
1148+
expect(Hoek.deepEqual(ne, { [sym]: true }, { symbols: true })).to.be.false();
1149+
expect(Hoek.deepEqual(ne, { [Symbol()]: undefined }, { symbols: true })).to.be.false();
1150+
1151+
expect(Hoek.deepEqual({ [sym]: true }, { [sym]: true }, { symbols: true })).to.be.true();
1152+
expect(Hoek.deepEqual({ [sym]: true }, {}, { symbols: true })).to.be.false();
1153+
expect(Hoek.deepEqual({ [sym]: true }, {}, { symbols: false })).to.be.true();
11501154
});
11511155

11521156
it('compares dates', () => {
@@ -1367,7 +1371,7 @@ describe('deepEqual()', () => {
13671371

13681372
it('handles valueOf() that throws', () => {
13691373

1370-
const throwing = class {
1374+
const throwing = class {
13711375

13721376
constructor(value) {
13731377

0 commit comments

Comments
 (0)
Please sign in to comment.