-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add static
areEqual
utility method (#15)
- Loading branch information
1 parent
aa2c8a8
commit 8eca271
Showing
6 changed files
with
122 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { DeepMap } from './src/map'; | ||
export { DeepSet } from './src/set'; | ||
export { areEqual } from './src/areEqual'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { areEqual } from '../areEqual'; | ||
|
||
/** | ||
* NOTE: areEqual relies on the use of a DeepSet, so we won't exhaustively cover the different equality scenarios. | ||
*/ | ||
describe('areEqual', () => { | ||
describe('Using object values', () => { | ||
const a = { key: 'value' }; | ||
const b = { key: 'value' }; | ||
const c = { key: 'value' }; | ||
const d = { key: 'otherValue' }; | ||
|
||
it('Returns true when all values are equal', async () => { | ||
expect(areEqual([a, b])).toBe(true); | ||
expect(areEqual([b, c])).toBe(true); | ||
expect(areEqual([a, b, c])).toBe(true); | ||
expect(areEqual([a])).toBe(true); // we'll define this as vacuously true | ||
}); | ||
|
||
it('Returns false when all values are not equal', async () => { | ||
expect(areEqual([a, d])).toBe(false); | ||
expect(areEqual([b, d])).toBe(false); | ||
expect(areEqual([a, b, c, d])).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('Using primitive values', () => { | ||
const a = 'value'; | ||
const b = 'value'; | ||
const c = 'value'; | ||
const d = 'otherValue'; | ||
|
||
it('Returns true when all values are equal', async () => { | ||
expect(areEqual([a, b])).toBe(true); | ||
expect(areEqual([b, c])).toBe(true); | ||
expect(areEqual([a, b, c])).toBe(true); | ||
expect(areEqual([a])).toBe(true); // we'll define this as vacuously true | ||
}); | ||
|
||
it('Returns false when all values are not equal', async () => { | ||
expect(areEqual([a, d])).toBe(false); | ||
expect(areEqual([b, d])).toBe(false); | ||
expect(areEqual([a, b, c, d])).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('Using options', () => { | ||
type MyObject = { key: string; other: string }; | ||
const a = { key: 'value', other: 'a' }; | ||
const b = { key: 'value', other: 'b' }; | ||
const c = { key: 'value', other: 'c' }; | ||
const opts = { | ||
transformer: (obj: MyObject): string => obj.key, | ||
}; | ||
|
||
it('Returns true when all values are equal according to transformer', async () => { | ||
expect(areEqual([a, b])).toBe(false); | ||
expect(areEqual([a, b, c])).toBe(false); | ||
// Now, with the transformer option | ||
expect(areEqual([a, b], opts)).toBe(true); | ||
expect(areEqual([a, b, c], opts)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('Misc', () => { | ||
it('Throws error when passing empty list of values', async () => { | ||
expect(() => areEqual([])).toThrow('Empty values list passed to areEqual function'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Options } from './options'; | ||
import { DeepSet } from './set'; | ||
|
||
/** | ||
* Static utility for doing a one-time equality check across the provided values. | ||
* @param values list whose elements will be compared to each other | ||
* @param options configuration options | ||
* @returns true if every element in `values` is equal to every other element | ||
* @throws {Error} if `values` list is empty | ||
*/ | ||
export function areEqual<V, TxV = V>(values: V[], options?: Options<V, null, TxV, null>): boolean { | ||
if (values.length === 0) { | ||
throw new Error('Empty values list passed to areEqual function'); | ||
} | ||
const set = new DeepSet(values, options); | ||
return set.size === 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters