From 33e569acf977252bd41e4cfdc21a7fd130dd5c2c Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Fri, 8 Nov 2024 08:04:06 +0900 Subject: [PATCH 1/2] fix(types): argument type and response type will be the same --- index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.ts b/index.ts index 9f28fe3..5c278ee 100644 --- a/index.ts +++ b/index.ts @@ -19,7 +19,7 @@ export type JsonValue = number | string | boolean | null | JsonValue[] | { [key: * `Set`, `Buffer`, ... are not allowed. * @returns The cloned JSON value. */ -export default function cloneJSON(value: JsonValue): JsonValue { +export default function cloneJSON(value: V): V { if (typeof value !== 'object' || value === null) { return value; } else if (Array.isArray(value)) { From 0b43c6aa7fa7f3e4c4c2d17fb383df272e9d48c4 Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Fri, 8 Nov 2024 08:11:06 +0900 Subject: [PATCH 2/2] Update index.ts --- index.ts | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/index.ts b/index.ts index 5c278ee..9e547c2 100644 --- a/index.ts +++ b/index.ts @@ -8,24 +8,19 @@ * @module */ -export type JsonValue = number | string | boolean | null | JsonValue[] | { [key: string]: JsonValue }; +export type JsonPrimitive = number | string | boolean | null; +export type JsonArray = JsonValue[]; +export type JsonObject = { [key: string]: JsonValue }; -/** - * This function clones the given JSON value. - * - * @param value The JSON value to be cloned. There are two invariants. 1) It must not contain circles - * as JSON does not allow it. This function will cause infinite loop for such values by - * design. 2) It must contain JSON values only. Other values like `Date`, `Regexp`, `Map`, - * `Set`, `Buffer`, ... are not allowed. - * @returns The cloned JSON value. - */ -export default function cloneJSON(value: V): V { +export type JsonValue = JsonPrimitive | JsonArray | JsonObject; + +function _cloneJSON(value: JsonValue): JsonValue { if (typeof value !== 'object' || value === null) { return value; } else if (Array.isArray(value)) { return value.map(e => (typeof e !== 'object' || e === null ? e : cloneJSON(e))); } else { - const ret: { [key: string]: JsonValue } = {}; + const ret: JsonObject = {}; for (const k in value) { const v = value[k]; ret[k] = typeof v !== 'object' || v === null ? v : cloneJSON(v); @@ -33,3 +28,17 @@ export default function cloneJSON(value: V): V { return ret; } } + +/** + * This function clones the given JSON value. + * + * @param value The JSON value to be cloned. There are two invariants. 1) It must not contain circles + * as JSON does not allow it. This function will cause infinite loop for such values by + * design. 2) It must contain JSON values only. Other values like `Date`, `Regexp`, `Map`, + * `Set`, `Buffer`, ... are not allowed. + * @returns The cloned JSON value. + */ +function cloneJSON(value: V): V { + // note: autoregressive function + return _cloneJSON(value) as V +}