Skip to content

Commit

Permalink
clean up typing
Browse files Browse the repository at this point in the history
  • Loading branch information
planttheidea committed Feb 27, 2023
1 parent 058724a commit 30d8418
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
13 changes: 6 additions & 7 deletions __tests__/recipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ describe('recipes', () => {
value: string;
}

const mutableState: Meta = { value: 'baz' };

const createState: CreateState<Meta> = (deepEqual) => ({
equals: (a, b, _keyA, _keyB, _parentA, _parentB, state) =>
deepEqual(a, b, state) ||
a === state.meta.value ||
b === state.meta.value,
meta: { value: 'baz' },
});

const deepEqual = createCustomEqual({ createState });
Expand All @@ -97,18 +96,18 @@ describe('recipes', () => {
const b = { bar: 'bar' };
const c = { bar: 'quz' };

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

it('should verify the object against meta', () => {
const a = { foo: 'bar', bar: 'baz' };
const b = { foo: 'bar', bar: 'baz' };
const c = { foo: 'bar', bar: 'quz' };

expect(deepEqual(a, b, mutableState)).toBe(true);
expect(deepEqual(a, c, mutableState)).toBe(true);
expect(deepEqual(a, {}, mutableState)).toBe(false);
expect(deepEqual(a, b)).toBe(true);
expect(deepEqual(a, c)).toBe(true);
expect(deepEqual(a, {})).toBe(false);
});
});

Expand Down
17 changes: 14 additions & 3 deletions src/equals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { getStrictProperties, hasOwn, sameValueZeroEqual } from './utils';
import type { Dictionary, State, TypedArray } from './internalTypes';
import type {
Dictionary,
PrimitiveWrapper,
State,
TypedArray,
} from './internalTypes';

const OWNER = '_owner';

Expand Down Expand Up @@ -98,7 +103,7 @@ export function areObjectsEqual(
return false;
}

let property: string | symbol;
let property: string;

// Decrementing `while` showed faster results than either incrementing or
// decrementing `for` loop and than an incrementing `while` loop. Declarative
Expand Down Expand Up @@ -192,7 +197,10 @@ export function areObjectsEqualStrict(
/**
* Whether the primitive wrappers passed are equal in value.
*/
export function arePrimitiveWrappersEqual(a: Date, b: Date): boolean {
export function arePrimitiveWrappersEqual(
a: PrimitiveWrapper,
b: PrimitiveWrapper,
): boolean {
return sameValueZeroEqual(a.valueOf(), b.valueOf());
}

Expand Down Expand Up @@ -249,6 +257,9 @@ export function areSetsEqual(
return isValueEqual;
}

/**
* Whether the TypedArray instances are equal in value.
*/
export function areTypedArraysEqual(a: TypedArray, b: TypedArray) {
let index = a.length;

Expand Down
19 changes: 8 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
CircularState,
CustomEqualCreatorOptions,
DefaultState,
State,
} from './internalTypes';
import { createInternalComparator, sameValueZeroEqual } from './utils';

Expand Down Expand Up @@ -74,7 +75,7 @@ export const strictCircularShallowEqual = createCustomEqual({
* support for legacy environments that do not support expected features like
* `RegExp.prototype.flags` out of the box.
*/
export function createCustomEqual<Meta>(
export function createCustomEqual<Meta = undefined>(
options: CustomEqualCreatorOptions<Meta> = {},
) {
const {
Expand All @@ -94,11 +95,7 @@ export function createCustomEqual<Meta>(
: createInternalComparator(isEqualCustom));

if (createState) {
return function isEqual<A, B>(
a: A,
b: B,
metaOverride?: Meta | undefined,
): boolean {
return function isEqual<A, B>(a: A, b: B): boolean {
const {
cache = circular ? new WeakMap() : undefined,
equals = isEqualCustomComparator,
Expand All @@ -109,9 +106,9 @@ export function createCustomEqual<Meta>(
return isEqualCustom(a, b, {
cache,
equals,
meta: metaOverride !== undefined ? metaOverride : meta,
meta,
strict,
} as CircularState<Meta>);
} as State<Meta>);
};
}

Expand All @@ -126,14 +123,14 @@ export function createCustomEqual<Meta>(
};
}

const state = Object.freeze({
const state = {
cache: undefined,
equals: isEqualCustomComparator,
meta: undefined,
strict: baseStrict,
});
} as DefaultState<Meta>;

return function equals<A, B>(a: A, b: B): boolean {
return isEqualCustom(a, b, state as DefaultState<Meta>);
return isEqualCustom(a, b, state);
};
}
4 changes: 4 additions & 0 deletions src/internalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export type InternalEqualityComparator<Meta> = (
state: State<Meta>,
) => boolean;

// We explicitly check for primitive wrapper types
// eslint-disable-next-line @typescript-eslint/ban-types
export type PrimitiveWrapper = Boolean | Number | String;

export type TypedArray =
| Float32Array
| Float64Array
Expand Down

0 comments on commit 30d8418

Please sign in to comment.