diff options
Diffstat (limited to 'engine/utils')
-rw-r--r-- | engine/utils/coding.ts | 27 | ||||
-rw-r--r-- | engine/utils/index.ts | 1 |
2 files changed, 28 insertions, 0 deletions
diff --git a/engine/utils/coding.ts b/engine/utils/coding.ts new file mode 100644 index 0000000..4c1b17f --- /dev/null +++ b/engine/utils/coding.ts @@ -0,0 +1,27 @@ +const replacer = (_key: any, value: any) => { + if (value instanceof Map) { + return { + dataType: "Map", + value: Array.from(value.entries()), + }; + } else { + return value; + } +}; + +const reviver = (_key: any, value: any) => { + if (typeof value === "object" && value !== null) { + if (value.dataType === "Map") { + return new Map(value.value); + } + } + return value; +}; + +export const stringify = (obj: any) => { + return JSON.stringify(obj, replacer); +}; + +export const parse = <T>(str: string) => { + return JSON.parse(str, reviver) as unknown as T; +}; diff --git a/engine/utils/index.ts b/engine/utils/index.ts index 82a0d05..b70734f 100644 --- a/engine/utils/index.ts +++ b/engine/utils/index.ts @@ -1,3 +1,4 @@ export * from "./rotateVector"; export * from "./dotProduct"; export * from "./clamp"; +export * from "./coding"; |