diff --git a/src/deepEqual.js b/src/deepEqual.js index 220be28..61214c0 100644 --- a/src/deepEqual.js +++ b/src/deepEqual.js @@ -1,38 +1,42 @@ -import get from './get.js'; -import isEqual from './isEqual.js'; -import traverse from './traverse.js'; - -/** - * Deeply compares two items. - * - * @example - * ``` javascript - * import { deepEqual } from 'object-agent'; - * - * deepEqual(null, undefined); - * // => false - * - * const item1 = { - * a: ['b'] - * } - * const item2 = { - * a: ['c'] - * } - * - * deepEqual(item1, item2); - * // => false - * ``` - * - * @function deepEqual - * @category Comparison - * - * @arg {*} item1 - * @arg {*} item2 - * - * @returns {Boolean} - */ -export default (item1, item2) => { - return Object.is(item1, item2) || !traverse(item1, (path, value, isCircular) => { - return isCircular && Object.is(value, get(item2, path)) || !isEqual(value, get(item2, path)); - }); -}; +import get from './get.js'; +import isEqual from './isEqual.js'; +import traverse from './traverse.js'; + +/** + * Deeply compares two items. + * + * @example + * ``` javascript + * import { deepEqual } from 'object-agent'; + * + * deepEqual(null, undefined); + * // => false + * + * const item1 = { + * a: ['b'] + * } + * const item2 = { + * a: ['c'] + * } + * + * deepEqual(item1, item2); + * // => false + * ``` + * + * @function deepEqual + * @category Comparison + * + * @arg {*} item1 + * @arg {*} item2 + * + * @returns {Boolean} + */ +export default (item1, item2) => { + return Object.is(item1, item2) || !traverse(item1, (path, value, isCircular) => { + if (isCircular && Object.is(value, get(item2, path))) { + return false; + } + + return !isEqual(value, get(item2, path)); + }); +}; diff --git a/tests/deepEqual.test.js b/tests/deepEqual.test.js index b37eb0e..b6898ef 100644 --- a/tests/deepEqual.test.js +++ b/tests/deepEqual.test.js @@ -1,40 +1,54 @@ -import displayValue from 'display-value'; -import { assert } from 'type-enforcer'; -import { clone, deepEqual } from '../index.js'; -import { testValues } from './testValues.js'; - -describe('deepEqual', () => { - testValues.forEach((value1, index1) => { - clone(testValues).forEach((value2, index2) => { - if (index1 !== index2) { - it(`should return false for ${displayValue(value1)} [${index1}] and ${displayValue(value2)} [${index2}]`, () => { - assert.is(deepEqual(value1, value2), false); - }); - } - else { - it(`should return true for ${displayValue(value1)} [${index1}] and ${displayValue(value2)} [${index2}]`, () => { - assert.is(deepEqual(value1, value2), true); - }); - } - }); - }); - - it('should compare a circular reference', () => { - const object = { - key1: 'something', - key2: { - key3: 'another' - } - }; - object.key2.key4 = [object.key2]; - const object2 = { - key1: 'something', - key2: { - key3: 'another' - } - }; - object2.key2.key4 = [object2.key2]; - - assert.equal(deepEqual(object, object2), true); - }); -}); +import displayValue from 'display-value'; +import { assert } from 'type-enforcer'; +import { clone, deepEqual } from '../index.js'; +import { testValues } from './testValues.js'; + +describe('deepEqual', () => { + testValues.forEach((value1, index1) => { + clone(testValues).forEach((value2, index2) => { + if (index1 !== index2) { + it(`should return false for ${displayValue(value1)} [${index1}] and ${displayValue(value2)} [${index2}]`, () => { + assert.is(deepEqual(value1, value2), false); + }); + } + else { + it(`should return true for ${displayValue(value1)} [${index1}] and ${displayValue(value2)} [${index2}]`, () => { + assert.is(deepEqual(value1, value2), true); + }); + } + }); + }); + + it('should compare the same item referenced multiple times', () => { + const item = {}; + + assert.equal(deepEqual([{ + item + }, { + item + }], [{ + item + }, { + item + }]), true); + }); + + it('should compare seperate circular references in different objects', () => { + const object = { + key1: 'something', + key2: { + key3: 'another' + } + }; + object.key2.key4 = [object.key2]; + const object2 = { + key1: 'something', + key2: { + key3: 'another' + } + }; + object2.key2.key4 = [object2.key2]; + + assert.equal(deepEqual(object, object2), true); + }); +});