@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,476 @@
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, JsonValue, isJsonObject } from '@fgv/ts-json-base';
24
+ import { Conversion, Converter, Result, captureResult, fail, succeed } from '@fgv/ts-utils';
25
+ import {
26
+ IJsonContext,
27
+ IJsonReferenceMap,
28
+ TemplateVars,
29
+ TemplateVarsExtendFunction,
30
+ defaultExtendVars
31
+ } from '../context';
32
+
33
+ import { EditorRules, IJsonEditorOptions, IJsonEditorRule, JsonEditor } from '../editor';
34
+
35
+ /**
36
+ * Conversion options for {@link JsonConverter | JsonConverter}.
37
+ * @public
38
+ */
39
+ export interface IJsonConverterOptions {
40
+ /**
41
+ * If `true` and if template variables are available,
42
+ * then string property values will be rendered using
43
+ * mustache and those variable values. Otherwise string
44
+ * properties are copied without modification.
45
+ *
46
+ * Defaults to `true` if vars are supplied with options,
47
+ * `false` otherwise.
48
+ */
49
+ useValueTemplates: boolean;
50
+
51
+ /**
52
+ * If `true` and if template variables are available,
53
+ * then property names will be rendered using
54
+ * mustache and those variable values. Otherwise
55
+ * property names are copied without modification.
56
+ *
57
+ * Defaults to `true` if vars are supplied with options,
58
+ * `false` otherwise.
59
+ */
60
+ useNameTemplates: boolean;
61
+
62
+ /**
63
+ * If `true` and if template variables are available,
64
+ * then string property names will be considered for
65
+ * conditionals.
66
+ *
67
+ * Default is to match {@link IJsonConverterOptions.useNameTemplates | useNameTemplates}.
68
+ */
69
+ useConditionalNames: boolean;
70
+
71
+ /**
72
+ * If `true` (default) then properties with unconditional names
73
+ * (which start with !) are flattened.
74
+ */
75
+ flattenUnconditionalValues: boolean;
76
+
77
+ /**
78
+ * If `true` and if both template variables and a
79
+ * context derivation function is available, then properties
80
+ * which match the multi-value name pattern will be expanded.
81
+ * Default matches {@link IJsonConverterOptions.useNameTemplates | useNameTemplates}.
82
+ *
83
+ * Default is `true` unless {@link IJsonConverterOptions.extendVars | extendVars} is
84
+ * explicitly set to `undefined`.
85
+ */
86
+ useMultiValueTemplateNames: boolean;
87
+
88
+ /**
89
+ * The variables (mustache view) used to render templated string names
90
+ * and properties. See the mustache documentation for details of mustache
91
+ * syntax and the template view.
92
+ */
93
+ vars?: TemplateVars;
94
+
95
+ /**
96
+ * Method used to extend variables for children of an array node during
97
+ * expansion. Default is to use a built-in extension function unless
98
+ * {@link IJsonConverterOptions.extendVars | extendVars} is explicitly set to undefined.
99
+ */
100
+ extendVars?: TemplateVarsExtendFunction;
101
+
102
+ /**
103
+ * If `true` and if a {@link IJsonReferenceMap | references map} is supplied
104
+ * in {@link IJsonConverterOptions.refs | refs}, then references in the source
105
+ * object will be replaced with the corresponding value from the reference map.
106
+ *
107
+ * Default is `true` if {@link IJsonConverterOptions.refs | refs} are present in options,
108
+ * `false` otherwise.
109
+ */
110
+ useReferences: boolean;
111
+
112
+ /**
113
+ * An optional {@link IJsonReferenceMap | reference map} used to insert any references
114
+ * in the converted JSON.
115
+ */
116
+ refs?: IJsonReferenceMap;
117
+
118
+ /**
119
+ * If {@link IJsonConverterOptions.onInvalidPropertyName | onInvalidPropertyName} is `'error'`
120
+ * (default) then any property name that is invalid after template rendering causes an error
121
+ * and stops conversion. If {@link IJsonConverterOptions.onInvalidPropertyName | onInvalidPropertyName}
122
+ * is `'ignore'`, then names which are invalid after template rendering are passed through unchanged.
123
+ */
124
+ onInvalidPropertyName: 'error' | 'ignore';
125
+
126
+ /**
127
+ * If {@link IJsonConverterOptions.onInvalidPropertyValue | onInvalidPropertyValue} is `'error'`
128
+ * (default) then any illegal property value causes an error and stops conversion. If
129
+ * {@link IJsonConverterOptions.onInvalidPropertyValue | onInvalidPropertyValue} is `'ignore'` then
130
+ * any invalid property values are silently ignored.
131
+ */
132
+ onInvalidPropertyValue: 'error' | 'ignore';
133
+
134
+ /**
135
+ * If {@link IJsonConverterOptions.onUndefinedPropertyValue | onUndefinedPropertyValue} is `'error'`,
136
+ * then any property with value `undefined` will cause an error and stop conversion. If
137
+ * {@link IJsonConverterOptions.onUndefinedPropertyValue | onUndefinedPropertyValue} is `'ignore'` (default)
138
+ * then any property with value `undefined` is silently ignored.
139
+ */
140
+ onUndefinedPropertyValue: 'error' | 'ignore';
141
+ }
142
+
143
+ /**
144
+ * Merges an optionally supplied partial set of {@link IJsonConverterOptions | options} with
145
+ * the default converter options and taking all dynamic rules into account (e.g. template usage enabled
146
+ * if variables are supplied and disabled if not), producing a fully-resolved set of
147
+ * {@link IJsonConverterOptions | IJsonConverterOptions}.
148
+ * @param partial - An optional partial {@link IJsonConverterOptions | IJsonConverterOptions}
149
+ * to be merged.
150
+ * @public
151
+ */
152
+ export function mergeDefaultJsonConverterOptions(
153
+ partial?: Partial<IJsonConverterOptions>
154
+ ): IJsonConverterOptions {
155
+ const haveVars = partial?.vars !== undefined;
156
+ const haveRefs = partial?.refs !== undefined;
157
+ const extender = partial?.hasOwnProperty('extendVars') ? partial.extendVars : defaultExtendVars;
158
+ const haveExtender = extender !== undefined;
159
+ const namesDefault = partial?.useNameTemplates ?? haveVars;
160
+ const conditionsDefault = partial?.useConditionalNames ?? namesDefault;
161
+
162
+ const options: IJsonConverterOptions = {
163
+ useValueTemplates: partial?.useValueTemplates ?? haveVars,
164
+ useNameTemplates: namesDefault,
165
+ useConditionalNames: conditionsDefault,
166
+ flattenUnconditionalValues: partial?.flattenUnconditionalValues ?? conditionsDefault,
167
+ useMultiValueTemplateNames: partial?.useMultiValueTemplateNames ?? (haveExtender && namesDefault),
168
+ useReferences: partial?.useReferences ?? haveRefs,
169
+ onInvalidPropertyName: partial?.onInvalidPropertyName ?? 'error',
170
+ onInvalidPropertyValue: partial?.onInvalidPropertyValue ?? 'error',
171
+ onUndefinedPropertyValue: partial?.onUndefinedPropertyValue ?? 'ignore',
172
+ extendVars: extender
173
+ };
174
+ if (partial?.vars) {
175
+ options.vars = partial.vars;
176
+ }
177
+ if (partial?.refs) {
178
+ options.refs = partial.refs;
179
+ }
180
+ return options;
181
+ }
182
+
183
+ /**
184
+ * Creates a new {@link IJsonContext | JSON context} using values supplied in an optional partial
185
+ * {@link IJsonConverterOptions | converter options}.
186
+ * @param partial - Optional partial {@link IJsonConverterOptions | IJsonConverterOptions} used to
187
+ * populate the context.
188
+ * @public
189
+ */
190
+ export function contextFromConverterOptions(
191
+ partial?: Partial<IJsonConverterOptions>
192
+ ): IJsonContext | undefined {
193
+ const context: IJsonContext = {};
194
+ if (partial?.vars) {
195
+ context.vars = partial.vars;
196
+ }
197
+ if (partial?.refs) {
198
+ context.refs = partial.refs;
199
+ }
200
+ if (partial?.hasOwnProperty('extendVars')) {
201
+ context.extendVars = partial.extendVars;
202
+ }
203
+ return context.vars || context.refs || context.extendVars ? context : undefined;
204
+ }
205
+
206
+ /**
207
+ * Creates a new {@link JsonEditor | JsonEditor} from an optionally supplied partial
208
+ * {@link IJsonConverterOptions | JSON converter options}.
209
+ * Expands supplied options with default values and constructs an editor with
210
+ * matching configuration and defined rules.
211
+ * @param partial - Optional partial {@link IJsonConverterOptions | IJsonConverterOptions}
212
+ * used to create the editor.
213
+ * @public
214
+ */
215
+ export function converterOptionsToEditor(partial?: Partial<IJsonConverterOptions>): Result<JsonEditor> {
216
+ const converterOptions = mergeDefaultJsonConverterOptions(partial);
217
+ const context = contextFromConverterOptions(partial);
218
+ const validation = {
219
+ onInvalidPropertyName: converterOptions.onInvalidPropertyName,
220
+ onInvalidPropertyValue: converterOptions.onInvalidPropertyValue,
221
+ onUndefinedPropertyValue: converterOptions.onUndefinedPropertyValue
222
+ };
223
+ const editorOptions: IJsonEditorOptions = { context, validation };
224
+
225
+ const rules: IJsonEditorRule[] = [];
226
+ if (converterOptions.useNameTemplates || converterOptions.useValueTemplates) {
227
+ const templateOptions = {
228
+ ...editorOptions,
229
+ useNameTemplates: converterOptions.useNameTemplates,
230
+ useValueTemplates: converterOptions.useValueTemplates
231
+ };
232
+ rules.push(new EditorRules.TemplatedJsonEditorRule(templateOptions));
233
+ }
234
+ if (converterOptions.useConditionalNames || converterOptions.flattenUnconditionalValues) {
235
+ const conditionalOptions = {
236
+ ...editorOptions,
237
+ flattenUnconditionalValues: converterOptions.flattenUnconditionalValues
238
+ };
239
+ rules.push(new EditorRules.ConditionalJsonEditorRule(conditionalOptions));
240
+ }
241
+ if (converterOptions.useMultiValueTemplateNames) {
242
+ rules.push(new EditorRules.MultiValueJsonEditorRule(editorOptions));
243
+ }
244
+ if (converterOptions.useReferences) {
245
+ rules.push(new EditorRules.ReferenceJsonEditorRule(editorOptions));
246
+ }
247
+
248
+ return JsonEditor.create(editorOptions, rules);
249
+ }
250
+
251
+ /**
252
+ * A thin wrapper to allow an arbitrary {@link JsonEditor | JsonEditor} to be used via the
253
+ * \@fgv/ts-utils `Converter` pattern.
254
+ * @public
255
+ */
256
+ export class JsonEditorConverter extends Conversion.BaseConverter<JsonValue, IJsonContext> {
257
+ public readonly editor: JsonEditor;
258
+
259
+ /**
260
+ * Constructs a new {@link JsonEditor | JsonEditor}Converter which uses the supplied editor
261
+ * @param editor -
262
+ */
263
+ public constructor(editor: JsonEditor) {
264
+ super((from, __self, context) => this._convert(from, context), editor.options.context);
265
+ this.editor = editor;
266
+ }
267
+
268
+ /**
269
+ * Constructs a new {@link JsonEditor | JsonEditor}Converter which uses the supplied editor
270
+ * @param editor -
271
+ */
272
+ public static createWithEditor(editor: JsonEditor): Result<JsonEditorConverter> {
273
+ return captureResult(() => new JsonEditorConverter(editor));
274
+ }
275
+
276
+ /**
277
+ * Gets a derived converter which fails if the resulting converted
278
+ * `JsonValue` is not a `JsonObject`.
279
+ */
280
+ public object(): Converter<JsonObject, IJsonContext> {
281
+ return this.map((jv) => {
282
+ if (!isJsonObject(jv)) {
283
+ return fail(`Cannot convert "${JSON.stringify(jv)}" to JSON object.`);
284
+ }
285
+ return succeed(jv);
286
+ });
287
+ }
288
+
289
+ /**
290
+ * Gets a derived converter which fails if the resulting converted
291
+ * `JsonValue` is not a `JsonArray`.
292
+ */
293
+ public array(): Converter<JsonArray, IJsonContext> {
294
+ return this.map((jv) => {
295
+ if (!Array.isArray(jv) || typeof jv !== 'object') {
296
+ return fail(`Cannot convert "${JSON.stringify(jv)}" to JSON array.`);
297
+ }
298
+ return succeed(jv);
299
+ });
300
+ }
301
+
302
+ protected _convert(from: unknown, context?: IJsonContext): Result<JsonValue> {
303
+ return this.editor.clone(from as JsonValue, context);
304
+ }
305
+ }
306
+
307
+ /**
308
+ * An \@fgv/ts-utils `Converter` from `unknown` to type-safe JSON, optionally
309
+ * rendering any string property names or values using mustache with a supplied view.
310
+ * @public
311
+ */
312
+ export class JsonConverter extends JsonEditorConverter {
313
+ /**
314
+ * Constructs a new {@link JsonConverter | JsonConverter} with
315
+ * supplied or default options.
316
+ * @param options - Optional partial {@link IJsonConverterOptions | options}
317
+ * to configure the converter.
318
+ */
319
+ public constructor(options?: Partial<IJsonConverterOptions>) {
320
+ const editor = converterOptionsToEditor(options).orThrow();
321
+ super(editor);
322
+ }
323
+
324
+ /**
325
+ * Creates a new {@link JsonConverter | JsonConverter}.
326
+ * @param options - Optional partial {@link IJsonConverterOptions | options}
327
+ * to configure the converter.
328
+ * @returns `Success` with a new {@link JsonConverter | JsonConverter}, or `Failure`
329
+ * with an informative message if an error occurs.
330
+ */
331
+ public static create(options?: Partial<IJsonConverterOptions>): Result<JsonConverter> {
332
+ return captureResult(() => new JsonConverter(options));
333
+ }
334
+ }
335
+
336
+ /**
337
+ * Initialization options for a {@link TemplatedJsonConverter | TemplatedJsonConverter}.
338
+ * @public
339
+ */
340
+ export type TemplatedJsonConverterOptions = Omit<
341
+ IJsonConverterOptions,
342
+ 'useNameTemplates' | 'useValueTemplates' | 'useMultiValueTemplateNames'
343
+ >;
344
+
345
+ /**
346
+ * An \@fgv/ts-utils `Converter` from `unknown` to type-safe JSON
347
+ * with mustache template rendering and multi-value property name rules enabled
348
+ * regardless of initial context.
349
+ * @public
350
+ */
351
+ export class TemplatedJsonConverter extends JsonEditorConverter {
352
+ /**
353
+ * Default {@link IJsonConverterOptions | JSON converter options}
354
+ * to enable templated conversion.
355
+ */
356
+ public static readonly templateOptions: Partial<IJsonConverterOptions> = {
357
+ useNameTemplates: true,
358
+ useValueTemplates: true,
359
+ useMultiValueTemplateNames: true,
360
+ useConditionalNames: false,
361
+ flattenUnconditionalValues: false
362
+ };
363
+
364
+ /**
365
+ * Constructs a new {@link TemplatedJsonConverter | TemplatedJsonConverter} with
366
+ * supplied or default options.
367
+ * @param options - Optional partial {@link TemplatedJsonConverterOptions | options}
368
+ * to configure the converter.
369
+ */
370
+ public constructor(options?: Partial<TemplatedJsonConverterOptions>) {
371
+ options = { ...options, ...TemplatedJsonConverter.templateOptions };
372
+ const editor = converterOptionsToEditor(options).orThrow();
373
+ super(editor);
374
+ }
375
+
376
+ /**
377
+ * Constructs a new {@link TemplatedJsonConverter | TemplatedJsonConverter} with
378
+ * supplied or default options.
379
+ * @param options - Optional partial {@link TemplatedJsonConverterOptions | options}
380
+ * to configure the converter.
381
+ */
382
+ public static create(options?: Partial<TemplatedJsonConverterOptions>): Result<JsonConverter> {
383
+ return captureResult(() => new TemplatedJsonConverter(options));
384
+ }
385
+ }
386
+
387
+ /**
388
+ * Options for a {@link ConditionalJsonConverter | ConditionalJsonConverter}.
389
+ * @public
390
+ */
391
+ export type ConditionalJsonConverterOptions = Omit<TemplatedJsonConverterOptions, 'useConditionalNames'>;
392
+
393
+ /**
394
+ * An \@fgv/ts-utils `Converter` from `unknown` to type-safe JSON with mustache
395
+ * template rendering, multi-value property name and conditional property
396
+ * name rules enabled regardless of initial context.
397
+ * @public
398
+ */
399
+ export class ConditionalJsonConverter extends JsonEditorConverter {
400
+ /**
401
+ * Default {@link IJsonConverterOptions | JSON converter options}
402
+ * to enable conditional conversion.
403
+ */
404
+ public static readonly conditionalOptions: Partial<IJsonConverterOptions> = {
405
+ ...TemplatedJsonConverter.templateOptions,
406
+ useConditionalNames: true,
407
+ flattenUnconditionalValues: true
408
+ };
409
+
410
+ /**
411
+ * Constructs a new {@link ConditionalJsonConverter | ConditionalJsonConverter} with supplied or
412
+ * default options.
413
+ * @param options - Optional partial {@link ConditionalJsonConverterOptions | configuration or context}
414
+ * for the converter.
415
+ */
416
+ public constructor(options?: Partial<ConditionalJsonConverterOptions>) {
417
+ options = { ...options, ...ConditionalJsonConverter.conditionalOptions };
418
+ const editor = converterOptionsToEditor(options).orThrow();
419
+ super(editor);
420
+ }
421
+
422
+ /**
423
+ * Constructs a new {@link ConditionalJsonConverter | ConditionalJsonConverter} with supplied or
424
+ * default options.
425
+ * @param options - Optional partial {@link ConditionalJsonConverterOptions | configuration or context}
426
+ * for the converter.
427
+ */
428
+ public static create(options?: Partial<ConditionalJsonConverterOptions>): Result<JsonConverter> {
429
+ return captureResult(() => new ConditionalJsonConverter(options));
430
+ }
431
+ }
432
+
433
+ /**
434
+ * Initialization options for a {@link RichJsonConverter | RichJsonConverter}.
435
+ * @public
436
+ */
437
+ export type RichJsonConverterOptions = Omit<ConditionalJsonConverterOptions, 'useReferences'>;
438
+
439
+ /**
440
+ * A \@fgv/ts-utils `Converter` from `unknown` to type-safe JSON with mustache
441
+ * template rendering, multi-value property name, conditional property
442
+ * name, and external reference rules enabled regardless of initial context.
443
+ * @public
444
+ */
445
+ export class RichJsonConverter extends JsonEditorConverter {
446
+ /**
447
+ * Default {@link IJsonConverterOptions | JSON converter options}
448
+ * to enable rich conversion.
449
+ */
450
+ public static readonly richOptions: Partial<IJsonConverterOptions> = {
451
+ ...ConditionalJsonConverter.conditionalOptions,
452
+ useReferences: true
453
+ };
454
+
455
+ /**
456
+ * Constructs a new {@link RichJsonConverter | RichJsonConverter} with supplied or
457
+ * default options.
458
+ * @param options - Optional partial {@link RichJsonConverterOptions | configuration or context}
459
+ * for the converter.
460
+ */
461
+ public constructor(options?: Partial<RichJsonConverterOptions>) {
462
+ options = { ...options, ...RichJsonConverter.richOptions };
463
+ const editor = converterOptionsToEditor(options).orThrow();
464
+ super(editor);
465
+ }
466
+
467
+ /**
468
+ * Constructs a new {@link RichJsonConverter | RichJsonConverter} with supplied or
469
+ * default options
470
+ * @param options - Optional partial {@link RichJsonConverterOptions | configuration or context}
471
+ * for the converter.
472
+ */
473
+ public static create(options?: Partial<RichJsonConverterOptions>): Result<JsonConverter> {
474
+ return captureResult(() => new RichJsonConverter(options));
475
+ }
476
+ }