-
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.
- Loading branch information
1 parent
baf8935
commit 58839f7
Showing
2 changed files
with
49 additions
and
0 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 @@ | ||
export function assert(condition: boolean, message: string) { | ||
if (!condition) { | ||
throw new Error(message); | ||
} | ||
} |
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,44 @@ | ||
/** | ||
* Compare any two objects | ||
* @param a | ||
* @param b | ||
* @returns true if the objects are deep equal | ||
*/ | ||
export function deepEqual(a: any, b: any): boolean { | ||
if (a === b) { | ||
return true; | ||
} | ||
if (!a || !b) { | ||
return false; | ||
} | ||
if (Array.isArray(a)) { | ||
if (!Array.isArray(b) || a.length !== b.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < a.length; i++) { | ||
if (!deepEqual(a[i], b[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} else if (Array.isArray(b)) { | ||
return false; | ||
} | ||
if (typeof a === 'object' && typeof b === 'object') { | ||
const aKeys = Object.keys(a); | ||
const bKeys = Object.keys(b); | ||
if (aKeys.length !== bKeys.length) { | ||
return false; | ||
} | ||
for (const key of aKeys) { | ||
if (!b.hasOwnProperty(key)) { | ||
return false; | ||
} | ||
if (!deepEqual(a[key], b[key])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
} |