diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-09-05 22:28:11 -0600 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-09-05 22:28:11 -0600 |
commit | 6708160cec15ccd4a79979ed8ba79310fbd9285f (patch) | |
tree | f716105066534b901399f2f2e5423c7caf7a773d /engine/utils/coding.ts | |
parent | d19da30f6dbf316bf89355b9b840a6d77e5435ec (diff) | |
download | jumpstorm-6708160cec15ccd4a79979ed8ba79310fbd9285f.tar.gz jumpstorm-6708160cec15ccd4a79979ed8ba79310fbd9285f.zip |
Diffstat (limited to 'engine/utils/coding.ts')
-rw-r--r-- | engine/utils/coding.ts | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/engine/utils/coding.ts b/engine/utils/coding.ts index 283844f..d0904fe 100644 --- a/engine/utils/coding.ts +++ b/engine/utils/coding.ts @@ -1,3 +1,8 @@ +import { compatto } from './compatto'; +import dictionary from './dictionary'; + +const { compress, decompress } = compatto({ dictionary }); + const replacer = (_key: any, value: any) => { if (value instanceof Map) { return { @@ -31,10 +36,17 @@ const reviver = (_key: any, value: any) => { }; // "deterministic" stringify -export const stringify = (obj: any) => { + +export const stringify = (obj: any): string => { return JSON.stringify(sortObj(obj), replacer); }; -export const parse = <T>(str: string) => { - return JSON.parse(str, reviver) as unknown as T; +export const serialize = (obj: any): Uint8Array => { + //return new Uint8Array(new TextEncoder().encode(stringify(obj))); + return compress(stringify(obj)); +}; + +export const parse = <T>(serialized: Uint8Array): T => { + //return JSON.parse(new TextDecoder().decode(serialized), reviver) as T; + return JSON.parse(decompress(serialized), reviver) as T; }; |