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

add ability to override number comparisons #130

Merged
merged 1 commit into from
Jan 3, 2025
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
3 changes: 2 additions & 1 deletion __tests__/comparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
areTypedArraysEqual,
areUrlsEqual,
} from '../src/equals';
import { createIsCircular } from '../src/utils';
import { createIsCircular, sameValueZeroEqual } from '../src/utils';

import type { InternalEqualityComparator, State } from '../src/internalTypes';

Expand All @@ -25,6 +25,7 @@ const STANDARD_COMPARATOR_OPTIONS = {
areErrorsEqual,
areFunctionsEqual,
areMapsEqual,
areNumbersEqual: sameValueZeroEqual,
areObjectsEqual,
arePrimitiveWrappersEqual,
areRegExpsEqual,
Expand Down
26 changes: 26 additions & 0 deletions __tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,32 @@ describe('strict', () => {
expect(customEqual(a, b)).toBe(false);
});

it('issue 112 - handle custom float comparison', () => {
const customEqual = createCustomEqual({
createCustomConfig: () => ({
areNumbersEqual(a, b) {
if (a === b) {
return true;
}

const diff = Math.abs(a - b);

if (diff < Number.EPSILON) {
return true;
}

return diff <= Number.EPSILON * Math.min(Math.abs(a), Math.abs(b));
},
}),
});

const a = 0.1 + 0.2;
const b = 0.3;

expect(deepEqual(a, b)).toBe(false);
expect(customEqual(a, b)).toBe(true);
});

it('issue 123 - custom function equality comparator', () => {
const customEqual = createCustomEqual({
createCustomConfig: () => ({
Expand Down
8 changes: 4 additions & 4 deletions src/comparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
areErrorsEqual as areErrorsEqualDefault,
areFunctionsEqual as areFunctionsEqualDefault,
areMapsEqual as areMapsEqualDefault,
areNumbersEqual as areNumbersEqualDefault,
areObjectsEqual as areObjectsEqualDefault,
areObjectsEqualStrict as areObjectsEqualStrictDefault,
arePrimitiveWrappersEqual as arePrimitiveWrappersEqualDefault,
Expand Down Expand Up @@ -61,6 +62,7 @@ export function createEqualityComparator<Meta>({
areErrorsEqual,
areFunctionsEqual,
areMapsEqual,
areNumbersEqual,
areObjectsEqual,
arePrimitiveWrappersEqual,
areRegExpsEqual,
Expand Down Expand Up @@ -91,10 +93,7 @@ export function createEqualityComparator<Meta>({

if (type !== 'object') {
if (type === 'number') {
// If the items are numbers, then the only possibility of them being equal but not strictly is if they are both
// `NaN`. Since `NaN` is uniquely not equal to itself, we can use self-comparison of both objects, which is
// faster than `isNaN()`.
return a !== a && b !== b;
return areNumbersEqual(a, b, state);
}

if (type === 'function') {
Expand Down Expand Up @@ -253,6 +252,7 @@ export function createEqualityComparatorConfig<Meta>({
areMapsEqual: strict
? combineComparators(areMapsEqualDefault, areObjectsEqualStrictDefault)
: areMapsEqualDefault,
areNumbersEqual: areNumbersEqualDefault,
areObjectsEqual: strict
? areObjectsEqualStrictDefault
: areObjectsEqualDefault,
Expand Down
5 changes: 5 additions & 0 deletions src/equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ export function areMapsEqual(
return true;
}

/**
* Whether the numbers are equal in value.
*/
export const areNumbersEqual = sameValueZeroEqual;

/**
* Whether the objects are equal in value.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/internalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export interface ComparatorConfig<Meta> {
* additional properties added to the map.
*/
areMapsEqual: TypeEqualityComparator<any, Meta>;
/**
* Whether the numbers passed are equal in value.
*/
areNumbersEqual: TypeEqualityComparator<any, Meta>;
/**
* Whether the objects passed are equal in value. In strict mode, this includes
* non-enumerable properties added to the map, as well as symbol properties.
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ export const hasOwn =
* Whether the values passed are strictly equal or both NaN.
*/
export function sameValueZeroEqual(a: any, b: any): boolean {
return a || b ? a === b : a === b || (a !== a && b !== b);
return a === b || (!a && !b && a !== a && b !== b);
planttheidea marked this conversation as resolved.
Show resolved Hide resolved
}