Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 73 - primitive wrapper support #75

Merged
merged 5 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"extends": ["airbnb"],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"settings": {
Expand Down
49 changes: 49 additions & 0 deletions __tests__/__helpers__/testSuites.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -112,13 +140,34 @@ 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',
shallowEqual: false,
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',
Expand Down
28 changes: 23 additions & 5 deletions src/comparator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
EqualityComparator,
InternalEqualityComparator,
areArraysEqual,
areMapsEqual,
areObjectsEqual,
Expand All @@ -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 */

/**
Expand Down Expand Up @@ -87,6 +101,10 @@ export function createComparator(createIsEqual?: EqualityComparatorCreator): Equ
}
}

if (a.valueOf !== valueOf || b.valueOf !== valueOf) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the real change, doing SMZ if the valueOf differs from the default on all objects.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I'm sure you could tell from the issue I filed, I was really wondering how/where you could possibly detect this without a large number of checks. This is a clever solution, I like it a lot.

return sameValueZeroEqual(a.valueOf(), b.valueOf());
}

return areObjectsEqual(a, b, isEqual, meta);
}

Expand Down
15 changes: 15 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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 = <A, B, Meta>(
objectA: A,
objectB: B,
meta?: Meta,
) => boolean;
20 changes: 3 additions & 17 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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 = <A, B, Meta>(
objectA: A,
objectB: B,
meta?: Meta,
) => boolean;

/**
* are the values passed strictly equal or both NaN
*
Expand Down