@fgv/ts-json-base 4.5.0-0 → 4.5.0-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -428,6 +428,16 @@ declare function readJsonFileSync(srcPath: string): Result<JsonValue>;
428
428
  */
429
429
  export declare function sanitizeJson(from: unknown): Result<JsonValue>;
430
430
 
431
+ /**
432
+ * Sanitizes some value using JSON stringification and parsing, returning an
433
+ * returning a matching strongly-typed value.
434
+ * @param from - The value to be sanitized.
435
+ * @returns `Success` with a {@link JsonObject | JsonObject} if conversion succeeds,
436
+ * `Failure` with details if an error occurs.
437
+ * @public
438
+ */
439
+ export declare function sanitizeJsonObject<T>(from: T): Result<T>;
440
+
431
441
  declare namespace Validators {
432
442
  export {
433
443
  IJsonValidatorContext,
@@ -92,4 +92,13 @@ export declare function pickJsonObject(src: JsonObject, path: string): Result<Js
92
92
  * @public
93
93
  */
94
94
  export declare function sanitizeJson(from: unknown): Result<JsonValue>;
95
+ /**
96
+ * Sanitizes some value using JSON stringification and parsing, returning an
97
+ * returning a matching strongly-typed value.
98
+ * @param from - The value to be sanitized.
99
+ * @returns `Success` with a {@link JsonObject | JsonObject} if conversion succeeds,
100
+ * `Failure` with details if an error occurs.
101
+ * @public
102
+ */
103
+ export declare function sanitizeJsonObject<T>(from: T): Result<T>;
95
104
  //# sourceMappingURL=common.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../../src/packlets/json/common.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,MAAM,EAAgC,MAAM,eAAe,CAAC;AAIrE;;;GAGG;AAEH,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;AAE7D;;;;GAIG;AAEH,MAAM,WAAW,UAAU;IACzB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1B;AAED;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAA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CAC1B;AAED;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG,aAAa,GAAG,UAAU,GAAG,SAAS,CAAC;AAE/D;;;GAGG;AAEH,MAAM,WAAW,SAAU,SAAQ,KAAK,CAAC,SAAS,CAAC;CAAG;AAEtD;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,WAAW,GAAG,QAAQ,GAAG,OAAO,CAAC;AAE7D;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,aAAa,CAEpE;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,UAAU,CAQ9D;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,SAAS,CAE5D;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC,aAAa,CAAC,CAStE;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,CAa9E;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC,CAOhF;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,CAE7D;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAExD"}
@@ -28,6 +28,7 @@ exports.classifyJsonValue = classifyJsonValue;
28
28
  exports.pickJsonValue = pickJsonValue;
29
29
  exports.pickJsonObject = pickJsonObject;
30
30
  exports.sanitizeJson = sanitizeJson;
31
+ exports.sanitizeJsonObject = sanitizeJsonObject;
31
32
  const ts_utils_1 = require("@fgv/ts-utils");
32
33
  /**
33
34
  * Test if an `unknown` is a {@link JsonValue | JsonValue}.
@@ -136,4 +137,15 @@ function pickJsonObject(src, path) {
136
137
  function sanitizeJson(from) {
137
138
  return (0, ts_utils_1.captureResult)(() => JSON.parse(JSON.stringify(from)));
138
139
  }
140
+ /**
141
+ * Sanitizes some value using JSON stringification and parsing, returning an
142
+ * returning a matching strongly-typed value.
143
+ * @param from - The value to be sanitized.
144
+ * @returns `Success` with a {@link JsonObject | JsonObject} if conversion succeeds,
145
+ * `Failure` with details if an error occurs.
146
+ * @public
147
+ */
148
+ function sanitizeJsonObject(from) {
149
+ return (0, ts_utils_1.captureResult)(() => JSON.parse(JSON.stringify(from)));
150
+ }
139
151
  //# sourceMappingURL=common.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/packlets/json/common.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;AAkDH,0CAEC;AASD,oCAQC;AASD,kCAEC;AAUD,8CASC;AAUD,sCAaC;AAYD,wCAOC;AAWD,oCAEC;AAxJD,4CAAqE;AAyCrE;;;;;;GAMG;AACH,SAAgB,eAAe,CAAC,IAAa;IAC3C,OAAO,OAAO,IAAI,KAAK,SAAS,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,CAAC;AAC5G,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,YAAY,CAAC,IAAa;IACxC,OAAO,CACL,OAAO,IAAI,KAAK,QAAQ;QACxB,IAAI,KAAK,IAAI;QACb,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QACpB,CAAC,CAAC,IAAI,YAAY,MAAM,CAAC;QACzB,CAAC,CAAC,IAAI,YAAY,IAAI,CAAC,CACxB,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAg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Y;IAC1D,OAAO,aAAa,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE;QAC9C,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC;YACrB,OAAO,IAAA,eAAI,EAAC,GAAG,IAAI,iBAAiB,CAAC,CAAC;QACxC,CAAC;QACD,OAAO,IAAA,kBAAO,EAAC,CAAC,CAAC,CAAC;IACpB,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,YAAY,CAAC,IAAa;IACxC,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC/D,CAAC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Result, captureResult, fail, succeed } from '@fgv/ts-utils';\n\n/* eslint-disable no-use-before-define */\n\n/**\n * Primitive (terminal) values allowed in by JSON.\n * @public\n */\n// eslint-disable-next-line @rushstack/no-new-null\nexport type JsonPrimitive = boolean | number | string | null;\n\n/**\n * A {@link JsonObject | JsonObject} is a string-keyed object\n * containing only valid {@link JsonValue | JSON values}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface JsonObject {\n [key: string]: JsonValue;\n}\n\n/**\n * A {@link JsonValue | JsonValue} is one of: a {@link JsonPrimitive | JsonPrimitive},\n * a {@link JsonObject | JsonObject} or an {@link JsonArray | JsonArray}.\n * @public\n */\nexport type JsonValue = JsonPrimitive | JsonObject | JsonArray;\n\n/**\n * A {@link JsonArray | JsonArray} is an array containing only valid {@link JsonValue | JsonValues}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/naming-convention\nexport interface JsonArray extends Array<JsonValue> {}\n\n/**\n * Classes of {@link JsonValue | JsonValue}.\n * @public\n */\nexport type JsonValueType = 'primitive' | 'object' | 'array';\n\n/**\n * Test if an `unknown` is a {@link JsonValue | JsonValue}.\n * @param from - The `unknown` to be tested\n * @returns `true` if the supplied parameter is a valid {@link JsonPrimitive | JsonPrimitive},\n * `false` otherwise.\n * @public\n */\nexport function isJsonPrimitive(from: unknown): from is JsonPrimitive {\n return typeof from === 'boolean' || typeof from === 'number' || typeof from === 'string' || from === null;\n}\n\n/**\n * Test if an `unknown` is potentially a {@link JsonObject | JsonObject}.\n * @param from - The `unknown` to be tested.\n * @returns `true` if the supplied parameter is a non-array, non-special object,\n * `false` otherwise.\n * @public\n */\nexport function isJsonObject(from: unknown): from is JsonObject {\n return (\n typeof from === 'object' &&\n from !== null &&\n !Array.isArray(from) &&\n !(from instanceof RegExp) &&\n !(from instanceof Date)\n );\n}\n\n/**\n * Test if an `unknown` is potentially a {@link JsonArray | JsonArray}.\n * @param from - The `unknown` to be tested.\n * @returns `true` if the supplied parameter is an array object,\n * `false` otherwise.\n * @public\n */\nexport function isJsonArray(from: unknown): from is JsonArray {\n return typeof from === 'object' && Array.isArray(from);\n}\n\n/**\n * Identifies whether some `unknown` value is a {@link JsonPrimitive | primitive},\n * {@link JsonObject | object} or {@link JsonArray | array}. Fails for any value\n * that cannot be converted to JSON (e.g. a function) _but_ this is a shallow test -\n * it does not test the properties of an object or elements in an array.\n * @param from - The `unknown` value to be tested\n * @public\n */\nexport function classifyJsonValue(from: unknown): Result<JsonValueType> {\n if (isJsonPrimitive(from)) {\n return succeed('primitive');\n } else if (isJsonObject(from)) {\n return succeed('object');\n } else if (isJsonArray(from)) {\n return succeed('array');\n }\n return fail(`Invalid JSON: ${from}`);\n}\n\n/**\n * Picks a nested field from a supplied {@link JsonObject | JsonObject}.\n * @param src - The {@link JsonObject | object} from which the field is to be picked.\n * @param path - Dot-separated path of the member to be picked.\n * @returns `Success` with the property if the path is valid, `Failure`\n * with an error message otherwise.\n * @public\n */\nexport function pickJsonValue(src: JsonObject, path: string): Result<JsonValue> {\n let result: JsonValue = src;\n for (const part of path.split('.')) {\n if (result && isJsonObject(result)) {\n result = result[part];\n if (result === undefined) {\n return fail(`${path}: child '${part}' does not exist`);\n }\n } else {\n return fail(`${path}: child '${part}' does not exist`);\n }\n }\n return succeed(result);\n}\n\n/**\n * Picks a nested {@link JsonObject | JsonObject} from a supplied\n * {@link JsonObject | JsonObject}.\n * @param src - The {@link JsonObject | object} from which the field is\n * to be picked.\n * @param path - Dot-separated path of the member to be picked.\n * @returns `Success` with the property if the path is valid and the value\n * is an object. Returns `Failure` with details if an error occurs.\n * @public\n */\nexport function pickJsonObject(src: JsonObject, path: string): Result<JsonObject> {\n return pickJsonValue(src, path).onSuccess((v) => {\n if (!isJsonObject(v)) {\n return fail(`${path}: not an object`);\n }\n return succeed(v);\n });\n}\n\n/**\n * \"Sanitizes\" an `unknown` by stringifying and then parsing it. Guarantees a\n * valid {@link JsonValue | JsonValue} but is not idempotent and gives no guarantees\n * about fidelity. Fails if the supplied value cannot be stringified as Json.\n * @param from - The `unknown` to be sanitized.\n * @returns `Success` with a {@link JsonValue | JsonValue} if conversion succeeds,\n * `Failure` with details if an error occurs.\n * @public\n */\nexport function sanitizeJson(from: unknown): Result<JsonValue> {\n return captureResult(() => JSON.parse(JSON.stringify(from)));\n}\n"]}
1
+ {"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/packlets/json/common.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;AAkDH,0CAEC;AASD,oCAQC;AASD,kCAEC;AAUD,8CASC;AAUD,sCAaC;AAYD,wCAOC;AAWD,oCAEC;AAUD,gDAEC;AApKD,4CAAqE;AAyCrE;;;;;;GAMG;AACH,SAAgB,eAAe,CAAC,IAAa;IAC3C,OAAO,OAAO,IAAI,KAAK,SAAS,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,CAAC;AAC5G,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,YAAY,CAAC,IAAa;IACxC,OAAO,CACL,OAAO,IAAI,KAAK,QAAQ;QACxB,IAAI,KAAK,IAAI;QACb,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QACpB,CAAC,CAAC,IAAI,YAAY,MAAM,CAAC;QACzB,CAAC,CAAC,IAAI,YAAY,IAAI,CAAC,CACxB,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,WAAW,CAAC,IAAa;IACvC,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;AACzD,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,iBAAiB,CAAC,IAAa;IAC7C,IAAI,eAAe,CAAC,IAAI,CAAC,EAAE,CAAC;QAC1B,OAAO,IAAA,kBAAO,EAAC,WAAW,CAAC,CAAC;IAC9B,CAAC;SAAM,IAAI,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC;QAC9B,OAAO,IAAA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C,IAAa;IACxC,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC/D,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,kBAAkB,CAAI,IAAO;IAC3C,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC/D,CAAC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Result, captureResult, fail, succeed } from '@fgv/ts-utils';\n\n/* eslint-disable no-use-before-define */\n\n/**\n * Primitive (terminal) values allowed in by JSON.\n * @public\n */\n// eslint-disable-next-line @rushstack/no-new-null\nexport type JsonPrimitive = boolean | number | string | null;\n\n/**\n * A {@link JsonObject | JsonObject} is a string-keyed object\n * containing only valid {@link JsonValue | JSON values}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface JsonObject {\n [key: string]: JsonValue;\n}\n\n/**\n * A {@link JsonValue | JsonValue} is one of: a {@link JsonPrimitive | JsonPrimitive},\n * a {@link JsonObject | JsonObject} or an {@link JsonArray | JsonArray}.\n * @public\n */\nexport type JsonValue = JsonPrimitive | JsonObject | JsonArray;\n\n/**\n * A {@link JsonArray | JsonArray} is an array containing only valid {@link JsonValue | JsonValues}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/naming-convention\nexport interface JsonArray extends Array<JsonValue> {}\n\n/**\n * Classes of {@link JsonValue | JsonValue}.\n * @public\n */\nexport type JsonValueType = 'primitive' | 'object' | 'array';\n\n/**\n * Test if an `unknown` is a {@link JsonValue | JsonValue}.\n * @param from - The `unknown` to be tested\n * @returns `true` if the supplied parameter is a valid {@link JsonPrimitive | JsonPrimitive},\n * `false` otherwise.\n * @public\n */\nexport function isJsonPrimitive(from: unknown): from is JsonPrimitive {\n return typeof from === 'boolean' || typeof from === 'number' || typeof from === 'string' || from === null;\n}\n\n/**\n * Test if an `unknown` is potentially a {@link JsonObject | JsonObject}.\n * @param from - The `unknown` to be tested.\n * @returns `true` if the supplied parameter is a non-array, non-special object,\n * `false` otherwise.\n * @public\n */\nexport function isJsonObject(from: unknown): from is JsonObject {\n return (\n typeof from === 'object' &&\n from !== null &&\n !Array.isArray(from) &&\n !(from instanceof RegExp) &&\n !(from instanceof Date)\n );\n}\n\n/**\n * Test if an `unknown` is potentially a {@link JsonArray | JsonArray}.\n * @param from - The `unknown` to be tested.\n * @returns `true` if the supplied parameter is an array object,\n * `false` otherwise.\n * @public\n */\nexport function isJsonArray(from: unknown): from is JsonArray {\n return typeof from === 'object' && Array.isArray(from);\n}\n\n/**\n * Identifies whether some `unknown` value is a {@link JsonPrimitive | primitive},\n * {@link JsonObject | object} or {@link JsonArray | array}. Fails for any value\n * that cannot be converted to JSON (e.g. a function) _but_ this is a shallow test -\n * it does not test the properties of an object or elements in an array.\n * @param from - The `unknown` value to be tested\n * @public\n */\nexport function classifyJsonValue(from: unknown): Result<JsonValueType> {\n if (isJsonPrimitive(from)) {\n return succeed('primitive');\n } else if (isJsonObject(from)) {\n return succeed('object');\n } else if (isJsonArray(from)) {\n return succeed('array');\n }\n return fail(`Invalid JSON: ${from}`);\n}\n\n/**\n * Picks a nested field from a supplied {@link JsonObject | JsonObject}.\n * @param src - The {@link JsonObject | object} from which the field is to be picked.\n * @param path - Dot-separated path of the member to be picked.\n * @returns `Success` with the property if the path is valid, `Failure`\n * with an error message otherwise.\n * @public\n */\nexport function pickJsonValue(src: JsonObject, path: string): Result<JsonValue> {\n let result: JsonValue = src;\n for (const part of path.split('.')) {\n if (result && isJsonObject(result)) {\n result = result[part];\n if (result === undefined) {\n return fail(`${path}: child '${part}' does not exist`);\n }\n } else {\n return fail(`${path}: child '${part}' does not exist`);\n }\n }\n return succeed(result);\n}\n\n/**\n * Picks a nested {@link JsonObject | JsonObject} from a supplied\n * {@link JsonObject | JsonObject}.\n * @param src - The {@link JsonObject | object} from which the field is\n * to be picked.\n * @param path - Dot-separated path of the member to be picked.\n * @returns `Success` with the property if the path is valid and the value\n * is an object. Returns `Failure` with details if an error occurs.\n * @public\n */\nexport function pickJsonObject(src: JsonObject, path: string): Result<JsonObject> {\n return pickJsonValue(src, path).onSuccess((v) => {\n if (!isJsonObject(v)) {\n return fail(`${path}: not an object`);\n }\n return succeed(v);\n });\n}\n\n/**\n * \"Sanitizes\" an `unknown` by stringifying and then parsing it. Guarantees a\n * valid {@link JsonValue | JsonValue} but is not idempotent and gives no guarantees\n * about fidelity. Fails if the supplied value cannot be stringified as Json.\n * @param from - The `unknown` to be sanitized.\n * @returns `Success` with a {@link JsonValue | JsonValue} if conversion succeeds,\n * `Failure` with details if an error occurs.\n * @public\n */\nexport function sanitizeJson(from: unknown): Result<JsonValue> {\n return captureResult(() => JSON.parse(JSON.stringify(from)));\n}\n\n/**\n * Sanitizes some value using JSON stringification and parsing, returning an\n * returning a matching strongly-typed value.\n * @param from - The value to be sanitized.\n * @returns `Success` with a {@link JsonObject | JsonObject} if conversion succeeds,\n * `Failure` with details if an error occurs.\n * @public\n */\nexport function sanitizeJsonObject<T>(from: T): Result<T> {\n return captureResult(() => JSON.parse(JSON.stringify(from)));\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-json-base",
3
- "version": "4.5.0-0",
3
+ "version": "4.5.0-1",
4
4
  "description": "Typescript types and basic functions for working with json",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-json-base.d.ts",
@@ -16,8 +16,8 @@
16
16
  "homepage": "https://github.com/ErikFortune/fgv/tree/main/libraries/ts-json-base#readme",
17
17
  "sideEffects": false,
18
18
  "devDependencies": {
19
- "@fgv/ts-utils": "4.5.0-0",
20
- "@fgv/ts-utils-jest": "4.5.0-0",
19
+ "@fgv/ts-utils": "4.5.0-1",
20
+ "@fgv/ts-utils-jest": "4.5.0-1",
21
21
  "@types/jest": "^29.5.14",
22
22
  "@types/node": "^22.13.11",
23
23
  "@typescript-eslint/eslint-plugin": "^8.27.0",
@@ -38,10 +38,10 @@
38
38
  "heft-jest": "~1.0.2",
39
39
  "@types/heft-jest": "1.0.6",
40
40
  "@microsoft/api-documenter": "^7.26.17",
41
- "@fgv/ts-extras": "4.5.0-0"
41
+ "@fgv/ts-extras": "4.5.0-1"
42
42
  },
43
43
  "peerDependencies": {
44
- "@fgv/ts-utils": "4.5.0-0"
44
+ "@fgv/ts-utils": "4.5.0-1"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "heft test --clean",