-
Notifications
You must be signed in to change notification settings - Fork 196
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
8 changed files
with
113 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Helper to serialize values that are not natively serializable and therefore not transferrable to the page | ||
* For now only `bigint` needs serialization. | ||
*/ | ||
export function serialize(obj: unknown): string { | ||
return JSON.stringify(obj, (_, v) => (typeof v === "bigint" ? `bigint(${v.toString()})` : v)); | ||
} | ||
|
||
/** | ||
* Helper to deserialize values that were serialized by `serialize` (because they are not natively serializable). | ||
* For now only `bigint` is serialized and need to be deserialized here. | ||
*/ | ||
export function deserialize(blob: string): Record<string, unknown> { | ||
const obj = JSON.parse(blob); | ||
|
||
// Check whether the value matches the mattern `${number}n` | ||
// (serialization of bigint in `serialize`) | ||
// and turn it back into a bigint | ||
const regex = /^bigint\((.*)\)$/; // Regular expression pattern. | ||
for (const [key, value] of Object.entries(obj)) { | ||
const match = typeof value === "string" && value.match(regex); // Attempt to match the pattern. | ||
if (match) { | ||
obj[key] = BigInt(match[1]); | ||
} | ||
} | ||
|
||
return obj; | ||
} |