diff --git a/.eslintrc b/.eslintrc index d48d448..1afa2a8 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,5 +1,4 @@ { - "extends": ["airbnb"], "parser": "@typescript-eslint/parser", "plugins": ["@typescript-eslint"], "settings": { diff --git a/__tests__/__helpers__/testSuites.js b/__tests__/__helpers__/testSuites.js index 8a9bc6d..f743d5f 100644 --- a/__tests__/__helpers__/testSuites.js +++ b/__tests__/__helpers__/testSuites.js @@ -49,6 +49,20 @@ module.exports = [ value1: -Infinity, value2: Infinity, }, + { + deepEqual: true, + description: 'equal number objects', + shallowEqual: true, + value1: new Number(1), + value2: new Number(1), + }, + { + deepEqual: false, + description: 'not equal number objects', + shallowEqual: false, + value1: new Number(1), + value2: new Number(2), + }, { deepEqual: false, description: 'number and array are not equal', @@ -84,6 +98,20 @@ module.exports = [ value1: 'a', value2: 'b', }, + { + deepEqual: true, + description: 'equal string objects', + shallowEqual: true, + value1: new String('foo'), + value2: new String('foo'), + }, + { + deepEqual: false, + description: 'not equal string objects', + shallowEqual: false, + value1: new String('foo'), + value2: new String('bar'), + }, { deepEqual: false, description: 'empty string and null are not equal', @@ -112,6 +140,20 @@ module.exports = [ value1: false, value2: false, }, + { + deepEqual: true, + description: 'equal boolean objects (true)', + shallowEqual: true, + value1: new Boolean(true), + value2: new Boolean(true), + }, + { + deepEqual: true, + description: 'equal boolean objects (false)', + shallowEqual: true, + value1: new Boolean(false), + value2: new Boolean(false), + }, { deepEqual: false, description: 'not equal booleans', @@ -119,6 +161,13 @@ module.exports = [ value1: true, value2: false, }, + { + deepEqual: false, + description: 'not equal boolean objects', + shallowEqual: false, + value1: new Boolean(true), + value2: new Boolean(false), + }, { deepEqual: false, description: '1 and true are not equal', diff --git a/src/comparator.ts b/src/comparator.ts index e436c46..162876e 100644 --- a/src/comparator.ts +++ b/src/comparator.ts @@ -1,6 +1,4 @@ import { - EqualityComparator, - InternalEqualityComparator, areArraysEqual, areMapsEqual, areObjectsEqual, @@ -11,17 +9,33 @@ import { sameValueZeroEqual, } from './utils'; +import type { EqualityComparator, InternalEqualityComparator } from './types'; + const HAS_MAP_SUPPORT = typeof Map === 'function'; const HAS_SET_SUPPORT = typeof Set === 'function'; -export type EqualityComparatorCreator = (fn: EqualityComparator) => InternalEqualityComparator; +const { valueOf } = Object.prototype; + +export type EqualityComparatorCreator = ( + fn: EqualityComparator, +) => InternalEqualityComparator; -export function createComparator(createIsEqual?: EqualityComparatorCreator): EqualityComparator { +export function createComparator( + createIsEqual?: EqualityComparatorCreator, +): EqualityComparator { const isEqual: InternalEqualityComparator = /* eslint-disable no-use-before-define */ typeof createIsEqual === 'function' ? createIsEqual(comparator) - : (a: any, b: any, indexOrKeyA: any, indexOrKeyB: any, parentA: any, parentB: any, meta: any) => comparator(a, b, meta); + : ( + a: any, + b: any, + indexOrKeyA: any, + indexOrKeyB: any, + parentA: any, + parentB: any, + meta: any, + ) => comparator(a, b, meta); /* eslint-enable */ /** @@ -87,6 +101,10 @@ export function createComparator(createIsEqual?: EqualityComparatorCreator): Equ } } + if (a.valueOf !== valueOf || b.valueOf !== valueOf) { + return sameValueZeroEqual(a.valueOf(), b.valueOf()); + } + return areObjectsEqual(a, b, isEqual, meta); } diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..8e31e6b --- /dev/null +++ b/src/types.ts @@ -0,0 +1,15 @@ +export type InternalEqualityComparator = ( + objectA: any, + objectB: any, + indexOrKeyA: any, + indexOrKeyB: any, + parentA: any, + parentB: any, + meta: any, +) => boolean; + +export type EqualityComparator = ( + objectA: A, + objectB: B, + meta?: Meta, +) => boolean; diff --git a/src/utils.ts b/src/utils.ts index d48123f..3d05aef 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,28 +1,14 @@ -const HAS_WEAKSET_SUPPORT = typeof WeakSet === 'function'; +import type { EqualityComparator, InternalEqualityComparator } from './types'; const { keys } = Object; +const HAS_WEAKSET_SUPPORT = typeof WeakSet === 'function'; + type Cache = { add: (value: any) => void; has: (value: any) => boolean; }; -export type InternalEqualityComparator = ( - objectA: any, - objectB: any, - indexOrKeyA: any, - indexOrKeyB: any, - parentA: any, - parentB: any, - meta: any, -) => boolean; - -export type EqualityComparator = ( - objectA: A, - objectB: B, - meta?: Meta, -) => boolean; - /** * are the values passed strictly equal or both NaN *