From 36b5286a0906f344b6a0927520bf6c7b6ce2090d Mon Sep 17 00:00:00 2001 From: Pablo Baleztena Date: Wed, 23 Oct 2024 23:45:08 -0300 Subject: [PATCH] [fabric/core] Add JSON utility functions for parsing and stringifying with core types --- packages/fabric/core/deno.json | 3 ++ packages/fabric/core/index.ts | 2 + packages/fabric/core/utils/index.ts | 1 + packages/fabric/core/utils/json-utils.ts | 43 ++++++++++++++++++++++ packages/fabric/domain/deno.json | 3 +- packages/fabric/domain/utils/index.ts | 1 - packages/fabric/domain/utils/json-utils.ts | 14 ------- 7 files changed, 50 insertions(+), 17 deletions(-) create mode 100644 packages/fabric/core/utils/json-utils.ts delete mode 100644 packages/fabric/domain/utils/json-utils.ts diff --git a/packages/fabric/core/deno.json b/packages/fabric/core/deno.json index 9e23e9d..6c1ba7d 100644 --- a/packages/fabric/core/deno.json +++ b/packages/fabric/core/deno.json @@ -3,5 +3,8 @@ "version": "0.1.0", "exports": { ".": "./index.ts" + }, + "imports": { + "decimal": "jsr:@quentinadam/decimal@^0.1.6" } } diff --git a/packages/fabric/core/index.ts b/packages/fabric/core/index.ts index ad77c14..dfbd794 100644 --- a/packages/fabric/core/index.ts +++ b/packages/fabric/core/index.ts @@ -1,3 +1,4 @@ +import Decimal from "decimal"; export * from "./array/index.ts"; export * from "./error/index.ts"; export * from "./record/index.ts"; @@ -7,3 +8,4 @@ export * from "./time/index.ts"; export * from "./types/index.ts"; export * from "./utils/index.ts"; export * from "./variant/index.ts"; +export { Decimal }; diff --git a/packages/fabric/core/utils/index.ts b/packages/fabric/core/utils/index.ts index 32f4fea..67fd1a9 100644 --- a/packages/fabric/core/utils/index.ts +++ b/packages/fabric/core/utils/index.ts @@ -1 +1,2 @@ export * from "./ensure.ts"; +export * from "./json-utils.ts"; diff --git a/packages/fabric/core/utils/json-utils.ts b/packages/fabric/core/utils/json-utils.ts new file mode 100644 index 0000000..a2f47f5 --- /dev/null +++ b/packages/fabric/core/utils/json-utils.ts @@ -0,0 +1,43 @@ +import { isRecord, PosixDate } from "@fabric/core"; +import Decimal from "decimal"; + +export namespace JSONUtils { + export function reviver(key: string, value: unknown) { + if (isRecord(value)) { + if (value._type === "bigint" && typeof value.value == "string") { + return BigInt(value.value); + } + + if (value._type === "decimal" && typeof value.value == "string") { + return Decimal.from(value.value); + } + + if (PosixDate.isPosixDateJSON(value)) { + return PosixDate.fromJson(value); + } + } + return value; + } + + export function parse(json: string): T { + return JSON.parse(json, reviver); + } + + export function stringify(value: T): string { + return JSON.stringify(value, (key, value) => { + if (typeof value === "bigint") { + return { + _type: "bigint", + value: value.toString(), + }; + } + if (value instanceof Decimal) { + return { + _type: "decimal", + value: value.toString(), + }; + } + return value; + }); + } +} diff --git a/packages/fabric/domain/deno.json b/packages/fabric/domain/deno.json index 6621ea7..4732b66 100644 --- a/packages/fabric/domain/deno.json +++ b/packages/fabric/domain/deno.json @@ -6,7 +6,6 @@ }, "imports": { "@fabric/core": "jsr:@fabric/core", - "@fabric/validations": "jsr:@fabric/validations", - "decimal": "jsr:@quentinadam/decimal@^0.1.6" + "@fabric/validations": "jsr:@fabric/validations" } } diff --git a/packages/fabric/domain/utils/index.ts b/packages/fabric/domain/utils/index.ts index 385247d..72c446e 100644 --- a/packages/fabric/domain/utils/index.ts +++ b/packages/fabric/domain/utils/index.ts @@ -1,2 +1 @@ -export * from "./json-utils.ts"; export * from "./sort-by-dependencies.ts"; diff --git a/packages/fabric/domain/utils/json-utils.ts b/packages/fabric/domain/utils/json-utils.ts deleted file mode 100644 index 22d3340..0000000 --- a/packages/fabric/domain/utils/json-utils.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { PosixDate } from "@fabric/core"; - -export namespace JSONUtils { - export function reviver(key: string, value: unknown) { - if (PosixDate.isPosixDateJSON(value)) { - return PosixDate.fromJson(value); - } - return value; - } - - export function parse(json: string): T { - return JSON.parse(json, reviver); - } -}