@fgv/ts-json-base 4.0.2 → 4.2.0

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.
package/CHANGELOG.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
2
  "name": "@fgv/ts-json-base",
3
3
  "entries": [
4
+ {
5
+ "version": "4.2.0",
6
+ "tag": "@fgv/ts-json-base_v4.2.0",
7
+ "date": "Mon, 20 Jan 2025 09:46:53 GMT",
8
+ "comments": {
9
+ "none": [
10
+ {
11
+ "comment": "plumb conversion context through"
12
+ }
13
+ ]
14
+ }
15
+ },
16
+ {
17
+ "version": "4.1.0",
18
+ "tag": "@fgv/ts-json-base_v4.1.0",
19
+ "date": "Thu, 09 Jan 2025 05:33:39 GMT",
20
+ "comments": {
21
+ "none": [
22
+ {
23
+ "comment": "update dependencies"
24
+ }
25
+ ]
26
+ }
27
+ },
28
+ {
29
+ "version": "4.0.2",
30
+ "tag": "@fgv/ts-json-base_v4.0.2",
31
+ "date": "Tue, 14 May 2024 14:45:53 GMT",
32
+ "comments": {}
33
+ },
4
34
  {
5
35
  "version": "4.0.1",
6
36
  "tag": "@fgv/ts-json-base_v4.0.1",
package/CHANGELOG.md CHANGED
@@ -1,6 +1,25 @@
1
1
  # Change Log - @fgv/ts-json-base
2
2
 
3
- This log was last generated on Tue, 14 May 2024 05:02:20 GMT and should not be manually modified.
3
+ This log was last generated on Mon, 20 Jan 2025 09:46:53 GMT and should not be manually modified.
4
+
5
+ ## 4.2.0
6
+ Mon, 20 Jan 2025 09:46:53 GMT
7
+
8
+ ### Updates
9
+
10
+ - plumb conversion context through
11
+
12
+ ## 4.1.0
13
+ Thu, 09 Jan 2025 05:33:39 GMT
14
+
15
+ ### Updates
16
+
17
+ - update dependencies
18
+
19
+ ## 4.0.2
20
+ Tue, 14 May 2024 14:45:53 GMT
21
+
22
+ _Version update only_
4
23
 
5
24
  ## 4.0.1
6
25
  Tue, 14 May 2024 05:02:20 GMT
@@ -30,7 +30,7 @@ export { Converters }
30
30
  * conversion and filtering
31
31
  * @public
32
32
  */
33
- declare function convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
33
+ declare function convertJsonDirectorySync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryOptions<T, TC>): Result<IReadDirectoryItem<T>[]>;
34
34
 
35
35
  /**
36
36
  * Reads and converts all JSON files from a directory, returning a
@@ -50,7 +50,7 @@ declare function convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string,
50
50
  * @returns `Success` with a result of type `<T>`, or `Failure`* with a message if an error occurs.
51
51
  * @public
52
52
  */
53
- declare function convertJsonFileSync<T>(srcPath: string, converter: Converter<T>): Result<T>;
53
+ declare function convertJsonFileSync<T, TC = unknown>(srcPath: string, converter: Converter<T, TC>): Result<T>;
54
54
 
55
55
  /**
56
56
  * @public
@@ -258,14 +258,14 @@ declare class JsonFsHelper {
258
258
  * @returns `Success` with a result of type `<T>`, or `Failure`
259
259
  * with a message if an error occurs.
260
260
  */
261
- convertJsonFileSync<T, TC = unknown>(srcPath: string, cv: Converter<T, TC> | Validator<T, TC>): Result<T>;
261
+ convertJsonFileSync<T, TC = unknown>(srcPath: string, cv: Converter<T, TC> | Validator<T, TC>, context?: TC): Result<T>;
262
262
  /**
263
263
  * Reads all JSON files from a directory and apply a supplied converter or validator.
264
264
  * @param srcPath - The path of the folder to be read.
265
265
  * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
266
266
  * conversion and filtering
267
267
  */
268
- convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
268
+ convertJsonDirectorySync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryOptions<T>, context?: TC): Result<IReadDirectoryItem<T>[]>;
269
269
  /**
270
270
  * Reads and converts or validates all JSON files from a directory, returning a
271
271
  * `Map<string, T>` indexed by file base name (i.e. minus the extension)
@@ -274,7 +274,7 @@ declare class JsonFsHelper {
274
274
  * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
275
275
  * filtering and naming.
276
276
  */
277
- convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>;
277
+ convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>, context?: unknown): Result<Map<string, T>>;
278
278
  /**
279
279
  * Write type-safe JSON to a file.
280
280
  * @param srcPath - Path of the file to write.
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.43.4"
8
+ "packageVersion": "7.49.1"
9
9
  }
10
10
  ]
11
11
  }
package/lib/index.js CHANGED
@@ -36,13 +36,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
36
36
  }) : function(o, v) {
37
37
  o["default"] = v;
38
38
  });
39
- var __importStar = (this && this.__importStar) || function (mod) {
40
- if (mod && mod.__esModule) return mod;
41
- var result = {};
42
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
43
- __setModuleDefault(result, mod);
44
- return result;
45
- };
39
+ var __importStar = (this && this.__importStar) || (function () {
40
+ var ownKeys = function(o) {
41
+ ownKeys = Object.getOwnPropertyNames || function (o) {
42
+ var ar = [];
43
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
44
+ return ar;
45
+ };
46
+ return ownKeys(o);
47
+ };
48
+ return function (mod) {
49
+ if (mod && mod.__esModule) return mod;
50
+ var result = {};
51
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
52
+ __setModuleDefault(result, mod);
53
+ return result;
54
+ };
55
+ })();
46
56
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
47
57
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
48
58
  };
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,kEAAoD;AAK3C,gCAAU;AAJnB,+DAAiD;AAI5B,4BAAQ;AAH7B,kEAAoD;AAGrB,gCAAU;AADzC,kDAAgC","sourcesContent":["/*\n * Copyright (c) 2023 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 * as Converters from './packlets/converters';\nimport * as JsonFile from './packlets/json-file';\nimport * as Validators from './packlets/validators';\n\nexport * from './packlets/json';\nexport { Converters, JsonFile, Validators };\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,kEAAoD;AAK3C,gCAAU;AAJnB,+DAAiD;AAI5B,4BAAQ;AAH7B,kEAAoD;AAGrB,gCAAU;AADzC,kDAAgC","sourcesContent":["/*\n * Copyright (c) 2023 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 * as Converters from './packlets/converters';\nimport * as JsonFile from './packlets/json-file';\nimport * as Validators from './packlets/validators';\n\nexport * from './packlets/json';\nexport { Converters, JsonFile, Validators };\n"]}
@@ -21,7 +21,13 @@
21
21
  * SOFTWARE.
22
22
  */
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
- exports.sanitizeJson = exports.pickJsonObject = exports.pickJsonValue = exports.classifyJsonValue = exports.isJsonArray = exports.isJsonObject = exports.isJsonPrimitive = void 0;
24
+ exports.isJsonPrimitive = isJsonPrimitive;
25
+ exports.isJsonObject = isJsonObject;
26
+ exports.isJsonArray = isJsonArray;
27
+ exports.classifyJsonValue = classifyJsonValue;
28
+ exports.pickJsonValue = pickJsonValue;
29
+ exports.pickJsonObject = pickJsonObject;
30
+ exports.sanitizeJson = sanitizeJson;
25
31
  const ts_utils_1 = require("@fgv/ts-utils");
26
32
  /**
27
33
  * Test if an `unknown` is a {@link JsonValue | JsonValue}.
@@ -33,7 +39,6 @@ const ts_utils_1 = require("@fgv/ts-utils");
33
39
  function isJsonPrimitive(from) {
34
40
  return typeof from === 'boolean' || typeof from === 'number' || typeof from === 'string' || from === null;
35
41
  }
36
- exports.isJsonPrimitive = isJsonPrimitive;
37
42
  /**
38
43
  * Test if an `unknown` is potentially a {@link JsonObject | JsonObject}.
39
44
  * @param from - The `unknown` to be tested.
@@ -48,7 +53,6 @@ function isJsonObject(from) {
48
53
  !(from instanceof RegExp) &&
49
54
  !(from instanceof Date));
50
55
  }
51
- exports.isJsonObject = isJsonObject;
52
56
  /**
53
57
  * Test if an `unknown` is potentially a {@link JsonArray | JsonArray}.
54
58
  * @param from - The `unknown` to be tested.
@@ -59,7 +63,6 @@ exports.isJsonObject = isJsonObject;
59
63
  function isJsonArray(from) {
60
64
  return typeof from === 'object' && Array.isArray(from);
61
65
  }
62
- exports.isJsonArray = isJsonArray;
63
66
  /**
64
67
  * Identifies whether some `unknown` value is a {@link JsonPrimitive | primitive},
65
68
  * {@link JsonObject | object} or {@link JsonArray | array}. Fails for any value
@@ -80,7 +83,6 @@ function classifyJsonValue(from) {
80
83
  }
81
84
  return (0, ts_utils_1.fail)(`Invalid JSON: ${from}`);
82
85
  }
83
- exports.classifyJsonValue = classifyJsonValue;
84
86
  /**
85
87
  * Picks a nested field from a supplied {@link JsonObject | JsonObject}.
86
88
  * @param src - The {@link JsonObject | object} from which the field is to be picked.
@@ -104,7 +106,6 @@ function pickJsonValue(src, path) {
104
106
  }
105
107
  return (0, ts_utils_1.succeed)(result);
106
108
  }
107
- exports.pickJsonValue = pickJsonValue;
108
109
  /**
109
110
  * Picks a nested {@link JsonObject | JsonObject} from a supplied
110
111
  * {@link JsonObject | JsonObject}.
@@ -123,7 +124,6 @@ function pickJsonObject(src, path) {
123
124
  return (0, ts_utils_1.succeed)(v);
124
125
  });
125
126
  }
126
- exports.pickJsonObject = pickJsonObject;
127
127
  /**
128
128
  * "Sanitizes" an `unknown` by stringifying and then parsing it. Guarantees a
129
129
  * valid {@link JsonValue | JsonValue} but is not idempotent and gives no guarantees
@@ -136,5 +136,4 @@ exports.pickJsonObject = pickJsonObject;
136
136
  function sanitizeJson(from) {
137
137
  return (0, ts_utils_1.captureResult)(() => JSON.parse(JSON.stringify(from)));
138
138
  }
139
- exports.sanitizeJson = sanitizeJson;
140
139
  //# sourceMappingURL=common.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/packlets/json/common.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,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;AAFD,0CAEC;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;AARD,oCAQC;AAED;;;;;;GAMG;AACH,SAAgB,WAAW,CAAC,IAAa;IACvC,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;AACzD,CAAC;AAFD,kCAEC;AAED;;;;;;;G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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;AAPD,wCAOC;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;AAFD,oCAEC","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;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,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,kBAAO,EAAC,QAAQ,CAAC,CAAC;IAC3B,CAAC;SAAM,IAAI,WAAW,CAAC,IAAI,CAAC,EAAE,CAA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,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"]}
@@ -13,7 +13,7 @@ export declare function readJsonFileSync(srcPath: string): Result<JsonValue>;
13
13
  * @returns `Success` with a result of type `<T>`, or `Failure`* with a message if an error occurs.
14
14
  * @public
15
15
  */
16
- export declare function convertJsonFileSync<T>(srcPath: string, converter: Converter<T>): Result<T>;
16
+ export declare function convertJsonFileSync<T, TC = unknown>(srcPath: string, converter: Converter<T, TC>): Result<T>;
17
17
  /**
18
18
  * Reads all JSON files from a directory and apply a supplied converter.
19
19
  * @param srcPath - The path of the folder to be read.
@@ -21,7 +21,7 @@ export declare function convertJsonFileSync<T>(srcPath: string, converter: Conve
21
21
  * conversion and filtering
22
22
  * @public
23
23
  */
24
- export declare function convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
24
+ export declare function convertJsonDirectorySync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryOptions<T, TC>): Result<IReadDirectoryItem<T>[]>;
25
25
  /**
26
26
  * Reads and converts all JSON files from a directory, returning a
27
27
  * `Map<string, T>` indexed by file base name (i.e. minus the extension)
@@ -1 +1 @@
1
- {"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../../src/packlets/json-file/file.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAEL,uBAAuB,EACvB,4BAA4B,EAC5B,kBAAkB,EAEnB,MAAM,gBAAgB,CAAC;AAGxB;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,CAEnE;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAE1F;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EACxC,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,uBAAuB,CAAC,CAAC,CAAC,GAClC,MAAM,CAAC,kBAAkB,CAAC,CAAC,CAAC,EAAE,CAAC,CAEjC;AAED;;;;;;;;GAQG;AACH,wBAAgB,6BAA6B,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO,EAC3D,OAAO,EAAE,MAAM,EACf,OA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C,CAExB;AAOD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAEpF"}
@@ -21,7 +21,11 @@
21
21
  * SOFTWARE.
22
22
  */
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
- exports.writeJsonFileSync = exports.convertJsonDirectoryToMapSync = exports.convertJsonDirectorySync = exports.convertJsonFileSync = exports.readJsonFileSync = void 0;
24
+ exports.readJsonFileSync = readJsonFileSync;
25
+ exports.convertJsonFileSync = convertJsonFileSync;
26
+ exports.convertJsonDirectorySync = convertJsonDirectorySync;
27
+ exports.convertJsonDirectoryToMapSync = convertJsonDirectoryToMapSync;
28
+ exports.writeJsonFileSync = writeJsonFileSync;
25
29
  const jsonFsHelper_1 = require("./jsonFsHelper");
26
30
  const jsonLike_1 = require("./jsonLike");
27
31
  /**
@@ -31,7 +35,6 @@ const jsonLike_1 = require("./jsonLike");
31
35
  function readJsonFileSync(srcPath) {
32
36
  return jsonFsHelper_1.DefaultJsonFsHelper.readJsonFileSync(srcPath);
33
37
  }
34
- exports.readJsonFileSync = readJsonFileSync;
35
38
  /**
36
39
  * Read a JSON file and apply a supplied converter.
37
40
  * @param srcPath - Path of the file to read.
@@ -42,7 +45,6 @@ exports.readJsonFileSync = readJsonFileSync;
42
45
  function convertJsonFileSync(srcPath, converter) {
43
46
  return jsonFsHelper_1.DefaultJsonFsHelper.convertJsonFileSync(srcPath, converter);
44
47
  }
45
- exports.convertJsonFileSync = convertJsonFileSync;
46
48
  /**
47
49
  * Reads all JSON files from a directory and apply a supplied converter.
48
50
  * @param srcPath - The path of the folder to be read.
@@ -53,7 +55,6 @@ exports.convertJsonFileSync = convertJsonFileSync;
53
55
  function convertJsonDirectorySync(srcPath, options) {
54
56
  return jsonFsHelper_1.DefaultJsonFsHelper.convertJsonDirectorySync(srcPath, options);
55
57
  }
56
- exports.convertJsonDirectorySync = convertJsonDirectorySync;
57
58
  /**
58
59
  * Reads and converts all JSON files from a directory, returning a
59
60
  * `Map<string, T>` indexed by file base name (i.e. minus the extension)
@@ -66,7 +67,6 @@ exports.convertJsonDirectorySync = convertJsonDirectorySync;
66
67
  function convertJsonDirectoryToMapSync(srcPath, options) {
67
68
  return jsonFsHelper_1.DefaultJsonFsHelper.convertJsonDirectoryToMapSync(srcPath, options);
68
69
  }
69
- exports.convertJsonDirectoryToMapSync = convertJsonDirectoryToMapSync;
70
70
  const CompatJsonFsHelper = new jsonFsHelper_1.JsonFsHelper({
71
71
  json: jsonLike_1.DefaultJsonLike,
72
72
  allowUndefinedWrite: true // for compatibility
@@ -78,5 +78,4 @@ const CompatJsonFsHelper = new jsonFsHelper_1.JsonFsHelper({
78
78
  function writeJsonFileSync(srcPath, value) {
79
79
  return CompatJsonFsHelper.writeJsonFileSync(srcPath, value);
80
80
  }
81
- exports.writeJsonFileSync = writeJsonFileSync;
82
81
  //# sourceMappingURL=file.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"file.js","sourceRoot":"","sources":["../../../src/packlets/json-file/file.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAIH,iDAMwB;AACxB,yCAA6C;AAE7C;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,OAAe;IAC9C,OAAO,kCAAmB,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;AACvD,CAAC;AAFD,4CAEC;AAED;;;;;;GAMG;AACH,SAAgB,mBAAmB,CAAI,OAAe,EAAE,SAAuB;IAC7E,OAAO,kCAAmB,CAAC,mBAAmB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;AACrE,CAAC;AAFD,kDAEC;AAED;;;;;;GAMG;AACH,SAAgB,wBAAwB,CACtC,OAAe,EACf,OAAmC;IAEnC,OAAO,kCAAmB,CAAC,wBAAwB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AACxE,CAAC;AALD,4DAKC;AAED;;;;;;;;GAQG;AACH,SAAgB,6BAA6B,CAC3C,OAAe,EACf,OAA4C;IAE5C,OAAO,kCAAmB,CAAC,6BAA6B,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAC7E,CAAC;AALD,sEAKC;AAED,MAAM,kBAAkB,GAAiB,IAAI,2BAAY,CAAC;IACxD,IAAI,EAAE,0BAAe;IACrB,mBAAmB,EAAE,IAAI,CAAC,oBAAoB;CAC/C,CAAC,CAAC;AAEH;;;GAGG;AACH,SAAgB,iBAAiB,CAAC,OAAe,EAAE,KAAgB;IACjE,OAAO,kBAAkB,CAAC,iBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AAC9D,CAAC;AAFD,8CAEC","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 { Converter, Result } from '@fgv/ts-utils';\nimport { JsonValue } from '../json';\nimport {\n DefaultJsonFsHelper,\n IJsonFsDirectoryOptions,\n IJsonFsDirectoryToMapOptions,\n IReadDirectoryItem,\n JsonFsHelper\n} from './jsonFsHelper';\nimport { DefaultJsonLike } from './jsonLike';\n\n/**\n * {@inheritdoc JsonFile.JsonFsHelper.readJsonFileSync}\n * @public\n */\nexport function readJsonFileSync(srcPath: string): Result<JsonValue> {\n return DefaultJsonFsHelper.readJsonFileSync(srcPath);\n}\n\n/**\n * Read a JSON file and apply a supplied converter.\n * @param srcPath - Path of the file to read.\n * @param converter - `Converter` used to convert the file contents\n * @returns `Success` with a result of type `<T>`, or `Failure`* with a message if an error occurs.\n * @public\n */\nexport function convertJsonFileSync<T>(srcPath: string, converter: Converter<T>): Result<T> {\n return DefaultJsonFsHelper.convertJsonFileSync(srcPath, converter);\n}\n\n/**\n * Reads all JSON files from a directory and apply a supplied converter.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control\n * conversion and filtering\n * @public\n */\nexport function convertJsonDirectorySync<T>(\n srcPath: string,\n options: IJsonFsDirectoryOptions<T>\n): Result<IReadDirectoryItem<T>[]> {\n return DefaultJsonFsHelper.convertJsonDirectorySync(srcPath, options);\n}\n\n/**\n * Reads and converts all JSON files from a directory, returning a\n * `Map<string, T>` indexed by file base name (i.e. minus the extension)\n * with an optional name transformation applied if present.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,\n * filtering and naming.\n * @public\n */\nexport function convertJsonDirectoryToMapSync<T, TC = unknown>(\n srcPath: string,\n options: IJsonFsDirectoryToMapOptions<T, TC>\n): Result<Map<string, T>> {\n return DefaultJsonFsHelper.convertJsonDirectoryToMapSync(srcPath, options);\n}\n\nconst CompatJsonFsHelper: JsonFsHelper = new JsonFsHelper({\n json: DefaultJsonLike,\n allowUndefinedWrite: true // for compatibility\n});\n\n/**\n * {@inheritDoc JsonFile.JsonFsHelper.writeJsonFileSync}\n * @public\n */\nexport function writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean> {\n return CompatJsonFsHelper.writeJsonFileSync(srcPath, value);\n}\n"]}
1
+ {"version":3,"file":"file.js","sourceRoot":"","sources":["../../../src/packlets/json-file/file.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;AAiBH,4CAEC;AASD,kDAKC;AASD,4DAKC;AAWD,sEAKC;AAWD,8CAEC;AAxED,iDAMwB;AACxB,yCAA6C;AAE7C;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,OAAe;IAC9C,OAAO,kCAAmB,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;AACvD,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,mBAAmB,CACjC,OAAe,EACf,SAA2B;IAE3B,OAAO,kCAAmB,CAAC,mBAAmB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;AACrE,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,wBAAwB,CACtC,OAAe,EACf,OAAuC;IAEvC,OAAO,kCAAmB,CAAC,wBAAwB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AACxE,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,6BAA6B,CAC3C,OAAe,EACf,OAA4C;IAE5C,OAAO,kCAAmB,CAAC,6BAA6B,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAC7E,CAAC;AAED,MAAM,kBAAkB,GAAiB,IAAI,2BAAY,CAAC;IACxD,IAAI,EAAE,0BAAe;IACrB,mBAAmB,EAAE,IAAI,CAAC,oBAAoB;CAC/C,CAAC,CAAC;AAEH;;;GAGG;AACH,SAAgB,iBAAiB,CAAC,OAAe,EAAE,KAAgB;IACjE,OAAO,kBAAkB,CAAC,iBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AAC9D,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 { Converter, Result } from '@fgv/ts-utils';\nimport { JsonValue } from '../json';\nimport {\n DefaultJsonFsHelper,\n IJsonFsDirectoryOptions,\n IJsonFsDirectoryToMapOptions,\n IReadDirectoryItem,\n JsonFsHelper\n} from './jsonFsHelper';\nimport { DefaultJsonLike } from './jsonLike';\n\n/**\n * {@inheritdoc JsonFile.JsonFsHelper.readJsonFileSync}\n * @public\n */\nexport function readJsonFileSync(srcPath: string): Result<JsonValue> {\n return DefaultJsonFsHelper.readJsonFileSync(srcPath);\n}\n\n/**\n * Read a JSON file and apply a supplied converter.\n * @param srcPath - Path of the file to read.\n * @param converter - `Converter` used to convert the file contents\n * @returns `Success` with a result of type `<T>`, or `Failure`* with a message if an error occurs.\n * @public\n */\nexport function convertJsonFileSync<T, TC = unknown>(\n srcPath: string,\n converter: Converter<T, TC>\n): Result<T> {\n return DefaultJsonFsHelper.convertJsonFileSync(srcPath, converter);\n}\n\n/**\n * Reads all JSON files from a directory and apply a supplied converter.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control\n * conversion and filtering\n * @public\n */\nexport function convertJsonDirectorySync<T, TC = unknown>(\n srcPath: string,\n options: IJsonFsDirectoryOptions<T, TC>\n): Result<IReadDirectoryItem<T>[]> {\n return DefaultJsonFsHelper.convertJsonDirectorySync(srcPath, options);\n}\n\n/**\n * Reads and converts all JSON files from a directory, returning a\n * `Map<string, T>` indexed by file base name (i.e. minus the extension)\n * with an optional name transformation applied if present.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,\n * filtering and naming.\n * @public\n */\nexport function convertJsonDirectoryToMapSync<T, TC = unknown>(\n srcPath: string,\n options: IJsonFsDirectoryToMapOptions<T, TC>\n): Result<Map<string, T>> {\n return DefaultJsonFsHelper.convertJsonDirectoryToMapSync(srcPath, options);\n}\n\nconst CompatJsonFsHelper: JsonFsHelper = new JsonFsHelper({\n json: DefaultJsonLike,\n allowUndefinedWrite: true // for compatibility\n});\n\n/**\n * {@inheritDoc JsonFile.JsonFsHelper.writeJsonFileSync}\n * @public\n */\nexport function writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean> {\n return CompatJsonFsHelper.writeJsonFileSync(srcPath, value);\n}\n"]}
@@ -92,14 +92,14 @@ export declare class JsonFsHelper {
92
92
  * @returns `Success` with a result of type `<T>`, or `Failure`
93
93
  * with a message if an error occurs.
94
94
  */
95
- convertJsonFileSync<T, TC = unknown>(srcPath: string, cv: Converter<T, TC> | Validator<T, TC>): Result<T>;
95
+ convertJsonFileSync<T, TC = unknown>(srcPath: string, cv: Converter<T, TC> | Validator<T, TC>, context?: TC): Result<T>;
96
96
  /**
97
97
  * Reads all JSON files from a directory and apply a supplied converter or validator.
98
98
  * @param srcPath - The path of the folder to be read.
99
99
  * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
100
100
  * conversion and filtering
101
101
  */
102
- convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
102
+ convertJsonDirectorySync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryOptions<T>, context?: TC): Result<IReadDirectoryItem<T>[]>;
103
103
  /**
104
104
  * Reads and converts or validates all JSON files from a directory, returning a
105
105
  * `Map<string, T>` indexed by file base name (i.e. minus the extension)
@@ -108,7 +108,7 @@ export declare class JsonFsHelper {
108
108
  * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
109
109
  * filtering and naming.
110
110
  */
111
- convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>;
111
+ convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>, context?: unknown): Result<Map<string, T>>;
112
112
  /**
113
113
  * Write type-safe JSON to a file.
114
114
  * @param srcPath - Path of the file to write.
@@ -1 +1 @@
1
- {"version":3,"file":"jsonFsHelper.d.ts","sourceRoot":"","sources":["../../../src/packlets/json-file/jsonFsHelper.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAA4C,MAAM,eAAe,CAAC;AAIvG,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAmB,SAAS,EAAE,MAAM,YAAY,CAAC;AAExD;;;;GAIG;AACH,MAAM,WAAW,uBAAuB,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO;IACtD;;OAEG;IACH,SAAS,EAAE,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAE/C;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;CAClB;AAED;;;GAGG;AACH,MAAM,WAAW,kBAAkB,CAAC,CAAC;IACnC;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,IAAI,EAAE,CAAC,CAAC;CACT;AAED;;;;GAIG;AACH,MAAM,MAAM,yBAAy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C;;;;;;;OAOG;IACI,6BAA6B,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO,EAClD,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,4BAA4B,CAAC,CAAC,EAAE,EAAE,CAAC,EAC5C,OAAO,CAAC,EAAE,OAAO,GAChB,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAgBzB;;;;OAIG;IACI,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC;IAa5E,SAAS,CAAC,mBAAmB,CAAC,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,uBAAuB,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO;CAKrG;AAED;;GAEG;AACH,eAAO,MAAM,mBAAmB,EAAE,YAAiC,CAAC"}
@@ -36,13 +36,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
36
36
  }) : function(o, v) {
37
37
  o["default"] = v;
38
38
  });
39
- var __importStar = (this && this.__importStar) || function (mod) {
40
- if (mod && mod.__esModule) return mod;
41
- var result = {};
42
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
43
- __setModuleDefault(result, mod);
44
- return result;
45
- };
39
+ var __importStar = (this && this.__importStar) || (function () {
40
+ var ownKeys = function(o) {
41
+ ownKeys = Object.getOwnPropertyNames || function (o) {
42
+ var ar = [];
43
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
44
+ return ar;
45
+ };
46
+ return ownKeys(o);
47
+ };
48
+ return function (mod) {
49
+ if (mod && mod.__esModule) return mod;
50
+ var result = {};
51
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
52
+ __setModuleDefault(result, mod);
53
+ return result;
54
+ };
55
+ })();
46
56
  Object.defineProperty(exports, "__esModule", { value: true });
47
57
  exports.DefaultJsonFsHelper = exports.JsonFsHelper = exports.DefaultJsonFsHelperConfig = void 0;
48
58
  const ts_utils_1 = require("@fgv/ts-utils");
@@ -100,9 +110,9 @@ class JsonFsHelper {
100
110
  * @returns `Success` with a result of type `<T>`, or `Failure`
101
111
  * with a message if an error occurs.
102
112
  */
103
- convertJsonFileSync(srcPath, cv) {
113
+ convertJsonFileSync(srcPath, cv, context) {
104
114
  return this.readJsonFileSync(srcPath).onSuccess((json) => {
105
- return cv.convert(json);
115
+ return cv.convert(json, context);
106
116
  });
107
117
  }
108
118
  /**
@@ -111,7 +121,7 @@ class JsonFsHelper {
111
121
  * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
112
122
  * conversion and filtering
113
123
  */
114
- convertJsonDirectorySync(srcPath, options) {
124
+ convertJsonDirectorySync(srcPath, options, context) {
115
125
  return (0, ts_utils_1.captureResult)(() => {
116
126
  const fullPath = path.resolve(srcPath);
117
127
  if (!fs.statSync(fullPath).isDirectory()) {
@@ -122,7 +132,7 @@ class JsonFsHelper {
122
132
  .map((fi) => {
123
133
  if (fi.isFile() && this._pathMatchesOptions(options, fi.name)) {
124
134
  const filePath = path.resolve(fullPath, fi.name);
125
- return this.convertJsonFileSync(filePath, options.converter)
135
+ return this.convertJsonFileSync(filePath, options.converter, context)
126
136
  .onSuccess((payload) => {
127
137
  return (0, ts_utils_1.succeed)({
128
138
  filename: fi.name,
@@ -147,8 +157,8 @@ class JsonFsHelper {
147
157
  * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
148
158
  * filtering and naming.
149
159
  */
150
- convertJsonDirectoryToMapSync(srcPath, options) {
151
- return this.convertJsonDirectorySync(srcPath, options).onSuccess((items) => {
160
+ convertJsonDirectoryToMapSync(srcPath, options, context) {
161
+ return this.convertJsonDirectorySync(srcPath, options, context).onSuccess((items) => {
152
162
  var _a;
153
163
  const transformName = (_a = options.transformName) !== null && _a !== void 0 ? _a : defaultNameTransformer;
154
164
  return (0, ts_utils_1.mapResults)(items.map((item) => {
@@ -170,6 +180,7 @@ class JsonFsHelper {
170
180
  return (0, ts_utils_1.captureResult)(() => {
171
181
  const fullPath = path.resolve(srcPath);
172
182
  const stringified = this.config.json.stringify(value, undefined, 2);
183
+ /* c8 ignore next 3 */
173
184
  if (stringified === undefined && this.config.allowUndefinedWrite !== true) {
174
185
  throw new Error(`Could not stringify ${value}`);
175
186
  }
@@ -179,6 +190,7 @@ class JsonFsHelper {
179
190
  }
180
191
  _pathMatchesOptions(options, path) {
181
192
  var _a;
193
+ /* c8 ignore next 1 */
182
194
  const match = (_a = options.files) !== null && _a !== void 0 ? _a : this.config.defaultFiles;
183
195
  return match.some((m) => m.exec(path));
184
196
  }
@@ -1 +1 @@
1
- {"version":3,"file":"jsonFsHelper.js","sourceRoot":"","sources":["../../../src/packlets/json-file/jsonFsHelper.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,4CAAuG;AACvG,uCAAyB;AACzB,2CAA6B;AAG7B,yCAAwD;AAkDxD;;;;;;GAMG;AACH,MAAM,sBAAsB,GAAG,CAAC,CAAS,EAAkB,EAAE,CAAC,IAAA,kBAAO,EAAC,CAAC,CAAC,CAAC;AAkBzE;;;GAGG;AACU,QAAA,yBAAyB,GAAwB;IAC5D,IAAI,EAAE,0BAAe;IACrB,mBAAmB,EAAE,KAAK;IAC1B,YAAY,EAAE,CAAC,SAAS,CAAC;CAC1B,CAAC;AAEF;;;;GAIG;AACH,MAAa,YAAY;IAMvB;;;;OAIG;IACH,YAAmB,IAA8B;QAC/C,IAAI,CAAC,MAAM,mCACN,iCAAyB,GACzB,CAAC,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,EAAE,CAAC,CAChB,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACI,gBAAgB,CAAC,OAAe;QACrC,OAAO,IAAA,wBAA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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 { Converter, Result, Validator, captureResult, fail, mapResults, succeed } from '@fgv/ts-utils';\nimport * as fs from 'fs';\nimport * as path from 'path';\n\nimport { JsonValue } from '../json';\nimport { DefaultJsonLike, IJsonLike } from './jsonLike';\n\n/**\n * Options for directory conversion.\n * TODO: add filtering, allowed and excluded.\n * @public\n */\nexport interface IJsonFsDirectoryOptions<T, TC = unknown> {\n /**\n * The converter used to convert incoming JSON objects.\n */\n converter: Converter<T, TC> | Validator<T, TC>;\n\n /**\n * Filter applied to items in the directory\n */\n files?: RegExp[];\n}\n\n/**\n * Return value for one item in a directory conversion.\n * @public\n */\nexport interface IReadDirectoryItem<T> {\n /**\n * Relative name of the file that was processed\n */\n filename: string;\n\n /**\n * The payload of the file.\n */\n item: T;\n}\n\n/**\n * Function to transform the name of a some entity, given an original name\n * and the contents of the entity.\n * @public\n */\nexport type ItemNameTransformFunction<T> = (name: string, item: T) => Result<string>;\n\n/**\n * Options controlling conversion of a directory to a `Map`.\n * @public\n */\nexport interface IJsonFsDirectoryToMapOptions<T, TC = unknown> extends IJsonFsDirectoryOptions<T, TC> {\n transformName?: ItemNameTransformFunction<T>;\n}\n\n/**\n * Function to transform the name of some entity, given only a previous name. This\n * default implementation always returns `Success` with the value that was passed in.\n * @param n - The name to be transformed.\n * @returns - `Success` with the string that was passed in.\n * @public\n */\nconst defaultNameTransformer = (n: string): Result<string> => succeed(n);\n\n/**\n * Configuration for {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @public\n */\nexport interface IJsonFsHelperConfig {\n json: IJsonLike;\n allowUndefinedWrite: boolean;\n defaultFiles: RegExp[];\n}\n\n/**\n * Initialization options for {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @public\n */\nexport type JsonFsHelperInitOptions = Partial<IJsonFsHelperConfig>;\n\n/**\n * Default configuration for {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @public\n */\nexport const DefaultJsonFsHelperConfig: IJsonFsHelperConfig = {\n json: DefaultJsonLike,\n allowUndefinedWrite: false,\n defaultFiles: [/.*.json/]\n};\n\n/**\n * Helper class to simplify common filesystem operations involving JSON (or JSON-like)\n * files.\n * @public\n */\nexport class JsonFsHelper {\n /**\n * Configuration for this {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n */\n public readonly config: IJsonFsHelperConfig;\n\n /**\n * Construct a new {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @param json - Optional {@link JsonFile.IJsonLike | IJsonLike} used to process strings\n * and JSON values.\n */\n public constructor(init?: JsonFsHelperInitOptions) {\n this.config = {\n ...DefaultJsonFsHelperConfig,\n ...(init ?? {})\n };\n }\n\n /**\n * Read type-safe JSON from a file.\n * @param srcPath - Path of the file to read\n * @returns `Success` with a {@link JsonValue | JsonValue} or `Failure`\n * with a message if an error occurs.\n */\n public readJsonFileSync(srcPath: string): Result<JsonValue> {\n return captureResult(() => {\n const fullPath = path.resolve(srcPath);\n const body = fs.readFileSync(fullPath, 'utf8').toString();\n return this.config.json.parse(body) as JsonValue;\n });\n }\n\n /**\n * Read a JSON file and apply a supplied converter or validator.\n * @param srcPath - Path of the file to read.\n * @param cv - Converter or validator used to process the file.\n * @returns `Success` with a result of type `<T>`, or `Failure`\n * with a message if an error occurs.\n */\n public convertJsonFileSync<T, TC = unknown>(\n srcPath: string,\n cv: Converter<T, TC> | Validator<T, TC>\n ): Result<T> {\n return this.readJsonFileSync(srcPath).onSuccess((json) => {\n return cv.convert(json);\n });\n }\n\n /**\n * Reads all JSON files from a directory and apply a supplied converter or validator.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control\n * conversion and filtering\n */\n public convertJsonDirectorySync<T>(\n srcPath: string,\n options: IJsonFsDirectoryOptions<T>\n ): Result<IReadDirectoryItem<T>[]> {\n return captureResult<IReadDirectoryItem<T>[]>(() => {\n const fullPath = path.resolve(srcPath);\n if (!fs.statSync(fullPath).isDirectory()) {\n throw new Error(`${fullPath}: Not a directory`);\n }\n const files = fs.readdirSync(fullPath, { withFileTypes: true });\n const results = files\n .map((fi) => {\n if (fi.isFile() && this._pathMatchesOptions(options, fi.name)) {\n const filePath = path.resolve(fullPath, fi.name);\n return this.convertJsonFileSync(filePath, options.converter)\n .onSuccess((payload) => {\n return succeed({\n filename: fi.name,\n item: payload\n });\n })\n .onFailure((message) => {\n return fail(`${fi.name}: ${message}`);\n });\n }\n return undefined;\n })\n .filter((r): r is Result<IReadDirectoryItem<T>> => r !== undefined);\n return mapResults(results).orThrow();\n });\n }\n\n /**\n * Reads and converts or validates all JSON files from a directory, returning a\n * `Map<string, T>` indexed by file base name (i.e. minus the extension)\n * with an optional name transformation applied if present.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,\n * filtering and naming.\n */\n public convertJsonDirectoryToMapSync<T, TC = unknown>(\n srcPath: string,\n options: IJsonFsDirectoryToMapOptions<T, TC>\n ): Result<Map<string, T>> {\n return this.convertJsonDirectorySync(srcPath, options).onSuccess((items) => {\n const transformName = options.transformName ?? defaultNameTransformer;\n return mapResults(\n items.map((item) => {\n const basename = path.basename(item.filename, '.json');\n return transformName(basename, item.item).onSuccess((name) => {\n return succeed<[string, T]>([name, item.item]);\n });\n })\n ).onSuccess((items) => {\n return succeed(new Map<string, T>(items));\n });\n });\n }\n\n /**\n * Write type-safe JSON to a file.\n * @param srcPath - Path of the file to write.\n * @param value - The {@link JsonValue | JsonValue} to be written.\n */\n public writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean> {\n return captureResult(() => {\n const fullPath = path.resolve(srcPath);\n const stringified = this.config.json.stringify(value, undefined, 2);\n if (stringified === undefined && this.config.allowUndefinedWrite !== true) {\n throw new Error(`Could not stringify ${value}`);\n }\n fs.writeFileSync(fullPath, stringified!);\n return true;\n });\n }\n\n protected _pathMatchesOptions<T, TC>(options: IJsonFsDirectoryOptions<T, TC>, path: string): boolean {\n const match = options.files ?? this.config.defaultFiles;\n return match.some((m) => m.exec(path));\n }\n}\n\n/**\n * @public\n */\nexport const DefaultJsonFsHelper: JsonFsHelper = new JsonFsHelper();\n"]}
1
+ {"version":3,"file":"jsonFsHelper.js","sourceRoot":"","sources":["../../../src/packlets/json-file/jsonFsHelper.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,4CAAuG;AACvG,uCAAyB;AACzB,2CAA6B;AAG7B,yCAAwD;AAkDxD;;;;;;GAMG;AACH,MAAM,sBAAsB,GAAG,CAAC,CAAS,EAAkB,EAAE,CAAC,IAAA,kBAAO,EAAC,CAAC,CAAC,CAAC;AAkBzE;;;GAGG;AACU,QAAA,yBAAyB,GAAwB;IAC5D,IAAI,EAAE,0BAAe;IACrB,mBAAmB,EAAE,KAAK;IAC1B,YAAY,EAAE,CAAC,SAAS,CAAC;CAC1B,CAAC;AAEF;;;;GAIG;AACH,MAAa,YAAY;IAMvB;;;;OAIG;IACH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AC,MAAM,CAAC,YAAY,CAAC;QACxD,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACzC,CAAC;CACF;AA3ID,oCA2IC;AAED;;GAEG;AACU,QAAA,mBAAmB,GAAiB,IAAI,YAAY,EAAE,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 { Converter, Result, Validator, captureResult, fail, mapResults, succeed } from '@fgv/ts-utils';\nimport * as fs from 'fs';\nimport * as path from 'path';\n\nimport { JsonValue } from '../json';\nimport { DefaultJsonLike, IJsonLike } from './jsonLike';\n\n/**\n * Options for directory conversion.\n * TODO: add filtering, allowed and excluded.\n * @public\n */\nexport interface IJsonFsDirectoryOptions<T, TC = unknown> {\n /**\n * The converter used to convert incoming JSON objects.\n */\n converter: Converter<T, TC> | Validator<T, TC>;\n\n /**\n * Filter applied to items in the directory\n */\n files?: RegExp[];\n}\n\n/**\n * Return value for one item in a directory conversion.\n * @public\n */\nexport interface IReadDirectoryItem<T> {\n /**\n * Relative name of the file that was processed\n */\n filename: string;\n\n /**\n * The payload of the file.\n */\n item: T;\n}\n\n/**\n * Function to transform the name of a some entity, given an original name\n * and the contents of the entity.\n * @public\n */\nexport type ItemNameTransformFunction<T> = (name: string, item: T) => Result<string>;\n\n/**\n * Options controlling conversion of a directory to a `Map`.\n * @public\n */\nexport interface IJsonFsDirectoryToMapOptions<T, TC = unknown> extends IJsonFsDirectoryOptions<T, TC> {\n transformName?: ItemNameTransformFunction<T>;\n}\n\n/**\n * Function to transform the name of some entity, given only a previous name. This\n * default implementation always returns `Success` with the value that was passed in.\n * @param n - The name to be transformed.\n * @returns - `Success` with the string that was passed in.\n * @public\n */\nconst defaultNameTransformer = (n: string): Result<string> => succeed(n);\n\n/**\n * Configuration for {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @public\n */\nexport interface IJsonFsHelperConfig {\n json: IJsonLike;\n allowUndefinedWrite: boolean;\n defaultFiles: RegExp[];\n}\n\n/**\n * Initialization options for {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @public\n */\nexport type JsonFsHelperInitOptions = Partial<IJsonFsHelperConfig>;\n\n/**\n * Default configuration for {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @public\n */\nexport const DefaultJsonFsHelperConfig: IJsonFsHelperConfig = {\n json: DefaultJsonLike,\n allowUndefinedWrite: false,\n defaultFiles: [/.*.json/]\n};\n\n/**\n * Helper class to simplify common filesystem operations involving JSON (or JSON-like)\n * files.\n * @public\n */\nexport class JsonFsHelper {\n /**\n * Configuration for this {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n */\n public readonly config: IJsonFsHelperConfig;\n\n /**\n * Construct a new {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @param json - Optional {@link JsonFile.IJsonLike | IJsonLike} used to process strings\n * and JSON values.\n */\n public constructor(init?: JsonFsHelperInitOptions) {\n this.config = {\n ...DefaultJsonFsHelperConfig,\n ...(init ?? {})\n };\n }\n\n /**\n * Read type-safe JSON from a file.\n * @param srcPath - Path of the file to read\n * @returns `Success` with a {@link JsonValue | JsonValue} or `Failure`\n * with a message if an error occurs.\n */\n public readJsonFileSync(srcPath: string): Result<JsonValue> {\n return captureResult(() => {\n const fullPath = path.resolve(srcPath);\n const body = fs.readFileSync(fullPath, 'utf8').toString();\n return this.config.json.parse(body) as JsonValue;\n });\n }\n\n /**\n * Read a JSON file and apply a supplied converter or validator.\n * @param srcPath - Path of the file to read.\n * @param cv - Converter or validator used to process the file.\n * @returns `Success` with a result of type `<T>`, or `Failure`\n * with a message if an error occurs.\n */\n public convertJsonFileSync<T, TC = unknown>(\n srcPath: string,\n cv: Converter<T, TC> | Validator<T, TC>,\n context?: TC\n ): Result<T> {\n return this.readJsonFileSync(srcPath).onSuccess((json) => {\n return cv.convert(json, context);\n });\n }\n\n /**\n * Reads all JSON files from a directory and apply a supplied converter or validator.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control\n * conversion and filtering\n */\n public convertJsonDirectorySync<T, TC = unknown>(\n srcPath: string,\n options: IJsonFsDirectoryOptions<T>,\n context?: TC\n ): Result<IReadDirectoryItem<T>[]> {\n return captureResult<IReadDirectoryItem<T>[]>(() => {\n const fullPath = path.resolve(srcPath);\n if (!fs.statSync(fullPath).isDirectory()) {\n throw new Error(`${fullPath}: Not a directory`);\n }\n const files = fs.readdirSync(fullPath, { withFileTypes: true });\n const results = files\n .map((fi) => {\n if (fi.isFile() && this._pathMatchesOptions(options, fi.name)) {\n const filePath = path.resolve(fullPath, fi.name);\n return this.convertJsonFileSync(filePath, options.converter, context)\n .onSuccess((payload) => {\n return succeed({\n filename: fi.name,\n item: payload\n });\n })\n .onFailure((message) => {\n return fail(`${fi.name}: ${message}`);\n });\n }\n return undefined;\n })\n .filter((r): r is Result<IReadDirectoryItem<T>> => r !== undefined);\n return mapResults(results).orThrow();\n });\n }\n\n /**\n * Reads and converts or validates all JSON files from a directory, returning a\n * `Map<string, T>` indexed by file base name (i.e. minus the extension)\n * with an optional name transformation applied if present.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,\n * filtering and naming.\n */\n public convertJsonDirectoryToMapSync<T, TC = unknown>(\n srcPath: string,\n options: IJsonFsDirectoryToMapOptions<T, TC>,\n context?: unknown\n ): Result<Map<string, T>> {\n return this.convertJsonDirectorySync(srcPath, options, context).onSuccess((items) => {\n const transformName = options.transformName ?? defaultNameTransformer;\n return mapResults(\n items.map((item) => {\n const basename = path.basename(item.filename, '.json');\n return transformName(basename, item.item).onSuccess((name) => {\n return succeed<[string, T]>([name, item.item]);\n });\n })\n ).onSuccess((items) => {\n return succeed(new Map<string, T>(items));\n });\n });\n }\n\n /**\n * Write type-safe JSON to a file.\n * @param srcPath - Path of the file to write.\n * @param value - The {@link JsonValue | JsonValue} to be written.\n */\n public writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean> {\n return captureResult(() => {\n const fullPath = path.resolve(srcPath);\n const stringified = this.config.json.stringify(value, undefined, 2);\n /* c8 ignore next 3 */\n if (stringified === undefined && this.config.allowUndefinedWrite !== true) {\n throw new Error(`Could not stringify ${value}`);\n }\n fs.writeFileSync(fullPath, stringified!);\n return true;\n });\n }\n\n protected _pathMatchesOptions<T, TC>(options: IJsonFsDirectoryOptions<T, TC>, path: string): boolean {\n /* c8 ignore next 1 */\n const match = options.files ?? this.config.defaultFiles;\n return match.some((m) => m.exec(path));\n }\n}\n\n/**\n * @public\n */\nexport const DefaultJsonFsHelper: JsonFsHelper = new JsonFsHelper();\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-json-base",
3
- "version": "4.0.2",
3
+ "version": "4.2.0",
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,38 +16,37 @@
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.0.2",
20
- "@fgv/ts-utils-jest": "4.0.2",
21
- "@types/jest": "^29.5.12",
22
- "@types/node": "^20.12.11",
23
- "@typescript-eslint/eslint-plugin": "^7.9.0",
24
- "@typescript-eslint/parser": "^7.9.0",
19
+ "@fgv/ts-utils": "4.2.0",
20
+ "@fgv/ts-utils-jest": "4.2.0",
21
+ "@types/jest": "^29.5.14",
22
+ "@types/node": "^20.14.9",
23
+ "@typescript-eslint/eslint-plugin": "^7.14.1",
24
+ "@typescript-eslint/parser": "^7.14.1",
25
25
  "eslint": "^8.57.0",
26
- "eslint-config-standard": "^17.1.0",
27
- "eslint-plugin-import": "^2.29.1",
26
+ "eslint-plugin-import": "^2.31.0",
28
27
  "eslint-plugin-node": "^11.1.0",
29
- "eslint-plugin-promise": "^6.1.1",
28
+ "eslint-plugin-promise": "^6.2.0",
30
29
  "jest": "^29.7.0",
31
30
  "jest-extended": "^4.0.2",
32
31
  "rimraf": "^5.0.7",
33
- "ts-jest": "^29.1.2",
32
+ "ts-jest": "^29.2.5",
34
33
  "ts-node": "^10.9.2",
35
- "typescript": "^5.4.5",
34
+ "typescript": "^5.7.3",
36
35
  "eslint-plugin-n": "^16.6.2",
37
- "@rushstack/heft-node-rig": "~2.6.3",
38
- "@rushstack/heft": "~0.66.6",
36
+ "@rushstack/heft-node-rig": "~2.6.49",
37
+ "@rushstack/heft": "~0.68.13",
39
38
  "heft-jest": "~1.0.2",
40
39
  "@types/heft-jest": "1.0.6",
41
- "@microsoft/api-documenter": "^7.24.5",
42
- "@fgv/ts-extras": "4.0.2"
40
+ "@microsoft/api-documenter": "^7.26.5",
41
+ "@fgv/ts-extras": "4.2.0"
43
42
  },
44
43
  "peerDependencies": {
45
- "@fgv/ts-utils": "4.0.2"
44
+ "@fgv/ts-utils": "4.2.0"
46
45
  },
47
46
  "scripts": {
48
47
  "build": "heft test --clean",
49
48
  "clean": "heft clean",
50
- "test": "heft test",
49
+ "test": "heft test --clean",
51
50
  "build-docs": "api-documenter markdown --input-folder ./temp --output-folder docs",
52
51
  "build-all": "rushx build; rushx build-docs",
53
52
  "test-handles": "jest --runInBand --detectOpenHandles",