@fgv/ts-json 1.9.6 → 2.0.2-alpha.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.
Files changed (127) hide show
  1. package/dist/ts-json.d.ts +1826 -0
  2. package/dist/tsdoc-metadata.json +11 -0
  3. package/lib/index.d.ts +5 -0
  4. package/lib/index.d.ts.map +1 -0
  5. package/lib/index.js +42 -0
  6. package/lib/index.js.map +1 -0
  7. package/lib/packlets/context/compositeJsonMap.d.ts +60 -0
  8. package/lib/packlets/context/compositeJsonMap.d.ts.map +1 -0
  9. package/lib/packlets/context/compositeJsonMap.js +105 -0
  10. package/lib/packlets/context/compositeJsonMap.js.map +1 -0
  11. package/lib/packlets/context/contextHelpers.d.ts +116 -0
  12. package/lib/packlets/context/contextHelpers.d.ts.map +1 -0
  13. package/lib/packlets/context/contextHelpers.js +191 -0
  14. package/lib/packlets/context/contextHelpers.js.map +1 -0
  15. package/lib/packlets/context/index.d.ts +4 -0
  16. package/lib/packlets/context/index.d.ts.map +1 -0
  17. package/lib/packlets/context/index.js +31 -0
  18. package/lib/packlets/context/index.js.map +1 -0
  19. package/lib/packlets/context/jsonContext.d.ts +84 -0
  20. package/lib/packlets/context/jsonContext.d.ts.map +1 -0
  21. package/lib/packlets/context/jsonContext.js +41 -0
  22. package/lib/packlets/context/jsonContext.js.map +1 -0
  23. package/lib/packlets/converters/converters.d.ts +54 -0
  24. package/lib/packlets/converters/converters.d.ts.map +1 -0
  25. package/lib/packlets/converters/converters.js +105 -0
  26. package/lib/packlets/converters/converters.js.map +1 -0
  27. package/lib/packlets/converters/file.d.ts +83 -0
  28. package/lib/packlets/converters/file.d.ts.map +1 -0
  29. package/lib/packlets/converters/file.js +162 -0
  30. package/lib/packlets/converters/file.js.map +1 -0
  31. package/lib/packlets/converters/index.d.ts +5 -0
  32. package/lib/packlets/converters/index.d.ts.map +1 -0
  33. package/lib/packlets/converters/index.js +56 -0
  34. package/lib/packlets/converters/index.js.map +1 -0
  35. package/lib/packlets/converters/jsonConverter.d.ts +276 -0
  36. package/lib/packlets/converters/jsonConverter.d.ts.map +1 -0
  37. package/lib/packlets/converters/jsonConverter.js +310 -0
  38. package/lib/packlets/converters/jsonConverter.js.map +1 -0
  39. package/lib/packlets/editor/common.d.ts +67 -0
  40. package/lib/packlets/editor/common.d.ts.map +1 -0
  41. package/lib/packlets/editor/common.js +3 -0
  42. package/lib/packlets/editor/common.js.map +1 -0
  43. package/lib/packlets/editor/index.d.ts +8 -0
  44. package/lib/packlets/editor/index.d.ts.map +1 -0
  45. package/{jsonEditor → lib/packlets/editor}/index.js +15 -9
  46. package/lib/packlets/editor/index.js.map +1 -0
  47. package/lib/packlets/editor/jsonEditor.d.ts +155 -0
  48. package/lib/packlets/editor/jsonEditor.d.ts.map +1 -0
  49. package/lib/packlets/editor/jsonEditor.js +332 -0
  50. package/lib/packlets/editor/jsonEditor.js.map +1 -0
  51. package/lib/packlets/editor/jsonEditorRule.d.ts +64 -0
  52. package/lib/packlets/editor/jsonEditorRule.d.ts.map +1 -0
  53. package/lib/packlets/editor/jsonEditorRule.js +54 -0
  54. package/lib/packlets/editor/jsonEditorRule.js.map +1 -0
  55. package/lib/packlets/editor/jsonEditorState.d.ts +125 -0
  56. package/lib/packlets/editor/jsonEditorState.d.ts.map +1 -0
  57. package/lib/packlets/editor/jsonEditorState.js +177 -0
  58. package/lib/packlets/editor/jsonEditorState.js.map +1 -0
  59. package/lib/packlets/editor/jsonReferenceMap.d.ts +303 -0
  60. package/lib/packlets/editor/jsonReferenceMap.d.ts.map +1 -0
  61. package/lib/packlets/editor/jsonReferenceMap.js +324 -0
  62. package/lib/packlets/editor/jsonReferenceMap.js.map +1 -0
  63. package/lib/packlets/editor/rules/conditional.d.ts +107 -0
  64. package/lib/packlets/editor/rules/conditional.d.ts.map +1 -0
  65. package/lib/packlets/editor/rules/conditional.js +167 -0
  66. package/lib/packlets/editor/rules/conditional.js.map +1 -0
  67. package/{jsonEditor → lib/packlets/editor}/rules/index.d.ts +1 -0
  68. package/lib/packlets/editor/rules/index.d.ts.map +1 -0
  69. package/lib/packlets/editor/rules/index.js +42 -0
  70. package/lib/packlets/editor/rules/index.js.map +1 -0
  71. package/lib/packlets/editor/rules/multivalue.d.ts +94 -0
  72. package/lib/packlets/editor/rules/multivalue.d.ts.map +1 -0
  73. package/lib/packlets/editor/rules/multivalue.js +141 -0
  74. package/lib/packlets/editor/rules/multivalue.js.map +1 -0
  75. package/lib/packlets/editor/rules/references.d.ts +69 -0
  76. package/lib/packlets/editor/rules/references.d.ts.map +1 -0
  77. package/lib/packlets/editor/rules/references.js +159 -0
  78. package/lib/packlets/editor/rules/references.js.map +1 -0
  79. package/lib/packlets/editor/rules/templates.d.ts +74 -0
  80. package/lib/packlets/editor/rules/templates.d.ts.map +1 -0
  81. package/lib/packlets/editor/rules/templates.js +127 -0
  82. package/lib/packlets/editor/rules/templates.js.map +1 -0
  83. package/lib/packlets/json/common.d.ts +85 -0
  84. package/lib/packlets/json/common.d.ts.map +1 -0
  85. package/lib/packlets/json/common.js +123 -0
  86. package/lib/packlets/json/common.js.map +1 -0
  87. package/lib/packlets/json/index.d.ts +2 -0
  88. package/lib/packlets/json/index.d.ts.map +1 -0
  89. package/lib/packlets/json/index.js +39 -0
  90. package/lib/packlets/json/index.js.map +1 -0
  91. package/package.json +58 -49
  92. package/common.d.ts +0 -58
  93. package/common.js +0 -116
  94. package/compositeJsonMap.d.ts +0 -47
  95. package/compositeJsonMap.js +0 -97
  96. package/contextHelpers.d.ts +0 -21
  97. package/contextHelpers.js +0 -101
  98. package/converters.d.ts +0 -37
  99. package/converters.js +0 -92
  100. package/file.d.ts +0 -60
  101. package/file.js +0 -140
  102. package/index.d.ts +0 -10
  103. package/index.js +0 -62
  104. package/jsonContext.d.ts +0 -74
  105. package/jsonContext.js +0 -40
  106. package/jsonConverter.d.ts +0 -229
  107. package/jsonConverter.js +0 -292
  108. package/jsonEditor/common.d.ts +0 -36
  109. package/jsonEditor/common.js +0 -3
  110. package/jsonEditor/index.d.ts +0 -5
  111. package/jsonEditor/jsonEditor.d.ts +0 -74
  112. package/jsonEditor/jsonEditor.js +0 -259
  113. package/jsonEditor/jsonEditorRule.d.ts +0 -47
  114. package/jsonEditor/jsonEditorRule.js +0 -44
  115. package/jsonEditor/jsonEditorState.d.ts +0 -24
  116. package/jsonEditor/jsonEditorState.js +0 -86
  117. package/jsonEditor/rules/conditional.d.ts +0 -84
  118. package/jsonEditor/rules/conditional.js +0 -146
  119. package/jsonEditor/rules/index.js +0 -21
  120. package/jsonEditor/rules/multivalue.d.ts +0 -78
  121. package/jsonEditor/rules/multivalue.js +0 -129
  122. package/jsonEditor/rules/references.d.ts +0 -62
  123. package/jsonEditor/rules/references.js +0 -158
  124. package/jsonEditor/rules/templates.d.ts +0 -56
  125. package/jsonEditor/rules/templates.js +0 -115
  126. package/jsonReferenceMap.d.ts +0 -138
  127. package/jsonReferenceMap.js +0 -202
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.35.2"
9
+ }
10
+ ]
11
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './packlets/context';
2
+ export * from './packlets/converters';
3
+ export * from './packlets/editor';
4
+ export * from './packlets/json';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAsBA,cAAc,oBAAoB,CAAC;AACnC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2023 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
24
+ if (k2 === undefined) k2 = k;
25
+ var desc = Object.getOwnPropertyDescriptor(m, k);
26
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
27
+ desc = { enumerable: true, get: function() { return m[k]; } };
28
+ }
29
+ Object.defineProperty(o, k2, desc);
30
+ }) : (function(o, m, k, k2) {
31
+ if (k2 === undefined) k2 = k;
32
+ o[k2] = m[k];
33
+ }));
34
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
35
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ __exportStar(require("./packlets/context"), exports);
39
+ __exportStar(require("./packlets/converters"), exports);
40
+ __exportStar(require("./packlets/editor"), exports);
41
+ __exportStar(require("./packlets/json"), exports);
42
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;AAEH,qDAAmC;AACnC,wDAAsC;AACtC,oDAAkC;AAClC,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\nexport * from './packlets/context';\nexport * from './packlets/converters';\nexport * from './packlets/editor';\nexport * from './packlets/json';\n"]}
@@ -0,0 +1,60 @@
1
+ import { DetailedResult, Result } from '@fgv/ts-utils';
2
+ import { JsonObject, JsonValue } from '../json';
3
+ import { IJsonContext, IJsonReferenceMap, JsonReferenceMapFailureReason } from './jsonContext';
4
+ /**
5
+ * A {@link CompositeJsonMap | CompositeJsonMap} presents a composed view of one or more other
6
+ * {@link IJsonReferenceMap | JSON reference maps}.
7
+ * @public
8
+ */
9
+ export declare class CompositeJsonMap implements IJsonReferenceMap {
10
+ /**
11
+ * The {@link IJsonReferenceMap | reference maps} from which this map is composed.
12
+ * @internal
13
+ */
14
+ protected _maps: IJsonReferenceMap[];
15
+ /**
16
+ * The {@link IJsonReferenceMap | reference maps} from which this map is to be composed.
17
+ * @param maps - An array o {@link IJsonReferenceMap | IJsonReferenceMap} containing the maps
18
+ * from which this map is to be composed.
19
+ * @internal
20
+ */
21
+ protected constructor(maps: IJsonReferenceMap[]);
22
+ /**
23
+ * Creates a new {@link CompositeJsonMap | CompositeJsonMap} from supplied
24
+ * {@link IJsonReferenceMap | maps}.
25
+ * @param maps - one or more {@link IJsonReferenceMap | object maps} to be composed.
26
+ */
27
+ static create(maps: IJsonReferenceMap[]): Result<CompositeJsonMap>;
28
+ /**
29
+ * Determine if a key might be valid for this map but does not determine
30
+ * if key actually exists. Allows key range to be constrained.
31
+ * @param key - The key to be tested.
32
+ * @returns `true` if the key is in the valid range, `false` otherwise.
33
+ */
34
+ keyIsInRange(key: string): boolean;
35
+ /**
36
+ * Determines if an object with the specified key actually exists in the map.
37
+ * @param key - The key to be tested.
38
+ * @returns `true` if an object with the specified key exists, `false` otherwise.
39
+ */
40
+ has(key: string): boolean;
41
+ /**
42
+ * Gets a {@link JsonObject | JSON object} specified by key.
43
+ * @param key - The key of the object to be retrieved.
44
+ * @param context - An optional {@link IJsonContext | JSON Context} used to format the object.
45
+ * @returns `Success` with the formatted object if successful. `Failure` with detail `'unknown'`
46
+ * if no such object exists, or `Failure` with detail `'error'` if the object was found but
47
+ * could not be formatted.
48
+ */
49
+ getJsonObject(key: string, context?: IJsonContext): DetailedResult<JsonObject, JsonReferenceMapFailureReason>;
50
+ /**
51
+ * Gets a {@link JsonValue | JSON value} specified by key.
52
+ * @param key - The key of the object to be retrieved.
53
+ * @param context - An optional {@link IJsonContext | JSON Context} used to format the value.
54
+ * @returns `Success` with the formatted object if successful. `Failure` with detail `'unknown'`
55
+ * if no such object exists, or failure with detail `'error'` if the object was found but
56
+ * could not be formatted.
57
+ */
58
+ getJsonValue(key: string, context?: IJsonContext): DetailedResult<JsonValue, JsonReferenceMapFailureReason>;
59
+ }
60
+ //# sourceMappingURL=compositeJsonMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compositeJsonMap.d.ts","sourceRoot":"","sources":["../../../src/packlets/context/compositeJsonMap.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,cAAc,EAAE,MAAM,EAAoD,MAAM,eAAe,CAAC;AACzG,OAAO,EAAE,UAAU,EAAE,SAAS,EAAgB,MAAM,SAAS,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,6BAA6B,EAAE,MAAM,eAAe,CAAC;AAE/F;;;;GAIG;AACH,qBAAa,gBAAiB,YAAW,iBAAiB;IACxD;;;OAGG;IACH,SAAS,CAAC,KAAK,EAAE,iBAAiB,EAAE,CAAC;IAErC;;;;;OAKG;IACH,SAAS,aAAa,IAAI,EAAE,iBAAiB,EAAE;IAI/C;;;;OAIG;WACW,MAAM,CAAC,IAAI,EAAE,iBAAiB,EAAE,GAAG,MAAM,CAAC,gBAAgB,CAAC;IAIzE;;;;;OAKG;IACI,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzC;;;;OAIG;IACI,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIhC;;;;;;;OAOG;IACI,aAAa,CAClB,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,YAAY,GACrB,cAAc,CAAC,UAAU,EAAE,6BAA6B,CAAC;IAS5D;;;;;;;OAOG;IAEI,YAAY,CACjB,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,YAAY,GACrB,cAAc,CAAC,SAAS,EAAE,6BAA6B,CAAC;CAW5D"}
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2020 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.CompositeJsonMap = void 0;
25
+ const ts_utils_1 = require("@fgv/ts-utils");
26
+ const json_1 = require("../json");
27
+ /**
28
+ * A {@link CompositeJsonMap | CompositeJsonMap} presents a composed view of one or more other
29
+ * {@link IJsonReferenceMap | JSON reference maps}.
30
+ * @public
31
+ */
32
+ class CompositeJsonMap {
33
+ /**
34
+ * The {@link IJsonReferenceMap | reference maps} from which this map is to be composed.
35
+ * @param maps - An array o {@link IJsonReferenceMap | IJsonReferenceMap} containing the maps
36
+ * from which this map is to be composed.
37
+ * @internal
38
+ */
39
+ constructor(maps) {
40
+ this._maps = maps;
41
+ }
42
+ /**
43
+ * Creates a new {@link CompositeJsonMap | CompositeJsonMap} from supplied
44
+ * {@link IJsonReferenceMap | maps}.
45
+ * @param maps - one or more {@link IJsonReferenceMap | object maps} to be composed.
46
+ */
47
+ static create(maps) {
48
+ return (0, ts_utils_1.captureResult)(() => new CompositeJsonMap(maps));
49
+ }
50
+ /**
51
+ * Determine if a key might be valid for this map but does not determine
52
+ * if key actually exists. Allows key range to be constrained.
53
+ * @param key - The key to be tested.
54
+ * @returns `true` if the key is in the valid range, `false` otherwise.
55
+ */
56
+ keyIsInRange(key) {
57
+ return this._maps.find((map) => map.keyIsInRange(key)) !== undefined;
58
+ }
59
+ /**
60
+ * Determines if an object with the specified key actually exists in the map.
61
+ * @param key - The key to be tested.
62
+ * @returns `true` if an object with the specified key exists, `false` otherwise.
63
+ */
64
+ has(key) {
65
+ return this._maps.find((map) => map.has(key)) !== undefined;
66
+ }
67
+ /**
68
+ * Gets a {@link JsonObject | JSON object} specified by key.
69
+ * @param key - The key of the object to be retrieved.
70
+ * @param context - An optional {@link IJsonContext | JSON Context} used to format the object.
71
+ * @returns `Success` with the formatted object if successful. `Failure` with detail `'unknown'`
72
+ * if no such object exists, or `Failure` with detail `'error'` if the object was found but
73
+ * could not be formatted.
74
+ */
75
+ getJsonObject(key, context) {
76
+ return this.getJsonValue(key, context).onSuccess((jv) => {
77
+ if (!(0, json_1.isJsonObject)(jv)) {
78
+ return (0, ts_utils_1.failWithDetail)(`${key}: not an object`, 'error');
79
+ }
80
+ return (0, ts_utils_1.succeedWithDetail)(jv);
81
+ });
82
+ }
83
+ /**
84
+ * Gets a {@link JsonValue | JSON value} specified by key.
85
+ * @param key - The key of the object to be retrieved.
86
+ * @param context - An optional {@link IJsonContext | JSON Context} used to format the value.
87
+ * @returns `Success` with the formatted object if successful. `Failure` with detail `'unknown'`
88
+ * if no such object exists, or failure with detail `'error'` if the object was found but
89
+ * could not be formatted.
90
+ */
91
+ // eslint-disable-next-line no-use-before-define
92
+ getJsonValue(key, context) {
93
+ for (const map of this._maps) {
94
+ if (map.keyIsInRange(key)) {
95
+ const result = map.getJsonValue(key, context);
96
+ if (result.isSuccess() || result.detail === 'error') {
97
+ return result;
98
+ }
99
+ }
100
+ }
101
+ return (0, ts_utils_1.failWithDetail)(`${key}: value not found`, 'unknown');
102
+ }
103
+ }
104
+ exports.CompositeJsonMap = CompositeJsonMap;
105
+ //# sourceMappingURL=compositeJsonMap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compositeJsonMap.js","sourceRoot":"","sources":["../../../src/packlets/context/compositeJsonMap.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAAyG;AACzG,kCAA8D;AAG9D;;;;GAIG;AACH,MAAa,gBAAgB;IAO3B;;;;;OAKG;IACH,YAAsB,IAAyB;QAC7C,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,IAAyB;QAC5C,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC;IACzD,CAAC;IAED;;;;;OAKG;IACI,YAAY,CAAC,GAAW;QAC7B,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,KAAK,SAAS,CAAC;IACvE,CAAC;IAED;;;;OAIG;IACI,GAAG,CAAC,GAAW;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,KAAK,SAAS,CAAC;IAC9D,CAAC;IAED;;;;;;;OAOG;IACI,aAAa,CAClB,GAAW,EACX,OAAsB;QAEtB,OAAO,IAAI,CAAC,YAAY,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,EAAE;YACtD,IAAI,CAAC,IAAA,mBAAY,EAAC,EAAE,CAAC,EAAE;gBACrB,OAAO,IAAA,yBAAc,EAAC,GAAG,GAAG,iBAAiB,EAAE,OAAO,CAAC,CAAC;aACzD;YACD,OAAO,IAAA,4BAAiB,EAAC,EAAE,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;OAOG;IACH,gDAAgD;IACzC,YAAY,CACjB,GAAW,EACX,OAAsB;QAEtB,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,KAAK,EAAE;YAC5B,IAAI,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE;gBACzB,MAAM,MAAM,GAAG,GAAG,CAAC,YAAY,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;gBAC9C,IAAI,MAAM,CAAC,SAAS,EAAE,IAAI,MAAM,CAAC,MAAM,KAAK,OAAO,EAAE;oBACnD,OAAO,MAAM,CAAC;iBACf;aACF;SACF;QACD,OAAO,IAAA,yBAAc,EAAC,GAAG,GAAG,mBAAmB,EAAE,SAAS,CAAC,CAAC;IAC9D,CAAC;CACF;AAxFD,4CAwFC","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 { DetailedResult, Result, captureResult, failWithDetail, succeedWithDetail } from '@fgv/ts-utils';\nimport { JsonObject, JsonValue, isJsonObject } from '../json';\nimport { IJsonContext, IJsonReferenceMap, JsonReferenceMapFailureReason } from './jsonContext';\n\n/**\n * A {@link CompositeJsonMap | CompositeJsonMap} presents a composed view of one or more other\n * {@link IJsonReferenceMap | JSON reference maps}.\n * @public\n */\nexport class CompositeJsonMap implements IJsonReferenceMap {\n /**\n * The {@link IJsonReferenceMap | reference maps} from which this map is composed.\n * @internal\n */\n protected _maps: IJsonReferenceMap[];\n\n /**\n * The {@link IJsonReferenceMap | reference maps} from which this map is to be composed.\n * @param maps - An array o {@link IJsonReferenceMap | IJsonReferenceMap} containing the maps\n * from which this map is to be composed.\n * @internal\n */\n protected constructor(maps: IJsonReferenceMap[]) {\n this._maps = maps;\n }\n\n /**\n * Creates a new {@link CompositeJsonMap | CompositeJsonMap} from supplied\n * {@link IJsonReferenceMap | maps}.\n * @param maps - one or more {@link IJsonReferenceMap | object maps} to be composed.\n */\n public static create(maps: IJsonReferenceMap[]): Result<CompositeJsonMap> {\n return captureResult(() => new CompositeJsonMap(maps));\n }\n\n /**\n * Determine if a key might be valid for this map but does not determine\n * if key actually exists. Allows key range to be constrained.\n * @param key - The key to be tested.\n * @returns `true` if the key is in the valid range, `false` otherwise.\n */\n public keyIsInRange(key: string): boolean {\n return this._maps.find((map) => map.keyIsInRange(key)) !== undefined;\n }\n\n /**\n * Determines if an object with the specified key actually exists in the map.\n * @param key - The key to be tested.\n * @returns `true` if an object with the specified key exists, `false` otherwise.\n */\n public has(key: string): boolean {\n return this._maps.find((map) => map.has(key)) !== undefined;\n }\n\n /**\n * Gets a {@link JsonObject | JSON object} specified by key.\n * @param key - The key of the object to be retrieved.\n * @param context - An optional {@link IJsonContext | JSON Context} used to format the object.\n * @returns `Success` with the formatted object if successful. `Failure` with detail `'unknown'`\n * if no such object exists, or `Failure` with detail `'error'` if the object was found but\n * could not be formatted.\n */\n public getJsonObject(\n key: string,\n context?: IJsonContext\n ): DetailedResult<JsonObject, JsonReferenceMapFailureReason> {\n return this.getJsonValue(key, context).onSuccess((jv) => {\n if (!isJsonObject(jv)) {\n return failWithDetail(`${key}: not an object`, 'error');\n }\n return succeedWithDetail(jv);\n });\n }\n\n /**\n * Gets a {@link JsonValue | JSON value} specified by key.\n * @param key - The key of the object to be retrieved.\n * @param context - An optional {@link IJsonContext | JSON Context} used to format the value.\n * @returns `Success` with the formatted object if successful. `Failure` with detail `'unknown'`\n * if no such object exists, or failure with detail `'error'` if the object was found but\n * could not be formatted.\n */\n // eslint-disable-next-line no-use-before-define\n public getJsonValue(\n key: string,\n context?: IJsonContext\n ): DetailedResult<JsonValue, JsonReferenceMapFailureReason> {\n for (const map of this._maps) {\n if (map.keyIsInRange(key)) {\n const result = map.getJsonValue(key, context);\n if (result.isSuccess() || result.detail === 'error') {\n return result;\n }\n }\n }\n return failWithDetail(`${key}: value not found`, 'unknown');\n }\n}\n"]}
@@ -0,0 +1,116 @@
1
+ import { Result } from '@fgv/ts-utils';
2
+ import { IJsonContext, IJsonReferenceMap, TemplateVars, VariableValue } from './jsonContext';
3
+ /**
4
+ * Helper class for working with {@link IJsonContext | IJsonContext} objects.
5
+ * @public
6
+ */
7
+ export declare class JsonContextHelper {
8
+ /**
9
+ * The base {@link IJsonContext | context} on which we are operating.
10
+ * @internal
11
+ */
12
+ protected _context?: IJsonContext;
13
+ /**
14
+ * Constructs a new {@link JsonContextHelper | JsonContextHelper}.
15
+ * @param context - The base {@link IJsonContext | IJsonContext} on
16
+ * which to operate.
17
+ */
18
+ constructor(context?: IJsonContext);
19
+ /**
20
+ * Creates a new {@link IJsonContext | context}.
21
+ * @param context - The base {@link IJsonContext | IJsonContext} on
22
+ * which to operate.
23
+ * @returns `Success` with the new {@link IJsonContext | IJsonContext},
24
+ * or `Failure` with more information if an error occurs.
25
+ */
26
+ static create(context?: IJsonContext): Result<JsonContextHelper>;
27
+ /**
28
+ * Static helper to extend context variables for a supplied {@link IJsonContext | IJsonContext}.
29
+ * @param baseContext - The {@link IJsonContext | IJsonContext} to be extended, or `undefined`
30
+ * to start from an empty context.
31
+ * @param vars - Optional {@link VariableValue | variable values} to be added to the
32
+ * {@link IJsonContext | context}.
33
+ * @returns `Success` with a new {@link TemplateVars | TemplateVars} containing the variables
34
+ * from the base context, merged with and overridden by any that were passed in, or `Failure`
35
+ * with a message if an error occurs.
36
+ */
37
+ static extendContextVars(baseContext: IJsonContext | undefined, vars?: VariableValue[]): Result<TemplateVars | undefined>;
38
+ /**
39
+ * Static helper to extend context references for a supplied {@link IJsonContext | IJsonContext}.
40
+ * @param baseContext - The {@link IJsonContext | IJsonContext} to be extended, or `undefined`
41
+ * to start from an empty context.
42
+ * @param refs - Optional {@link IJsonReferenceMap | reference maps} to be added to the
43
+ * {@link IJsonContext | context}.
44
+ * @returns `Success` with a new {@link IJsonReferenceMap | reference map} which projects
45
+ * the references from the base context, merged with and overridden by any that were passed in,
46
+ * or `Failure` with a message if an error occurs.
47
+ */
48
+ static extendContextRefs(baseContext: IJsonContext | undefined, refs?: IJsonReferenceMap[]): Result<IJsonReferenceMap | undefined>;
49
+ /**
50
+ * Static helper to extend context variables and references for a supplied {@link IJsonContext | IJsonContext}.
51
+ * @param baseContext - The {@link IJsonContext | IJsonContext} to be extended, or `undefined`
52
+ * to start from an empty context.
53
+ * @param add - Optional initializer containing {@link VariableValue | variable values} and/or
54
+ * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.
55
+ * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and
56
+ * references from the base context, merged with and overridden by any that were passed in, or
57
+ * `Failure` with a message if an error occurs.
58
+ */
59
+ static extendContext(baseContext?: IJsonContext | undefined, add?: {
60
+ vars?: VariableValue[];
61
+ refs?: IJsonReferenceMap[];
62
+ }): Result<IJsonContext | undefined>;
63
+ /**
64
+ * Static helper to merge context variables and references for a supplied {@link IJsonContext | IJsonContext}.
65
+ * @param baseContext - The {@link IJsonContext | IJsonContext} into which variables and references
66
+ * are to be merged, or `undefined` to start from an empty context.
67
+ * @param add - Optional initializer containing {@link VariableValue | variable values} and/or
68
+ * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.
69
+ * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and
70
+ * references from the base context, merged with and overridden by any that were passed in, or
71
+ * `Failure` with a message if an error occurs.
72
+ */
73
+ static mergeContext(baseContext: IJsonContext | undefined, add: IJsonContext | undefined): Result<IJsonContext | undefined>;
74
+ /**
75
+ * Applies {@link JsonContextHelper.extendContextVars | extendContextVars} to the
76
+ * {@link IJsonContext | IJsonContext} associated with this helper.
77
+ * @param vars - Optional {@link VariableValue | variable values} to be added to the
78
+ * @returns `Success` with a new {@link TemplateVars | TemplateVars} containing the variables
79
+ * from the base context, merged with and overridden by any that were passed in, or `Failure`
80
+ * with a message if an error occurs.
81
+ */
82
+ extendVars(vars?: VariableValue[]): Result<TemplateVars | undefined>;
83
+ /**
84
+ * Applies {@link JsonContextHelper.extendContextRefs | extendContextRefs} to the
85
+ * {@link IJsonContext | IJsonContext} associated with this helper.
86
+ * @param refs - Optional {@link IJsonReferenceMap | reference maps} to be added to the
87
+ * @returns `Success` with a new {@link IJsonReferenceMap | reference map} which projects
88
+ * the references from the base context, merged with and overridden by any that were passed in,
89
+ * or `Failure` with a message if an error occurs.
90
+ */
91
+ extendRefs(refs?: IJsonReferenceMap[]): Result<IJsonReferenceMap | undefined>;
92
+ /**
93
+ * Applies {@link JsonContextHelper.(extendContext:static) | extendContext} to the
94
+ * {@link IJsonContext | IJsonContext} associated with this helper.
95
+ * @param add - Optional initializer containing {@link VariableValue | variable values} and/or
96
+ * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.
97
+ * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and
98
+ * references from the base context, merged with and overridden by any that were passed in, or
99
+ * `Failure` with a message if an error occurs.
100
+ */
101
+ extendContext(add?: {
102
+ vars?: VariableValue[];
103
+ refs?: IJsonReferenceMap[];
104
+ }): Result<IJsonContext | undefined>;
105
+ /**
106
+ * Applies {@link JsonContextHelper.(mergeContext:static) | mergeContext} to the
107
+ * {@link IJsonContext | IJsonContext} associated with this helper.
108
+ * @param add - Optional initializer containing {@link VariableValue | variable values} and/or
109
+ * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.
110
+ * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and
111
+ * references from the base context, merged with and overridden by any that were passed in, or
112
+ * `Failure` with a message if an error occurs.
113
+ */
114
+ mergeContext(merge?: IJsonContext): Result<IJsonContext | undefined>;
115
+ }
116
+ //# sourceMappingURL=contextHelpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contextHelpers.d.ts","sourceRoot":"","sources":["../../../src/packlets/context/contextHelpers.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,MAAM,EAA0B,MAAM,eAAe,CAAC;AAE/D,OAAO,EACL,YAAY,EACZ,iBAAiB,EACjB,YAAY,EACZ,aAAa,EAEd,MAAM,eAAe,CAAC;AAEvB;;;GAGG;AACH,qBAAa,iBAAiB;IAC5B;;;OAGG;IACH,SAAS,CAAC,QAAQ,CAAC,EAAE,YAAY,CAAC;IAElC;;;;OAIG;gBACgB,OAAO,CAAC,EAAE,YAAY;IAIzC;;;;;;OAMG;WACW,MAAM,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,MAAM,CAAC,iBAAiB,CAAC;IAIvE;;;;;;;;;OASG;WACW,iBAAiB,CAC7B,WAAW,EAAE,YAAY,GAAG,SAAS,EACrC,IAAI,CAAC,EAAE,aAAa,EAAE,GACrB,MAAM,CAAC,YAAY,GAAG,SAAS,CAAC;IAQnC;;;;;;;;;OASG;WACW,iBAAiB,CAC7B,WAAW,EAAE,YAAY,GAAG,SAAS,EACrC,IAAI,CAAC,EAAE,iBAAiB,EAAE,GACzB,MAAM,CAAC,iBAAiB,GAAG,SAAS,CAAC;IAWxC;;;;;;;;;OASG;WACW,aAAa,CACzB,WAAW,CAAC,EAAE,YAAY,GAAG,SAAS,EACtC,GAAG,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,aAAa,EAAE,CAAC;QAAC,IAAI,CAAC,EAAE,iBAAiB,EAAE,CAAA;KAAE,GAC3D,MAAM,CAAC,YAAY,GAAG,SAAS,CAAC;IAenC;;;;;;;;;OASG;WACW,YAAY,CACxB,WAAW,EAAE,YAAY,GAAG,SAAS,EACrC,GAAG,EAAE,YAAY,GAAG,SAAS,GAC5B,MAAM,CAAC,YAAY,GAAG,SAAS,CAAC;IAmBnC;;;;;;;OAOG;IACI,UAAU,CAAC,IAAI,CAAC,EAAE,aAAa,EAAE,GAAG,MAAM,CAAC,YAAY,GAAG,SAAS,CAAC;IAI3E;;;;;;;OAOG;IACI,UAAU,CAAC,IAAI,CAAC,EAAE,iBAAiB,EAAE,GAAG,MAAM,CAAC,iBAAiB,GAAG,SAAS,CAAC;IAIpF;;;;;;;;OAQG;IACI,aAAa,CAAC,GAAG,CAAC,EAAE;QACzB,IAAI,CAAC,EAAE,aAAa,EAAE,CAAC;QACvB,IAAI,CAAC,EAAE,iBAAiB,EAAE,CAAC;KAC5B,GAAG,MAAM,CAAC,YAAY,GAAG,SAAS,CAAC;IAIpC;;;;;;;;OAQG;IACI,YAAY,CAAC,KAAK,CAAC,EAAE,YAAY,GAAG,MAAM,CAAC,YAAY,GAAG,SAAS,CAAC;CAG5E"}
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2020 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.JsonContextHelper = void 0;
25
+ const ts_utils_1 = require("@fgv/ts-utils");
26
+ const compositeJsonMap_1 = require("./compositeJsonMap");
27
+ const jsonContext_1 = require("./jsonContext");
28
+ /**
29
+ * Helper class for working with {@link IJsonContext | IJsonContext} objects.
30
+ * @public
31
+ */
32
+ class JsonContextHelper {
33
+ /**
34
+ * Constructs a new {@link JsonContextHelper | JsonContextHelper}.
35
+ * @param context - The base {@link IJsonContext | IJsonContext} on
36
+ * which to operate.
37
+ */
38
+ constructor(context) {
39
+ this._context = context;
40
+ }
41
+ /**
42
+ * Creates a new {@link IJsonContext | context}.
43
+ * @param context - The base {@link IJsonContext | IJsonContext} on
44
+ * which to operate.
45
+ * @returns `Success` with the new {@link IJsonContext | IJsonContext},
46
+ * or `Failure` with more information if an error occurs.
47
+ */
48
+ static create(context) {
49
+ return (0, ts_utils_1.captureResult)(() => new JsonContextHelper(context));
50
+ }
51
+ /**
52
+ * Static helper to extend context variables for a supplied {@link IJsonContext | IJsonContext}.
53
+ * @param baseContext - The {@link IJsonContext | IJsonContext} to be extended, or `undefined`
54
+ * to start from an empty context.
55
+ * @param vars - Optional {@link VariableValue | variable values} to be added to the
56
+ * {@link IJsonContext | context}.
57
+ * @returns `Success` with a new {@link TemplateVars | TemplateVars} containing the variables
58
+ * from the base context, merged with and overridden by any that were passed in, or `Failure`
59
+ * with a message if an error occurs.
60
+ */
61
+ static extendContextVars(baseContext, vars) {
62
+ var _a, _b;
63
+ if (vars && vars.length > 0) {
64
+ const extend = (_a = baseContext === null || baseContext === void 0 ? void 0 : baseContext.extendVars) !== null && _a !== void 0 ? _a : jsonContext_1.defaultExtendVars;
65
+ return extend((_b = baseContext === null || baseContext === void 0 ? void 0 : baseContext.vars) !== null && _b !== void 0 ? _b : {}, vars);
66
+ }
67
+ return (0, ts_utils_1.succeed)(baseContext === null || baseContext === void 0 ? void 0 : baseContext.vars);
68
+ }
69
+ /**
70
+ * Static helper to extend context references for a supplied {@link IJsonContext | IJsonContext}.
71
+ * @param baseContext - The {@link IJsonContext | IJsonContext} to be extended, or `undefined`
72
+ * to start from an empty context.
73
+ * @param refs - Optional {@link IJsonReferenceMap | reference maps} to be added to the
74
+ * {@link IJsonContext | context}.
75
+ * @returns `Success` with a new {@link IJsonReferenceMap | reference map} which projects
76
+ * the references from the base context, merged with and overridden by any that were passed in,
77
+ * or `Failure` with a message if an error occurs.
78
+ */
79
+ static extendContextRefs(baseContext, refs) {
80
+ if (refs && refs.length > 0) {
81
+ const full = (baseContext === null || baseContext === void 0 ? void 0 : baseContext.refs) ? [...refs, baseContext.refs] : refs;
82
+ if (full.length > 1) {
83
+ return compositeJsonMap_1.CompositeJsonMap.create(full);
84
+ }
85
+ return (0, ts_utils_1.succeed)(full[0]);
86
+ }
87
+ return (0, ts_utils_1.succeed)(baseContext === null || baseContext === void 0 ? void 0 : baseContext.refs);
88
+ }
89
+ /**
90
+ * Static helper to extend context variables and references for a supplied {@link IJsonContext | IJsonContext}.
91
+ * @param baseContext - The {@link IJsonContext | IJsonContext} to be extended, or `undefined`
92
+ * to start from an empty context.
93
+ * @param add - Optional initializer containing {@link VariableValue | variable values} and/or
94
+ * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.
95
+ * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and
96
+ * references from the base context, merged with and overridden by any that were passed in, or
97
+ * `Failure` with a message if an error occurs.
98
+ */
99
+ static extendContext(baseContext, add) {
100
+ return JsonContextHelper.extendContextVars(baseContext, (add === null || add === void 0 ? void 0 : add.vars) || []).onSuccess((vars) => {
101
+ return JsonContextHelper.extendContextRefs(baseContext, (add === null || add === void 0 ? void 0 : add.refs) || []).onSuccess((refs) => {
102
+ if (!vars && !refs && !(baseContext === null || baseContext === void 0 ? void 0 : baseContext.extendVars)) {
103
+ return (0, ts_utils_1.succeed)(undefined);
104
+ }
105
+ const rtrn = { vars, refs };
106
+ if (baseContext === null || baseContext === void 0 ? void 0 : baseContext.extendVars) {
107
+ rtrn.extendVars = baseContext.extendVars;
108
+ }
109
+ return (0, ts_utils_1.succeed)(rtrn);
110
+ });
111
+ });
112
+ }
113
+ /**
114
+ * Static helper to merge context variables and references for a supplied {@link IJsonContext | IJsonContext}.
115
+ * @param baseContext - The {@link IJsonContext | IJsonContext} into which variables and references
116
+ * are to be merged, or `undefined` to start from an empty context.
117
+ * @param add - Optional initializer containing {@link VariableValue | variable values} and/or
118
+ * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.
119
+ * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and
120
+ * references from the base context, merged with and overridden by any that were passed in, or
121
+ * `Failure` with a message if an error occurs.
122
+ */
123
+ static mergeContext(baseContext, add) {
124
+ var _a, _b;
125
+ if (baseContext) {
126
+ if (add) {
127
+ const rtrn = {
128
+ vars: (_a = add.vars) !== null && _a !== void 0 ? _a : baseContext.vars,
129
+ refs: (_b = add.refs) !== null && _b !== void 0 ? _b : baseContext.refs
130
+ };
131
+ if (add.hasOwnProperty('extendVars')) {
132
+ rtrn.extendVars = add.extendVars;
133
+ }
134
+ else if (baseContext.hasOwnProperty('extendVars')) {
135
+ rtrn.extendVars = baseContext.extendVars;
136
+ }
137
+ return (0, ts_utils_1.succeed)(rtrn);
138
+ }
139
+ return (0, ts_utils_1.succeed)(baseContext);
140
+ }
141
+ return (0, ts_utils_1.succeed)(add);
142
+ }
143
+ /**
144
+ * Applies {@link JsonContextHelper.extendContextVars | extendContextVars} to the
145
+ * {@link IJsonContext | IJsonContext} associated with this helper.
146
+ * @param vars - Optional {@link VariableValue | variable values} to be added to the
147
+ * @returns `Success` with a new {@link TemplateVars | TemplateVars} containing the variables
148
+ * from the base context, merged with and overridden by any that were passed in, or `Failure`
149
+ * with a message if an error occurs.
150
+ */
151
+ extendVars(vars) {
152
+ return JsonContextHelper.extendContextVars(this._context, vars);
153
+ }
154
+ /**
155
+ * Applies {@link JsonContextHelper.extendContextRefs | extendContextRefs} to the
156
+ * {@link IJsonContext | IJsonContext} associated with this helper.
157
+ * @param refs - Optional {@link IJsonReferenceMap | reference maps} to be added to the
158
+ * @returns `Success` with a new {@link IJsonReferenceMap | reference map} which projects
159
+ * the references from the base context, merged with and overridden by any that were passed in,
160
+ * or `Failure` with a message if an error occurs.
161
+ */
162
+ extendRefs(refs) {
163
+ return JsonContextHelper.extendContextRefs(this._context, refs);
164
+ }
165
+ /**
166
+ * Applies {@link JsonContextHelper.(extendContext:static) | extendContext} to the
167
+ * {@link IJsonContext | IJsonContext} associated with this helper.
168
+ * @param add - Optional initializer containing {@link VariableValue | variable values} and/or
169
+ * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.
170
+ * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and
171
+ * references from the base context, merged with and overridden by any that were passed in, or
172
+ * `Failure` with a message if an error occurs.
173
+ */
174
+ extendContext(add) {
175
+ return JsonContextHelper.extendContext(this._context, add);
176
+ }
177
+ /**
178
+ * Applies {@link JsonContextHelper.(mergeContext:static) | mergeContext} to the
179
+ * {@link IJsonContext | IJsonContext} associated with this helper.
180
+ * @param add - Optional initializer containing {@link VariableValue | variable values} and/or
181
+ * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.
182
+ * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and
183
+ * references from the base context, merged with and overridden by any that were passed in, or
184
+ * `Failure` with a message if an error occurs.
185
+ */
186
+ mergeContext(merge) {
187
+ return JsonContextHelper.mergeContext(this._context, merge);
188
+ }
189
+ }
190
+ exports.JsonContextHelper = JsonContextHelper;
191
+ //# sourceMappingURL=contextHelpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contextHelpers.js","sourceRoot":"","sources":["../../../src/packlets/context/contextHelpers.ts"],"names":[],"mappings":"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","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, succeed } from '@fgv/ts-utils';\nimport { CompositeJsonMap } from './compositeJsonMap';\nimport {\n IJsonContext,\n IJsonReferenceMap,\n TemplateVars,\n VariableValue,\n defaultExtendVars\n} from './jsonContext';\n\n/**\n * Helper class for working with {@link IJsonContext | IJsonContext} objects.\n * @public\n */\nexport class JsonContextHelper {\n /**\n * The base {@link IJsonContext | context} on which we are operating.\n * @internal\n */\n protected _context?: IJsonContext;\n\n /**\n * Constructs a new {@link JsonContextHelper | JsonContextHelper}.\n * @param context - The base {@link IJsonContext | IJsonContext} on\n * which to operate.\n */\n public constructor(context?: IJsonContext) {\n this._context = context;\n }\n\n /**\n * Creates a new {@link IJsonContext | context}.\n * @param context - The base {@link IJsonContext | IJsonContext} on\n * which to operate.\n * @returns `Success` with the new {@link IJsonContext | IJsonContext},\n * or `Failure` with more information if an error occurs.\n */\n public static create(context?: IJsonContext): Result<JsonContextHelper> {\n return captureResult(() => new JsonContextHelper(context));\n }\n\n /**\n * Static helper to extend context variables for a supplied {@link IJsonContext | IJsonContext}.\n * @param baseContext - The {@link IJsonContext | IJsonContext} to be extended, or `undefined`\n * to start from an empty context.\n * @param vars - Optional {@link VariableValue | variable values} to be added to the\n * {@link IJsonContext | context}.\n * @returns `Success` with a new {@link TemplateVars | TemplateVars} containing the variables\n * from the base context, merged with and overridden by any that were passed in, or `Failure`\n * with a message if an error occurs.\n */\n public static extendContextVars(\n baseContext: IJsonContext | undefined,\n vars?: VariableValue[]\n ): Result<TemplateVars | undefined> {\n if (vars && vars.length > 0) {\n const extend = baseContext?.extendVars ?? defaultExtendVars;\n return extend(baseContext?.vars ?? {}, vars);\n }\n return succeed(baseContext?.vars);\n }\n\n /**\n * Static helper to extend context references for a supplied {@link IJsonContext | IJsonContext}.\n * @param baseContext - The {@link IJsonContext | IJsonContext} to be extended, or `undefined`\n * to start from an empty context.\n * @param refs - Optional {@link IJsonReferenceMap | reference maps} to be added to the\n * {@link IJsonContext | context}.\n * @returns `Success` with a new {@link IJsonReferenceMap | reference map} which projects\n * the references from the base context, merged with and overridden by any that were passed in,\n * or `Failure` with a message if an error occurs.\n */\n public static extendContextRefs(\n baseContext: IJsonContext | undefined,\n refs?: IJsonReferenceMap[]\n ): Result<IJsonReferenceMap | undefined> {\n if (refs && refs.length > 0) {\n const full = baseContext?.refs ? [...refs, baseContext.refs] : refs;\n if (full.length > 1) {\n return CompositeJsonMap.create(full);\n }\n return succeed(full[0]);\n }\n return succeed(baseContext?.refs);\n }\n\n /**\n * Static helper to extend context variables and references for a supplied {@link IJsonContext | IJsonContext}.\n * @param baseContext - The {@link IJsonContext | IJsonContext} to be extended, or `undefined`\n * to start from an empty context.\n * @param add - Optional initializer containing {@link VariableValue | variable values} and/or\n * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.\n * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and\n * references from the base context, merged with and overridden by any that were passed in, or\n * `Failure` with a message if an error occurs.\n */\n public static extendContext(\n baseContext?: IJsonContext | undefined,\n add?: { vars?: VariableValue[]; refs?: IJsonReferenceMap[] }\n ): Result<IJsonContext | undefined> {\n return JsonContextHelper.extendContextVars(baseContext, add?.vars || []).onSuccess((vars) => {\n return JsonContextHelper.extendContextRefs(baseContext, add?.refs || []).onSuccess((refs) => {\n if (!vars && !refs && !baseContext?.extendVars) {\n return succeed(undefined);\n }\n const rtrn: IJsonContext = { vars, refs };\n if (baseContext?.extendVars) {\n rtrn.extendVars = baseContext.extendVars;\n }\n return succeed(rtrn);\n });\n });\n }\n\n /**\n * Static helper to merge context variables and references for a supplied {@link IJsonContext | IJsonContext}.\n * @param baseContext - The {@link IJsonContext | IJsonContext} into which variables and references\n * are to be merged, or `undefined` to start from an empty context.\n * @param add - Optional initializer containing {@link VariableValue | variable values} and/or\n * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.\n * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and\n * references from the base context, merged with and overridden by any that were passed in, or\n * `Failure` with a message if an error occurs.\n */\n public static mergeContext(\n baseContext: IJsonContext | undefined,\n add: IJsonContext | undefined\n ): Result<IJsonContext | undefined> {\n if (baseContext) {\n if (add) {\n const rtrn: IJsonContext = {\n vars: add.vars ?? baseContext.vars,\n refs: add.refs ?? baseContext.refs\n };\n if (add.hasOwnProperty('extendVars')) {\n rtrn.extendVars = add.extendVars;\n } else if (baseContext.hasOwnProperty('extendVars')) {\n rtrn.extendVars = baseContext.extendVars;\n }\n return succeed(rtrn);\n }\n return succeed(baseContext);\n }\n return succeed(add);\n }\n\n /**\n * Applies {@link JsonContextHelper.extendContextVars | extendContextVars} to the\n * {@link IJsonContext | IJsonContext} associated with this helper.\n * @param vars - Optional {@link VariableValue | variable values} to be added to the\n * @returns `Success` with a new {@link TemplateVars | TemplateVars} containing the variables\n * from the base context, merged with and overridden by any that were passed in, or `Failure`\n * with a message if an error occurs.\n */\n public extendVars(vars?: VariableValue[]): Result<TemplateVars | undefined> {\n return JsonContextHelper.extendContextVars(this._context, vars);\n }\n\n /**\n * Applies {@link JsonContextHelper.extendContextRefs | extendContextRefs} to the\n * {@link IJsonContext | IJsonContext} associated with this helper.\n * @param refs - Optional {@link IJsonReferenceMap | reference maps} to be added to the\n * @returns `Success` with a new {@link IJsonReferenceMap | reference map} which projects\n * the references from the base context, merged with and overridden by any that were passed in,\n * or `Failure` with a message if an error occurs.\n */\n public extendRefs(refs?: IJsonReferenceMap[]): Result<IJsonReferenceMap | undefined> {\n return JsonContextHelper.extendContextRefs(this._context, refs);\n }\n\n /**\n * Applies {@link JsonContextHelper.(extendContext:static) | extendContext} to the\n * {@link IJsonContext | IJsonContext} associated with this helper.\n * @param add - Optional initializer containing {@link VariableValue | variable values} and/or\n * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.\n * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and\n * references from the base context, merged with and overridden by any that were passed in, or\n * `Failure` with a message if an error occurs.\n */\n public extendContext(add?: {\n vars?: VariableValue[];\n refs?: IJsonReferenceMap[];\n }): Result<IJsonContext | undefined> {\n return JsonContextHelper.extendContext(this._context, add);\n }\n\n /**\n * Applies {@link JsonContextHelper.(mergeContext:static) | mergeContext} to the\n * {@link IJsonContext | IJsonContext} associated with this helper.\n * @param add - Optional initializer containing {@link VariableValue | variable values} and/or\n * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.\n * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and\n * references from the base context, merged with and overridden by any that were passed in, or\n * `Failure` with a message if an error occurs.\n */\n public mergeContext(merge?: IJsonContext): Result<IJsonContext | undefined> {\n return JsonContextHelper.mergeContext(this._context, merge);\n }\n}\n"]}
@@ -0,0 +1,4 @@
1
+ export { CompositeJsonMap } from './compositeJsonMap';
2
+ export { JsonContextHelper } from './contextHelpers';
3
+ export { IJsonContext, IJsonReferenceMap, JsonReferenceMapFailureReason, TemplateVars, TemplateVarsExtendFunction, VariableValue, defaultExtendVars } from './jsonContext';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/context/index.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EACL,YAAY,EACZ,iBAAiB,EACjB,6BAA6B,EAC7B,YAAY,EACZ,0BAA0B,EAC1B,aAAa,EACb,iBAAiB,EAClB,MAAM,eAAe,CAAC"}