-
Notifications
You must be signed in to change notification settings - Fork 48
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
Showing
14 changed files
with
246 additions
and
86 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
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
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
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
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,16 @@ | ||
export abstract class Value { | ||
/** | ||
* Compare equality with another value. | ||
*/ | ||
abstract equals(other: unknown): boolean; | ||
|
||
/** | ||
* Convert this value to a serializable string | ||
*/ | ||
abstract toJSON(): unknown; | ||
|
||
/** | ||
* Convert this value to a string representation | ||
*/ | ||
abstract toString(): string; | ||
} |
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,38 @@ | ||
import { Value } from "../data/value"; | ||
|
||
/** | ||
* Compares two values for deep equality, including arrays, objects, | ||
* and SurrealQL value types. | ||
* | ||
* @param x The first value to compare | ||
* @param y The second value to compare | ||
* @returns Whether the two values are recursively equal | ||
*/ | ||
export function equals(x: unknown, y: unknown): boolean { | ||
if (Object.is(x, y)) return true; | ||
if (x instanceof Date && y instanceof Date) { | ||
return x.getTime() === y.getTime(); | ||
} | ||
if (x instanceof RegExp && y instanceof RegExp) { | ||
return x.toString() === y.toString(); | ||
} | ||
if (x instanceof Value && y instanceof Value) { | ||
return x.equals(y); | ||
} | ||
if ( | ||
typeof x !== "object" || | ||
x === null || | ||
typeof y !== "object" || | ||
y === null | ||
) { | ||
return false; | ||
} | ||
const keysX = Reflect.ownKeys(x as unknown as object) as (keyof typeof x)[]; | ||
const keysY = Reflect.ownKeys(y as unknown as object); | ||
if (keysX.length !== keysY.length) return false; | ||
for (let i = 0; i < keysX.length; i++) { | ||
if (!Reflect.has(y as unknown as object, keysX[i])) return false; | ||
if (!equals(x[keysX[i]], y[keysX[i]])) return false; | ||
} | ||
return true; | ||
} |
Oops, something went wrong.