-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90e2245
commit b5991ca
Showing
6 changed files
with
188 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@layerzerolabs/devtools": patch | ||
--- | ||
|
||
Add isDeepEqual utility |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { deepStrictEqual } from 'assert' | ||
|
||
/** | ||
* Compares two object by value, returning `true` if they match | ||
* | ||
* ``` | ||
* const theyMatch = isDeepEqual({ a: 1 }, { a: 1 }) // true | ||
* const theyDontMatch = isDeepEqual({ a: 1 }, { a: '1' }) // false | ||
* ``` | ||
* | ||
* @param {T} a | ||
* @param {unknown} b | ||
* @returns {boolean} | ||
*/ | ||
export const isDeepEqual = (a: unknown, b: unknown): boolean => { | ||
try { | ||
return deepStrictEqual(a, b), true | ||
} catch { | ||
return false | ||
} | ||
} |
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 +1,2 @@ | ||
export * from './assertion' | ||
export * from './promise' |
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,102 @@ | ||
import { isDeepEqual } from '@/common/assertion' | ||
import fc from 'fast-check' | ||
|
||
describe('common/assertion', () => { | ||
describe('isDeepEqual', () => { | ||
const arrayArbitrary = fc.array(fc.anything()) | ||
const entriesArbitrary = fc.array(fc.tuple(fc.anything(), fc.anything())) | ||
|
||
it('should return true for identical values', () => { | ||
fc.assert( | ||
fc.property(fc.anything(), (value) => { | ||
expect(isDeepEqual(value, value)).toBeTruthy() | ||
}) | ||
) | ||
}) | ||
|
||
it('should return true for arrays containing the same values', () => { | ||
fc.assert( | ||
fc.property(arrayArbitrary, (array) => { | ||
expect(isDeepEqual(array, [...array])).toBeTruthy() | ||
expect(isDeepEqual([...array], array)).toBeTruthy() | ||
}) | ||
) | ||
}) | ||
|
||
it('should return false for arrays containing different values', () => { | ||
fc.assert( | ||
fc.property(arrayArbitrary, arrayArbitrary, (arrayA, arrayB) => { | ||
// We'll do a very simplified precondition - we'll only run tests when the first elements are different | ||
fc.pre(!isDeepEqual(arrayA[0], arrayB[0])) | ||
|
||
expect(isDeepEqual(arrayA, arrayB)).toBeFalsy() | ||
expect(isDeepEqual(arrayB, arrayA)).toBeFalsy() | ||
}) | ||
) | ||
}) | ||
|
||
it('should return false for arrays containing more values', () => { | ||
fc.assert( | ||
fc.property(arrayArbitrary, fc.anything(), (array, extraValue) => { | ||
expect(isDeepEqual(array, [...array, extraValue])).toBeFalsy() | ||
expect(isDeepEqual([...array, extraValue], array)).toBeFalsy() | ||
}) | ||
) | ||
}) | ||
|
||
it('should return true for sets containing the same values', () => { | ||
fc.assert( | ||
fc.property(arrayArbitrary, (array) => { | ||
const setA = new Set(array) | ||
const setB = new Set(array) | ||
|
||
expect(isDeepEqual(setA, setB)).toBeTruthy() | ||
expect(isDeepEqual(setB, setA)).toBeTruthy() | ||
}) | ||
) | ||
}) | ||
|
||
it('should return true for maps containing the same values', () => { | ||
fc.assert( | ||
fc.property(entriesArbitrary, (entries) => { | ||
const mapA = new Map(entries) | ||
const mapB = new Map(entries) | ||
|
||
expect(isDeepEqual(mapA, mapB)).toBeTruthy() | ||
expect(isDeepEqual(mapB, mapA)).toBeTruthy() | ||
}) | ||
) | ||
}) | ||
|
||
it('should return true for objects containing the same values', () => { | ||
fc.assert( | ||
fc.property( | ||
fc.record({ | ||
value: fc.anything(), | ||
}), | ||
(object) => { | ||
expect(isDeepEqual(object, { ...object })).toBeTruthy() | ||
expect(isDeepEqual({ ...object }, object)).toBeTruthy() | ||
} | ||
) | ||
) | ||
}) | ||
|
||
it('should return false for objects containing different values', () => { | ||
fc.assert( | ||
fc.property( | ||
fc.record({ | ||
value: fc.anything(), | ||
}), | ||
fc.anything(), | ||
(object, value) => { | ||
fc.pre(!isDeepEqual(object.value, value)) | ||
|
||
expect(isDeepEqual(object, { value })).toBeFalsy() | ||
expect(isDeepEqual({ value }, object)).toBeFalsy() | ||
} | ||
) | ||
) | ||
}) | ||
}) | ||
}) |
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