@fgv/ts-json 5.0.0-2 → 5.0.0-21

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 (94) hide show
  1. package/.vscode/launch.json +16 -0
  2. package/.vscode/settings.json +32 -0
  3. package/config/api-extractor.json +343 -0
  4. package/config/rig.json +16 -0
  5. package/dist/ts-json.d.ts +719 -28
  6. package/dist/tsdoc-metadata.json +1 -1
  7. package/lib/index.d.ts +2 -0
  8. package/lib/index.js +25 -0
  9. package/lib/packlets/diff/detailedDiff.d.ts +375 -0
  10. package/lib/packlets/diff/detailedDiff.js +342 -0
  11. package/lib/packlets/diff/index.d.ts +3 -0
  12. package/lib/packlets/diff/index.js +40 -0
  13. package/lib/packlets/diff/threeWayDiff.d.ts +263 -0
  14. package/lib/packlets/diff/threeWayDiff.js +261 -0
  15. package/lib/packlets/diff/utils.d.ts +6 -0
  16. package/lib/packlets/diff/utils.js +62 -0
  17. package/lib/packlets/editor/common.d.ts +6 -0
  18. package/lib/packlets/editor/jsonEditor.d.ts +57 -28
  19. package/lib/packlets/editor/jsonEditor.js +101 -32
  20. package/lib/test/legacy/jsonConverter.conditional.test.d.ts +2 -0
  21. package/lib/test/legacy/jsonEditor/rules/conditional.test.d.ts +2 -0
  22. package/lib/test/legacy/jsonEditor/rules/multivalue.test.d.ts +2 -0
  23. package/lib/test/legacy/jsonEditor/rules/references.test.d.ts +2 -0
  24. package/lib/test/legacy/jsonEditor/rules/templates.test.d.ts +2 -0
  25. package/lib/test/unit/contextHelpers.test.d.ts +2 -0
  26. package/lib/test/unit/converters.test.d.ts +2 -0
  27. package/lib/test/unit/diff/jsonDiff.test.d.ts +2 -0
  28. package/lib/test/unit/jsonConverter.test.d.ts +2 -0
  29. package/lib/test/unit/jsonEditor/jsonEditor.test.d.ts +2 -0
  30. package/lib/test/unit/jsonEditor/templateContext.test.d.ts +2 -0
  31. package/lib/test/unit/jsonReferenceMap.test.d.ts +2 -0
  32. package/package.json +11 -11
  33. package/src/index.ts +29 -0
  34. package/src/packlets/context/compositeJsonMap.ts +120 -0
  35. package/src/packlets/context/contextHelpers.ts +221 -0
  36. package/src/packlets/context/index.ts +33 -0
  37. package/src/packlets/context/jsonContext.ts +133 -0
  38. package/src/packlets/converters/converters.ts +117 -0
  39. package/src/packlets/converters/index.ts +26 -0
  40. package/src/packlets/converters/jsonConverter.ts +476 -0
  41. package/src/packlets/diff/detailedDiff.ts +585 -0
  42. package/src/packlets/diff/index.ts +24 -0
  43. package/src/packlets/diff/threeWayDiff.ts +420 -0
  44. package/src/packlets/diff/utils.ts +66 -0
  45. package/src/packlets/editor/common.ts +125 -0
  46. package/src/packlets/editor/index.ts +36 -0
  47. package/src/packlets/editor/jsonEditor.ts +523 -0
  48. package/src/packlets/editor/jsonEditorRule.ts +117 -0
  49. package/src/packlets/editor/jsonEditorState.ts +225 -0
  50. package/src/packlets/editor/jsonReferenceMap.ts +516 -0
  51. package/src/packlets/editor/rules/conditional.ts +222 -0
  52. package/src/packlets/editor/rules/index.ts +25 -0
  53. package/src/packlets/editor/rules/multivalue.ts +206 -0
  54. package/src/packlets/editor/rules/references.ts +177 -0
  55. package/src/packlets/editor/rules/templates.ts +159 -0
  56. package/CHANGELOG.md +0 -115
  57. package/lib/index.d.ts.map +0 -1
  58. package/lib/index.js.map +0 -1
  59. package/lib/packlets/context/compositeJsonMap.d.ts.map +0 -1
  60. package/lib/packlets/context/compositeJsonMap.js.map +0 -1
  61. package/lib/packlets/context/contextHelpers.d.ts.map +0 -1
  62. package/lib/packlets/context/contextHelpers.js.map +0 -1
  63. package/lib/packlets/context/index.d.ts.map +0 -1
  64. package/lib/packlets/context/index.js.map +0 -1
  65. package/lib/packlets/context/jsonContext.d.ts.map +0 -1
  66. package/lib/packlets/context/jsonContext.js.map +0 -1
  67. package/lib/packlets/converters/converters.d.ts.map +0 -1
  68. package/lib/packlets/converters/converters.js.map +0 -1
  69. package/lib/packlets/converters/index.d.ts.map +0 -1
  70. package/lib/packlets/converters/index.js.map +0 -1
  71. package/lib/packlets/converters/jsonConverter.d.ts.map +0 -1
  72. package/lib/packlets/converters/jsonConverter.js.map +0 -1
  73. package/lib/packlets/editor/common.d.ts.map +0 -1
  74. package/lib/packlets/editor/common.js.map +0 -1
  75. package/lib/packlets/editor/index.d.ts.map +0 -1
  76. package/lib/packlets/editor/index.js.map +0 -1
  77. package/lib/packlets/editor/jsonEditor.d.ts.map +0 -1
  78. package/lib/packlets/editor/jsonEditor.js.map +0 -1
  79. package/lib/packlets/editor/jsonEditorRule.d.ts.map +0 -1
  80. package/lib/packlets/editor/jsonEditorRule.js.map +0 -1
  81. package/lib/packlets/editor/jsonEditorState.d.ts.map +0 -1
  82. package/lib/packlets/editor/jsonEditorState.js.map +0 -1
  83. package/lib/packlets/editor/jsonReferenceMap.d.ts.map +0 -1
  84. package/lib/packlets/editor/jsonReferenceMap.js.map +0 -1
  85. package/lib/packlets/editor/rules/conditional.d.ts.map +0 -1
  86. package/lib/packlets/editor/rules/conditional.js.map +0 -1
  87. package/lib/packlets/editor/rules/index.d.ts.map +0 -1
  88. package/lib/packlets/editor/rules/index.js.map +0 -1
  89. package/lib/packlets/editor/rules/multivalue.d.ts.map +0 -1
  90. package/lib/packlets/editor/rules/multivalue.js.map +0 -1
  91. package/lib/packlets/editor/rules/references.d.ts.map +0 -1
  92. package/lib/packlets/editor/rules/references.js.map +0 -1
  93. package/lib/packlets/editor/rules/templates.d.ts.map +0 -1
  94. package/lib/packlets/editor/rules/templates.js.map +0 -1
@@ -0,0 +1,221 @@
1
+ /*
2
+ * Copyright (c) 2020 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ import { Result, captureResult, succeed } from '@fgv/ts-utils';
24
+ import { CompositeJsonMap } from './compositeJsonMap';
25
+ import {
26
+ IJsonContext,
27
+ IJsonReferenceMap,
28
+ TemplateVars,
29
+ VariableValue,
30
+ defaultExtendVars
31
+ } from './jsonContext';
32
+
33
+ /**
34
+ * Helper class for working with {@link IJsonContext | IJsonContext} objects.
35
+ * @public
36
+ */
37
+ export class JsonContextHelper {
38
+ /**
39
+ * The base {@link IJsonContext | context} on which we are operating.
40
+ * @internal
41
+ */
42
+ protected _context?: IJsonContext;
43
+
44
+ /**
45
+ * Constructs a new {@link JsonContextHelper | JsonContextHelper}.
46
+ * @param context - The base {@link IJsonContext | IJsonContext} on
47
+ * which to operate.
48
+ */
49
+ public constructor(context?: IJsonContext) {
50
+ this._context = context;
51
+ }
52
+
53
+ /**
54
+ * Creates a new {@link IJsonContext | context}.
55
+ * @param context - The base {@link IJsonContext | IJsonContext} on
56
+ * which to operate.
57
+ * @returns `Success` with the new {@link IJsonContext | IJsonContext},
58
+ * or `Failure` with more information if an error occurs.
59
+ */
60
+ public static create(context?: IJsonContext): Result<JsonContextHelper> {
61
+ return captureResult(() => new JsonContextHelper(context));
62
+ }
63
+
64
+ /**
65
+ * Static helper to extend context variables for a supplied {@link IJsonContext | IJsonContext}.
66
+ * @param baseContext - The {@link IJsonContext | IJsonContext} to be extended, or `undefined`
67
+ * to start from an empty context.
68
+ * @param vars - Optional {@link VariableValue | variable values} to be added to the
69
+ * {@link IJsonContext | context}.
70
+ * @returns `Success` with a new {@link TemplateVars | TemplateVars} containing the variables
71
+ * from the base context, merged with and overridden by any that were passed in, or `Failure`
72
+ * with a message if an error occurs.
73
+ */
74
+ public static extendContextVars(
75
+ baseContext: IJsonContext | undefined,
76
+ vars?: VariableValue[]
77
+ ): Result<TemplateVars | undefined> {
78
+ if (vars && vars.length > 0) {
79
+ const extend = baseContext?.extendVars ?? defaultExtendVars;
80
+ return extend(baseContext?.vars ?? {}, vars);
81
+ }
82
+ return succeed(baseContext?.vars);
83
+ }
84
+
85
+ /**
86
+ * Static helper to extend context references for a supplied {@link IJsonContext | IJsonContext}.
87
+ * @param baseContext - The {@link IJsonContext | IJsonContext} to be extended, or `undefined`
88
+ * to start from an empty context.
89
+ * @param refs - Optional {@link IJsonReferenceMap | reference maps} to be added to the
90
+ * {@link IJsonContext | context}.
91
+ * @returns `Success` with a new {@link IJsonReferenceMap | reference map} which projects
92
+ * the references from the base context, merged with and overridden by any that were passed in,
93
+ * or `Failure` with a message if an error occurs.
94
+ */
95
+ public static extendContextRefs(
96
+ baseContext: IJsonContext | undefined,
97
+ refs?: IJsonReferenceMap[]
98
+ ): Result<IJsonReferenceMap | undefined> {
99
+ if (refs && refs.length > 0) {
100
+ const full = baseContext?.refs ? [...refs, baseContext.refs] : refs;
101
+ if (full.length > 1) {
102
+ return CompositeJsonMap.create(full);
103
+ }
104
+ return succeed(full[0]);
105
+ }
106
+ return succeed(baseContext?.refs);
107
+ }
108
+
109
+ /**
110
+ * Static helper to extend context variables and references for a supplied {@link IJsonContext | IJsonContext}.
111
+ * @param baseContext - The {@link IJsonContext | IJsonContext} to be extended, or `undefined`
112
+ * to start from an empty context.
113
+ * @param add - Optional initializer containing {@link VariableValue | variable values} and/or
114
+ * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.
115
+ * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and
116
+ * references from the base context, merged with and overridden by any that were passed in, or
117
+ * `Failure` with a message if an error occurs.
118
+ */
119
+ public static extendContext(
120
+ baseContext?: IJsonContext | undefined,
121
+ add?: { vars?: VariableValue[]; refs?: IJsonReferenceMap[] }
122
+ ): Result<IJsonContext | undefined> {
123
+ return JsonContextHelper.extendContextVars(baseContext, add?.vars || []).onSuccess((vars) => {
124
+ return JsonContextHelper.extendContextRefs(baseContext, add?.refs || []).onSuccess((refs) => {
125
+ if (!vars && !refs && !baseContext?.extendVars) {
126
+ return succeed(undefined);
127
+ }
128
+ const rtrn: IJsonContext = { vars, refs };
129
+ if (baseContext?.extendVars) {
130
+ rtrn.extendVars = baseContext.extendVars;
131
+ }
132
+ return succeed(rtrn);
133
+ });
134
+ });
135
+ }
136
+
137
+ /**
138
+ * Static helper to merge context variables and references for a supplied {@link IJsonContext | IJsonContext}.
139
+ * @param baseContext - The {@link IJsonContext | IJsonContext} into which variables and references
140
+ * are to be merged, or `undefined` to start from an empty context.
141
+ * @param add - Optional initializer containing {@link VariableValue | variable values} and/or
142
+ * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.
143
+ * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and
144
+ * references from the base context, merged with and overridden by any that were passed in, or
145
+ * `Failure` with a message if an error occurs.
146
+ */
147
+ public static mergeContext(
148
+ baseContext: IJsonContext | undefined,
149
+ add: IJsonContext | undefined
150
+ ): Result<IJsonContext | undefined> {
151
+ if (baseContext) {
152
+ if (add) {
153
+ const rtrn: IJsonContext = {
154
+ vars: add.vars ?? baseContext.vars,
155
+ refs: add.refs ?? baseContext.refs
156
+ };
157
+ if (add.hasOwnProperty('extendVars')) {
158
+ rtrn.extendVars = add.extendVars;
159
+ } else if (baseContext.hasOwnProperty('extendVars')) {
160
+ rtrn.extendVars = baseContext.extendVars;
161
+ }
162
+ return succeed(rtrn);
163
+ }
164
+ return succeed(baseContext);
165
+ }
166
+ return succeed(add);
167
+ }
168
+
169
+ /**
170
+ * Applies {@link JsonContextHelper.extendContextVars | extendContextVars} to the
171
+ * {@link IJsonContext | IJsonContext} associated with this helper.
172
+ * @param vars - Optional {@link VariableValue | variable values} to be added to the
173
+ * @returns `Success` with a new {@link TemplateVars | TemplateVars} containing the variables
174
+ * from the base context, merged with and overridden by any that were passed in, or `Failure`
175
+ * with a message if an error occurs.
176
+ */
177
+ public extendVars(vars?: VariableValue[]): Result<TemplateVars | undefined> {
178
+ return JsonContextHelper.extendContextVars(this._context, vars);
179
+ }
180
+
181
+ /**
182
+ * Applies {@link JsonContextHelper.extendContextRefs | extendContextRefs} to the
183
+ * {@link IJsonContext | IJsonContext} associated with this helper.
184
+ * @param refs - Optional {@link IJsonReferenceMap | reference maps} to be added to the
185
+ * @returns `Success` with a new {@link IJsonReferenceMap | reference map} which projects
186
+ * the references from the base context, merged with and overridden by any that were passed in,
187
+ * or `Failure` with a message if an error occurs.
188
+ */
189
+ public extendRefs(refs?: IJsonReferenceMap[]): Result<IJsonReferenceMap | undefined> {
190
+ return JsonContextHelper.extendContextRefs(this._context, refs);
191
+ }
192
+
193
+ /**
194
+ * Applies static `JsonContextHelper.extendContext` to the
195
+ * {@link IJsonContext | IJsonContext} associated with this helper.
196
+ * @param add - Optional initializer containing {@link VariableValue | variable values} and/or
197
+ * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.
198
+ * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and
199
+ * references from the base context, merged with and overridden by any that were passed in, or
200
+ * `Failure` with a message if an error occurs.
201
+ */
202
+ public extendContext(add?: {
203
+ vars?: VariableValue[];
204
+ refs?: IJsonReferenceMap[];
205
+ }): Result<IJsonContext | undefined> {
206
+ return JsonContextHelper.extendContext(this._context, add);
207
+ }
208
+
209
+ /**
210
+ * Applies static `JsonContextHelper.mergeContext` to the
211
+ * {@link IJsonContext | IJsonContext} associated with this helper.
212
+ * @param add - Optional initializer containing {@link VariableValue | variable values} and/or
213
+ * {@link IJsonReferenceMap | reference maps} to be added to the {@link IJsonContext | context}.
214
+ * @returns `Success` with a new {@link IJsonContext | IJsonContext} containing the variables and
215
+ * references from the base context, merged with and overridden by any that were passed in, or
216
+ * `Failure` with a message if an error occurs.
217
+ */
218
+ public mergeContext(merge?: IJsonContext): Result<IJsonContext | undefined> {
219
+ return JsonContextHelper.mergeContext(this._context, merge);
220
+ }
221
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ * Copyright (c) 2023 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ export { CompositeJsonMap } from './compositeJsonMap';
24
+ export { JsonContextHelper } from './contextHelpers';
25
+ export {
26
+ IJsonContext,
27
+ IJsonReferenceMap,
28
+ JsonReferenceMapFailureReason,
29
+ TemplateVars,
30
+ TemplateVarsExtendFunction,
31
+ VariableValue,
32
+ defaultExtendVars
33
+ } from './jsonContext';
@@ -0,0 +1,133 @@
1
+ /*
2
+ * Copyright (c) 2020 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ import { JsonObject, JsonValue } from '@fgv/ts-json-base';
24
+ import { DetailedResult, Result, succeed } from '@fgv/ts-utils';
25
+
26
+ /**
27
+ * Collection of variables used for template replacement in a JSON edit or
28
+ * conversion.
29
+ * @public
30
+ */
31
+ export type TemplateVars = Record<string, unknown>;
32
+
33
+ /**
34
+ * Describes one value in a {@link TemplateVars | TemplateVars} collection of
35
+ * variables.
36
+ * @public
37
+ */
38
+ export type VariableValue = [string, unknown];
39
+
40
+ /**
41
+ * Function used to create a new collection of {@link TemplateVars | TemplateVars} with
42
+ * one or more new or changed values.
43
+ * @public
44
+ */
45
+ export type TemplateVarsExtendFunction = (
46
+ base: TemplateVars | undefined,
47
+ values: VariableValue[]
48
+ ) => Result<TemplateVars | undefined>;
49
+
50
+ /**
51
+ * This default implementation of a {@link TemplateVarsExtendFunction | TemplateVarsExtendFunction}
52
+ * creates a new collection via inheritance from the supplied collection.
53
+ * @param base - The base {@link TemplateVars | variables} to be extended.
54
+ * @param values - The {@link VariableValue | values} to be added or overridden in the new variables.
55
+ * @public
56
+ */
57
+ export function defaultExtendVars(
58
+ base: TemplateVars | undefined,
59
+ values: VariableValue[]
60
+ ): Result<TemplateVars | undefined> {
61
+ /* c8 ignore next 1 */
62
+ const rtrn = base ? Object.create(base) : {};
63
+ for (const v of values) {
64
+ rtrn[v[0]] = v[1];
65
+ }
66
+ return succeed(rtrn);
67
+ }
68
+
69
+ /**
70
+ * Failure reason for {@link IJsonReferenceMap | IJsonReferenceMap} lookup, where `'unknown'`
71
+ * means that the object is not present in the map and `'error'` means
72
+ * that an error occurred while retrieving or converting it.
73
+ * @public
74
+ */
75
+ export type JsonReferenceMapFailureReason = 'unknown' | 'error';
76
+
77
+ /**
78
+ * Interface for a simple map that returns named `JsonValue` values with templating,
79
+ * conditional logic, and external reference lookups applied using an optionally supplied context.
80
+ * @public
81
+ */
82
+ export interface IJsonReferenceMap {
83
+ /**
84
+ * Determine if a key might be valid for this map but does not determine if key actually
85
+ * exists. Allows key range to be constrained.
86
+ * @param key - The key to be tested.
87
+ * @returns `true` if the key is in the valid range, `false` otherwise.
88
+ */
89
+ keyIsInRange(key: string): boolean;
90
+
91
+ /**
92
+ * Determines if an object with the specified key actually exists in the map.
93
+ * @param key - The key to be tested.
94
+ * @returns `true` if an object with the specified key exists, `false` otherwise.
95
+ */
96
+ has(key: string): boolean;
97
+
98
+ /**
99
+ * Gets a `JsonObject` specified by key.
100
+ * @param key - The key of the object to be retrieved.
101
+ * @param context - Optional {@link IJsonContext | IJsonContext} used to construct
102
+ * the object.
103
+ * @returns `Success` with the formatted JsonObject if successful. `Failure`
104
+ * with detail `'unknown'` if no such object exists, or `Failure` with detail `'error'` if
105
+ * the object was found but could not be formatted.
106
+ */
107
+ // eslint-disable-next-line no-use-before-define
108
+ getJsonObject(
109
+ key: string,
110
+ context?: IJsonContext
111
+ ): DetailedResult<JsonObject, JsonReferenceMapFailureReason>;
112
+
113
+ /**
114
+ * Gets a `JsonValue` specified by key.
115
+ * @param key - The key of the object to be retrieved.
116
+ * @param context - Optional {@link IJsonContext | JSON Context} used to format the value
117
+ * @returns `Success` with the formatted `JsonValue` if successful. `Failure`
118
+ * with detail `'unknown'` if no such object exists, or `Failure` with detail `'error'` if
119
+ * the object was found but could not be formatted.
120
+ */
121
+ // eslint-disable-next-line no-use-before-define
122
+ getJsonValue(key: string, context?: IJsonContext): DetailedResult<JsonValue, JsonReferenceMapFailureReason>;
123
+ }
124
+
125
+ /**
126
+ * Context used to convert or edit JSON objects.
127
+ * @public
128
+ */
129
+ export interface IJsonContext {
130
+ vars?: TemplateVars;
131
+ refs?: IJsonReferenceMap;
132
+ extendVars?: TemplateVarsExtendFunction;
133
+ }
@@ -0,0 +1,117 @@
1
+ /*
2
+ * Copyright (c) 2020 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ import { JsonArray, JsonObject } from '@fgv/ts-json-base';
24
+ import { Converter } from '@fgv/ts-utils';
25
+ import { IJsonContext } from '../context';
26
+ import {
27
+ ConditionalJsonConverter,
28
+ ConditionalJsonConverterOptions,
29
+ JsonConverter,
30
+ RichJsonConverter,
31
+ RichJsonConverterOptions,
32
+ TemplatedJsonConverter,
33
+ TemplatedJsonConverterOptions
34
+ } from './jsonConverter';
35
+
36
+ /**
37
+ * A simple validating {@link JsonConverter | JSON converter}. Converts unknown to
38
+ * JSON or fails if the unknown contains any invalid JSON values.
39
+ * @public
40
+ */
41
+ export const json: JsonConverter = new JsonConverter();
42
+
43
+ /**
44
+ * A simple validating {@link JsonConverter | JSON converter}. Converts unknown
45
+ * to a `JsonObject`, or fails if the `unknown` contains invalid
46
+ * JSON or is not an object.
47
+ * @public
48
+ */
49
+ export const jsonObject: Converter<JsonObject, IJsonContext> = json.object();
50
+
51
+ /**
52
+ * A simple validating {@link JsonConverter | JSON converter}. Converts `unknown` to a
53
+ * `JsonArray`, or fails if the unknown contains invalid JSON or is
54
+ * not an array.
55
+ * @public
56
+ */
57
+ export const jsonArray: Converter<JsonArray, IJsonContext> = json.array();
58
+
59
+ let templatedJsonDefault: JsonConverter | undefined;
60
+ let conditionalJsonDefault: JsonConverter | undefined;
61
+ let richJsonDefault: JsonConverter | undefined;
62
+
63
+ /**
64
+ * Helper function which creates a new {@link JsonConverter | JsonConverter} which converts an
65
+ * `unknown` value to JSON, rendering any property names or string values using mustache with
66
+ * the supplied context. See the mustache documentation for details of mustache syntax and view.
67
+ * @param options - {@link TemplatedJsonConverterOptions | Options and context} for
68
+ * the conversion.
69
+ * @public
70
+ */
71
+ export function templatedJson(options?: Partial<TemplatedJsonConverterOptions>): JsonConverter {
72
+ if (!options) {
73
+ if (!templatedJsonDefault) {
74
+ templatedJsonDefault = new TemplatedJsonConverter();
75
+ }
76
+ return templatedJsonDefault;
77
+ }
78
+ return new TemplatedJsonConverter(options);
79
+ }
80
+
81
+ /**
82
+ * Helper function which creates a new {@link JsonConverter | JsonConverter} which converts a
83
+ * supplied `unknown` to strongly-typed JSON, by first rendering any property
84
+ * names or string values using mustache with the supplied context, then applying
85
+ * multi-value property expansion and conditional flattening based on property names.
86
+ * @param options - {@link ConditionalJsonConverterOptions | Options and context} for
87
+ * the conversion.
88
+ * @public
89
+ */
90
+ export function conditionalJson(options?: Partial<ConditionalJsonConverterOptions>): JsonConverter {
91
+ if (!options) {
92
+ if (!conditionalJsonDefault) {
93
+ conditionalJsonDefault = new ConditionalJsonConverter();
94
+ }
95
+ return conditionalJsonDefault;
96
+ }
97
+ return new ConditionalJsonConverter(options);
98
+ }
99
+
100
+ /**
101
+ * Helper function which creates a new {@link JsonConverter | JsonConverter} which converts a
102
+ * supplied `unknown` to strongly-typed JSON, by first rendering any property
103
+ * names or string values using mustache with the supplied context, then applying
104
+ * multi-value property expansion and conditional flattening based on property names.
105
+ * @param options - {@link RichJsonConverterOptions | Options and context} for
106
+ * the conversion.
107
+ * @public
108
+ */
109
+ export function richJson(options?: Partial<RichJsonConverterOptions>): JsonConverter {
110
+ if (!options) {
111
+ if (!richJsonDefault) {
112
+ richJsonDefault = new RichJsonConverter();
113
+ }
114
+ return richJsonDefault;
115
+ }
116
+ return new RichJsonConverter(options);
117
+ }
@@ -0,0 +1,26 @@
1
+ /*
2
+ * Copyright (c) 2023 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ import * as Converters from './converters';
24
+
25
+ export * from './jsonConverter';
26
+ export { Converters };