Skip to content

Commit

Permalink
feat: expose default equal function (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
divdavem committed Dec 8, 2023
1 parent 2776f34 commit db524b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
SubscriberObject,
asWritable,
asReadable,
equal,
batch,
computed,
derived,
Expand Down Expand Up @@ -659,6 +660,39 @@ describe('stores', () => {
fixture.destroy();
expect(componentInstance.store.hasListeners).toBe(false);
});

it('equal should work as designed', () => {
// returns false for objects and functions:
const obj = {};
const fn = () => {};
expect(equal(obj, obj)).toBe(false);
expect(equal(fn, fn)).toBe(false);
expect(equal(obj, fn)).toBe(false);
expect(equal(fn, obj)).toBe(false);
expect(equal(obj, {})).toBe(false);
expect(equal(fn, () => {})).toBe(false);
// works as Object.is for other values:
expect(equal(true, true)).toBe(true);
expect(equal(false, false)).toBe(true);
expect(equal(true, false)).toBe(false);
expect(equal(false, true)).toBe(false);
expect(equal(0, 0)).toBe(true);
expect(equal(1, 1)).toBe(true);
expect(equal(NaN, NaN)).toBe(true);
expect(equal(NaN, null)).toBe(false);
expect(equal(null, NaN)).toBe(false);
expect(equal(Infinity, NaN)).toBe(false);
expect(equal(Infinity, Infinity)).toBe(true);
expect(equal(-Infinity, -Infinity)).toBe(true);
expect(equal(Infinity, -Infinity)).toBe(false);
expect(equal(-1, 1)).toBe(false);
expect(equal('a', 'b')).toBe(false);
expect(equal('a', 'a')).toBe(true);
expect(equal(null, undefined)).toBe(false);
expect(equal(undefined, null)).toBe(false);
expect(equal(undefined, undefined)).toBe(true);
expect(equal(null, null)).toBe(true);
});
});

describe('readable shorthand', () => {
Expand Down
15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,19 @@ const createEqualCache = (valueIndex: number): Record<number, boolean> => ({

const skipEqualInSet = Symbol();

/**
* Default implementation of the equal function used by tansu when a store
* changes, to know if listeners need to be notified.
* Returns false if `a` is a function or an object, or if `a` and `b`
* are different according to `Object.is`. Otherwise, returns true.
*
* @param a - First value to compare.
* @param b - Second value to compare.
* @returns true if a and b are considered equal.
*/
export const equal = <T>(a: T, b: T): boolean =>
Object.is(a, b) && (!a || typeof a !== 'object') && typeof a !== 'function';

/**
* Base class that can be extended to easily create a custom {@link Readable} store.
*
Expand Down Expand Up @@ -566,7 +579,7 @@ export abstract class Store<T> implements Readable<T> {
* @returns true if a and b are considered different.
*/
protected notEqual(a: T, b: T): boolean {
return !Object.is(a, b) || (a && typeof a === 'object') || typeof a === 'function';
return !equal(a, b);
}

/**
Expand Down

0 comments on commit db524b7

Please sign in to comment.