Skip to content

Commit

Permalink
Ignore symbols in deepEqual() by default. Closes #281
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Nov 28, 2018
1 parent 27ac630 commit 947a326
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ var config = Hoek.applyToDefaultsWithShallow(defaults, options, [['db', 'server'
### deepEqual(b, a, [options])

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

```javascript
Hoek.deepEqual({ a: [1, 2], b: 'string', c: { d: true } }, { a: [1, 2], b: 'string', c: { d: true } }); //results in true
Expand Down
38 changes: 20 additions & 18 deletions lib/deep-equal.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -228,31 +228,33 @@ internals.isDeepEqualObj = function (instanceType, obj, ref, options, seen) {

// Check symbols

const objSymbols = getOwnPropertySymbols(obj);
const refSymbols = new Set(getOwnPropertySymbols(ref));
if (options.symbols) {
const objSymbols = getOwnPropertySymbols(obj);
const refSymbols = new Set(getOwnPropertySymbols(ref));

for (let i = 0; i < objSymbols.length; ++i) {
const key = objSymbols[i];
for (let i = 0; i < objSymbols.length; ++i) {
const key = objSymbols[i];

if (hasOwnEnumerableProperty(obj, key)) {
if (!hasOwnEnumerableProperty(ref, key)) {
return false;
}
if (hasOwnEnumerableProperty(obj, key)) {
if (!hasOwnEnumerableProperty(ref, key)) {
return false;
}

if (!isDeepEqual(obj[key], ref[key], options, seen)) {
if (!isDeepEqual(obj[key], ref[key], options, seen)) {
return false;
}
}
else if (hasOwnEnumerableProperty(ref, key)) {
return false;
}
}
else if (hasOwnEnumerableProperty(ref, key)) {
return false;
}

refSymbols.delete(key);
}
refSymbols.delete(key);
}

for (const key of refSymbols) {
if (hasOwnEnumerableProperty(ref, key)) {
return false;
for (const key of refSymbols) {
if (hasOwnEnumerableProperty(ref, key)) {
return false;
}
}
}

Expand Down
30 changes: 17 additions & 13 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ describe('cloneWithShallow()', () => {
c: {
d: 6
},
e() {}
e() { }
};

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

expect(Hoek.deepEqual({ [sym]: { c: true } }, { [sym]: { c: true } })).to.be.true();
expect(Hoek.deepEqual({ [sym]: { c: true } }, { [sym]: { c: false } })).to.be.false();
expect(Hoek.deepEqual({ [sym]: { c: true } }, { [sym]: true })).to.be.false();
expect(Hoek.deepEqual({ [sym]: undefined }, { [sym]: undefined })).to.be.true();
expect(Hoek.deepEqual({ [sym]: undefined }, {})).to.be.false();
expect(Hoek.deepEqual({}, { [sym]: undefined })).to.be.false();
expect(Hoek.deepEqual({ [sym]: { c: true } }, { [sym]: { c: true } }, { symbols: true })).to.be.true();
expect(Hoek.deepEqual({ [sym]: { c: true } }, { [sym]: { c: false } }, { symbols: true })).to.be.false();
expect(Hoek.deepEqual({ [sym]: { c: true } }, { [sym]: true }, { symbols: true })).to.be.false();
expect(Hoek.deepEqual({ [sym]: undefined }, { [sym]: undefined }, { symbols: true })).to.be.true();
expect(Hoek.deepEqual({ [sym]: undefined }, {}, { symbols: true })).to.be.false();
expect(Hoek.deepEqual({}, { [sym]: undefined }, { symbols: true })).to.be.false();

expect(Hoek.deepEqual({}, ne)).to.be.true();
expect(Hoek.deepEqual(ne, {})).to.be.true();
expect(Hoek.deepEqual({ [sym]: true }, ne)).to.be.false();
expect(Hoek.deepEqual(ne, { [sym]: true })).to.be.false();
expect(Hoek.deepEqual(ne, { [Symbol()]: undefined })).to.be.false();
expect(Hoek.deepEqual({}, ne, { symbols: true })).to.be.true();
expect(Hoek.deepEqual(ne, {}, { symbols: true })).to.be.true();
expect(Hoek.deepEqual({ [sym]: true }, ne, { symbols: true })).to.be.false();
expect(Hoek.deepEqual(ne, { [sym]: true }, { symbols: true })).to.be.false();
expect(Hoek.deepEqual(ne, { [Symbol()]: undefined }, { symbols: true })).to.be.false();

expect(Hoek.deepEqual({ [sym]: true }, { [sym]: true }, { symbols: true })).to.be.true();
expect(Hoek.deepEqual({ [sym]: true }, {}, { symbols: true })).to.be.false();
expect(Hoek.deepEqual({ [sym]: true }, {}, { symbols: false })).to.be.true();
});

it('compares dates', () => {
Expand Down Expand Up @@ -1367,7 +1371,7 @@ describe('deepEqual()', () => {

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

const throwing = class {
const throwing = class {

constructor(value) {

Expand Down

0 comments on commit 947a326

Please sign in to comment.