@fgv/ts-json 5.0.1-8 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,315 @@
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
+ import { captureResult, fail, failWithDetail, mapResults, recordToMap, succeed, succeedWithDetail } from '@fgv/ts-utils';
23
+ import { isJsonObject } from '@fgv/ts-json-base';
24
+ import { JsonEditor } from './jsonEditor';
25
+ /**
26
+ * Policy object responsible for validating or correcting
27
+ * keys in a {@link IJsonReferenceMap | reference map}.
28
+ * @public
29
+ */
30
+ export class ReferenceMapKeyPolicy {
31
+ /**
32
+ * Constructs a new {@link ReferenceMapKeyPolicy | ReferenceMapKeyPolicy}.
33
+ * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options}
34
+ * used to construct the {@link ReferenceMapKeyPolicy}.
35
+ * @param isValid - An optional predicate to test a supplied key for validity.
36
+ */
37
+ constructor(options, isValid) {
38
+ this._defaultOptions = options;
39
+ this._isValid = isValid !== null && isValid !== void 0 ? isValid : ReferenceMapKeyPolicy.defaultKeyPredicate;
40
+ }
41
+ /**
42
+ * The static default key name validation predicate rejects keys that contain
43
+ * mustache templates or which start with the default conditional prefix
44
+ * `'?'`.
45
+ * @param key - The key to test.
46
+ * @returns `true` if the key is valid, `false` otherwise.
47
+ */
48
+ static defaultKeyPredicate(key) {
49
+ return key.length > 0 && !key.includes('{{') && !key.startsWith('?');
50
+ }
51
+ /**
52
+ * Determines if a supplied key and item are valid according to the current policy.
53
+ * @param key - The key to be tested.
54
+ * @param item - The item to be tested.
55
+ * @returns `true` if the key and value are valid, `false` otherwise.
56
+ */
57
+ isValid(key, item) {
58
+ return this._isValid(key, item);
59
+ }
60
+ /**
61
+ * Determines if a supplied key and item are valid according to the current policy.
62
+ * @param key - The key to be tested.
63
+ * @param item - The item to be tested.
64
+ * @returns `Success` with the key if valid, `Failure` with an error message if invalid.
65
+ */
66
+ validate(key, item, __options) {
67
+ return this.isValid(key, item) ? succeed(key) : fail(`${key}: invalid key`);
68
+ }
69
+ /**
70
+ * Validates an array of entries using the validation rules for this policy.
71
+ * @param items - The array of entries to be validated.
72
+ * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to control
73
+ * validation.
74
+ * @returns `Success` with an array of validated entries, or `Failure` with an error message
75
+ * if validation fails.
76
+ */
77
+ validateItems(items, options) {
78
+ return mapResults(items.map((item) => {
79
+ return this.validate(...item, options).onSuccess((valid) => {
80
+ return succeed([valid, item[1]]);
81
+ });
82
+ }));
83
+ }
84
+ /**
85
+ * Validates a `Map\<string, T\>` using the validation rules for this policy.
86
+ * @param items - The `Map\<string, T\>` to be validated.
87
+ * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to control
88
+ * validation.
89
+ * @returns `Success` with a new `Map\<string, T\>`, or `Failure` with an error message
90
+ * if validation fails.
91
+ */
92
+ validateMap(map, options) {
93
+ return this.validateItems(Array.from(map.entries()), options).onSuccess((valid) => {
94
+ return captureResult(() => new Map(valid));
95
+ });
96
+ }
97
+ }
98
+ /**
99
+ * A {@link PrefixKeyPolicy | PrefixKeyPolicy} enforces that all keys start with a supplied
100
+ * prefix, optionally adding the prefix as necessary.
101
+ * @public
102
+ */
103
+ export class PrefixKeyPolicy extends ReferenceMapKeyPolicy {
104
+ /**
105
+ * Constructs a new {@link PrefixKeyPolicy | PrefixKeyPolicy}.
106
+ * @param prefix - The string prefix to be enforced or applied.
107
+ * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to
108
+ * configure the policy.
109
+ */
110
+ constructor(prefix, options) {
111
+ super(options);
112
+ this.prefix = prefix;
113
+ }
114
+ /**
115
+ * Determines if a key is valid according to policy.
116
+ * @param key - The key to be tested.
117
+ * @param __item - The item to be tested.
118
+ * @returns `true` if the key starts with the expected prefix, `false` otherwise.
119
+ */
120
+ isValid(key, __item) {
121
+ return (key.startsWith(this.prefix) && key !== this.prefix && ReferenceMapKeyPolicy.defaultKeyPredicate(key));
122
+ }
123
+ /**
124
+ * Determines if a key is valid according to policy, optionally coercing to a valid value by
125
+ * adding the required prefix.
126
+ * @param key - The key to be tested.
127
+ * @param item - The item which corresponds to the key.
128
+ * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to guide
129
+ * validation.
130
+ * @returns `Success` with a valid key name if the supplied key is valid or if `makeValid` is set
131
+ * in the policy options. Returns `Failure` with an error message if an error occurs.
132
+ */
133
+ validate(key, item, options) {
134
+ var _a;
135
+ /* c8 ignore next */
136
+ const makeValid = ((_a = (options !== null && options !== void 0 ? options : this._defaultOptions)) === null || _a === void 0 ? void 0 : _a.makeValid) === true;
137
+ if (this.isValid(key, item)) {
138
+ return succeed(key);
139
+ }
140
+ else if (makeValid && ReferenceMapKeyPolicy.defaultKeyPredicate(key)) {
141
+ return succeed(`${this.prefix}${key}`);
142
+ }
143
+ return fail(`${key}: invalid key`);
144
+ }
145
+ }
146
+ /**
147
+ * Abstract base class with common functionality for simple
148
+ * {@link IJsonReferenceMap | reference map} implementations.
149
+ * @public
150
+ */
151
+ export class SimpleJsonMapBase {
152
+ /**
153
+ * Constructs a new {@link SimpleJsonMap | SimpleJsonMap}.
154
+ * @param values - Initial values for the map.
155
+ * @param context - An optional {@link IJsonContext | IJsonContext} used for any conversions
156
+ * involving items in this map.
157
+ * @param keyPolicy - The {@link ReferenceMapKeyPolicy | key policy} to use for this map.
158
+ * @internal
159
+ */
160
+ constructor(values, context, keyPolicy) {
161
+ values = SimpleJsonMapBase._toMap(values).orThrow();
162
+ this._keyPolicy = keyPolicy !== null && keyPolicy !== void 0 ? keyPolicy : new ReferenceMapKeyPolicy();
163
+ this._values = this._keyPolicy.validateMap(values).orThrow();
164
+ this._context = context;
165
+ }
166
+ /**
167
+ * Returns a `Map\<string, T\>` derived from a supplied {@link MapOrRecord | MapOrRecord}
168
+ * @param values - The {@link MapOrRecord | MapOrRecord} to be returned as a map.
169
+ * @returns `Success` with the corresponding `Map\<string, T\>` or `Failure` with a
170
+ * message if an error occurs.
171
+ * @internal
172
+ */
173
+ static _toMap(values) {
174
+ if (values === undefined) {
175
+ return captureResult(() => new Map());
176
+ }
177
+ else if (!(values instanceof Map)) {
178
+ return recordToMap(values, (__k, v) => succeed(v));
179
+ }
180
+ return succeed(values);
181
+ }
182
+ /**
183
+ * Determine if a key might be valid for this map but does not determine if key actually
184
+ * exists. Allows key range to be constrained.
185
+ * @param key - key to be tested
186
+ * @returns `true` if the key is in the valid range, `false` otherwise.
187
+ */
188
+ keyIsInRange(key) {
189
+ return this._keyPolicy.isValid(key);
190
+ }
191
+ /**
192
+ * Determines if an entry with the specified key actually exists in the map.
193
+ * @param key - key to be tested
194
+ * @returns `true` if an object with the specified key exists, `false` otherwise.
195
+ */
196
+ has(key) {
197
+ return this._values.has(key);
198
+ }
199
+ /**
200
+ * Gets a `JsonObject` specified by key.
201
+ * @param key - key of the object to be retrieved
202
+ * @param context - optional {@link IJsonContext | JSON context} used to format the
203
+ * returned object.
204
+ * @returns {@link ts-utils#Success | `Success`} with the formatted object if successful.
205
+ * {@link ts-utils#Failure | `Failure`} with detail 'unknown' if no such object exists,
206
+ * or {@link ts-utils#Failure | `Failure`} with detail 'error' if the object was found
207
+ * but could not be formatted.
208
+ */
209
+ getJsonObject(key, context) {
210
+ return this.getJsonValue(key, context).onSuccess((jv) => {
211
+ if (!isJsonObject(jv)) {
212
+ return failWithDetail(`${key}: not an object`, 'error');
213
+ }
214
+ return succeedWithDetail(jv);
215
+ });
216
+ }
217
+ }
218
+ /**
219
+ * A {@link SimpleJsonMap | SimpleJsonMap } presents a view of a simple map
220
+ * of JSON values.
221
+ * @public
222
+ */
223
+ export class SimpleJsonMap extends SimpleJsonMapBase {
224
+ /**
225
+ * Constructs a new {@link SimpleJsonMap | SimpleJsonMap} from the supplied objects
226
+ * @param values - A string-keyed `Map` or `Record` of the `JsonValue`
227
+ * to be returned.
228
+ * @param context - Optional {@link IJsonContext | IJsonContext} used to format returned values.
229
+ * @param options - Optional {@link ISimpleJsonMapOptions | ISimpleJsonMapOptions} for initialization.
230
+ * @public
231
+ */
232
+ constructor(values, context, options) {
233
+ super(values, context, options === null || options === void 0 ? void 0 : options.keyPolicy);
234
+ this._editor = options === null || options === void 0 ? void 0 : options.editor;
235
+ }
236
+ /**
237
+ * Creates a new {@link SimpleJsonMap | SimpleJsonMap} from the supplied objects
238
+ * @param values - A string-keyed `Map` or `Record` of the `JsonValue`
239
+ * to be returned.
240
+ * @param context - Optional {@link IJsonContext | IJsonContext} used to format returned values.
241
+ * @param options - Optional {@link ISimpleJsonMapOptions | ISimpleJsonMapOptions} for initialization.
242
+ * @returns `Success` with a {@link SimpleJsonMap | SimpleJsonMap} or `Failure` with a message if
243
+ * an error occurs.
244
+ */
245
+ static createSimple(values, context, options) {
246
+ return captureResult(() => new SimpleJsonMap(values, context, options));
247
+ }
248
+ /**
249
+ * Gets a `JsonValue` specified by key.
250
+ * @param key - key of the value to be retrieved
251
+ * @param context - Optional {@link IJsonContext | JSON context} used to format the value
252
+ * @returns Success with the formatted object if successful. Failure with detail 'unknown'
253
+ * if no such object exists, or failure with detail 'error' if the object was found but
254
+ * could not be formatted.
255
+ */
256
+ getJsonValue(key, context) {
257
+ context = context !== null && context !== void 0 ? context : this._context;
258
+ const value = this._values.get(key);
259
+ if (!value) {
260
+ return failWithDetail(`${key}: JSON value not found`, 'unknown');
261
+ }
262
+ return this._clone(value, context);
263
+ }
264
+ /**
265
+ * @internal
266
+ */
267
+ _clone(value, context) {
268
+ if (!this._editor) {
269
+ const result = JsonEditor.create();
270
+ /* c8 ignore next 3 - nearly impossible to reproduce */
271
+ if (result.isFailure()) {
272
+ return failWithDetail(result.message, 'error');
273
+ }
274
+ this._editor = result.value;
275
+ }
276
+ return this._editor.clone(value, context).withFailureDetail('error');
277
+ }
278
+ }
279
+ /**
280
+ * A {@link PrefixedJsonMap | PrefixedJsonMap} enforces a supplied prefix for all contained values,
281
+ * optionally adding the prefix as necessary (default `true`).
282
+ * @public
283
+ */
284
+ export class PrefixedJsonMap extends SimpleJsonMap {
285
+ /**
286
+ * Constructs a new {@link PrefixedJsonMap | PrefixedJsonMap} from the supplied values
287
+ * @param prefix - A string prefix to be enforced for and added to key names as necessary
288
+ * @param values - A string-keyed Map or Record of the `JsonValue` to be returned
289
+ * @param context - Optional {@link IJsonContext | JSON Context} used to format returned values
290
+ * @param editor - Optional {@link JsonEditor | JsonEditor} used to format returned values
291
+ * @public
292
+ */
293
+ constructor(values, context, options) {
294
+ super(values, context, options);
295
+ }
296
+ static createPrefixed(prefixOptions, values, context, editor) {
297
+ return captureResult(() => new PrefixedJsonMap(values, context, { keyPolicy: this._toPolicy(prefixOptions), editor }));
298
+ }
299
+ /**
300
+ * Constructs a new {@link PrefixKeyPolicy | PrefixKeyPolicy} from a supplied prefix
301
+ * or set of {@link IKeyPrefixOptions | prefix options}.
302
+ * @param prefixOptions - The prefix or {@link IKeyPrefixOptions | prefix options} or options
303
+ * for the new policy.
304
+ * @returns A new {@link ReferenceMapKeyPolicy | ReferenceMapKeyPolicy} which enforces the
305
+ * supplied prefix or options.
306
+ * @internal
307
+ */
308
+ static _toPolicy(prefixOptions) {
309
+ if (typeof prefixOptions === 'string') {
310
+ return new PrefixKeyPolicy(prefixOptions, { makeValid: true });
311
+ }
312
+ return new PrefixKeyPolicy(prefixOptions.prefix, { makeValid: prefixOptions.addPrefix !== false });
313
+ }
314
+ }
315
+ //# sourceMappingURL=jsonReferenceMap.js.map
@@ -0,0 +1,163 @@
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
+ import { isJsonObject } from '@fgv/ts-json-base';
23
+ import { captureResult, failWithDetail, succeedWithDetail } from '@fgv/ts-utils';
24
+ import { JsonEditorRuleBase } from '../jsonEditorRule';
25
+ /**
26
+ * The {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule} evaluates
27
+ * properties with conditional keys, omitting non-matching keys and merging keys that match,
28
+ * or default keys only if no other keys match.
29
+ *
30
+ * The default syntax for a conditional key is:
31
+ * "?value1=value2" - matches if value1 and value2 are the same, is ignored otherwise.
32
+ * "?value" - matches if value is a non-empty, non-whitespace string. Is ignored otherwise.
33
+ * "?default" - matches only if no other conditional blocks in the same object were matched.
34
+ * @public
35
+ */
36
+ export class ConditionalJsonEditorRule extends JsonEditorRuleBase {
37
+ /**
38
+ * Creates a new {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.
39
+ * @param options - Optional {@link EditorRules.IConditionalJsonRuleOptions | configuration options}
40
+ * used for this rule.
41
+ */
42
+ constructor(options) {
43
+ super();
44
+ this._options = options;
45
+ }
46
+ /**
47
+ * Creates a new {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.
48
+ * @param options - Optional {@link EditorRules.IConditionalJsonRuleOptions | configuration options}
49
+ * used for this rule.
50
+ */
51
+ static create(options) {
52
+ return captureResult(() => new ConditionalJsonEditorRule(options));
53
+ }
54
+ /**
55
+ * Evaluates a property for conditional application.
56
+ * @param key - The key of the property to be considered
57
+ * @param value - The `JsonValue` of the property to be considered.
58
+ * @param state - The {@link JsonEditorState | editor state} for the object being edited.
59
+ * @returns Returns `Success` with detail `'deferred'` and a
60
+ * {@link EditorRules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject}.
61
+ * for a matching, default or unconditional key. Returns `Failure` with detail `'ignore'` for
62
+ * a non-matching conditional, or with detail `'error'` if an error occurs. Otherwise
63
+ * fails with detail `'inapplicable'`.
64
+ */
65
+ editProperty(key, value, state) {
66
+ var _a;
67
+ const result = this._tryParseCondition(key, state).onSuccess((deferred) => {
68
+ if (isJsonObject(value)) {
69
+ const rtrn = Object.assign(Object.assign({}, deferred), { value });
70
+ return succeedWithDetail(rtrn, 'deferred');
71
+ }
72
+ return failWithDetail(`${key}: conditional body must be object`, 'error');
73
+ });
74
+ if (result.isFailure() && result.detail === 'error') {
75
+ return state.failValidation('invalidPropertyName', result.message, (_a = this._options) === null || _a === void 0 ? void 0 : _a.validation);
76
+ }
77
+ return result;
78
+ }
79
+ /**
80
+ * Finalizes any deferred conditional properties. If the only deferred property is
81
+ * default, that property is emitted. Otherwise all matching properties are emitted.
82
+ * @param finalized - The deferred properties to be considered for merge.
83
+ * @param __state - The {@link JsonEditorState | editor state} for the object
84
+ * being edited.
85
+ */
86
+ finalizeProperties(finalized, __state) {
87
+ let toMerge = finalized;
88
+ if (finalized.length > 1) {
89
+ if (finalized.find((o) => o.matchType === 'match') !== undefined) {
90
+ toMerge = finalized.filter((o) => o.matchType === 'match' || o.matchType === 'unconditional');
91
+ }
92
+ }
93
+ return succeedWithDetail(toMerge.map((o) => o.value).filter(isJsonObject), 'edited');
94
+ }
95
+ /**
96
+ * Determines if a given property key is conditional. Derived classes can override this
97
+ * method to use a different format for conditional properties.
98
+ * @param value - The `JsonValue` of the property to be considered.
99
+ * @param state - The {@link JsonEditorState | editor state} for the object being edited.
100
+ * @returns `Success` with detail `'deferred'` and a
101
+ * {@link EditorRules.IConditionalJsonKeyResult | IConditionalJsonKeyResult} describing the
102
+ * match for a default or matching conditional property. Returns `Failure` with detail `'ignore'`
103
+ * for a non-matching conditional property. Fails with detail `'error'` if an error occurs
104
+ * or with detail `'inapplicable'` if the key does not represent a conditional property.
105
+ * @public
106
+ */
107
+ _tryParseCondition(key, state) {
108
+ var _a, _b;
109
+ if (key.startsWith('?')) {
110
+ // ignore everything after any #
111
+ key = key.split('#')[0].trim();
112
+ if (key === '?default') {
113
+ return succeedWithDetail({ matchType: 'default' }, 'deferred');
114
+ }
115
+ const parts = key.substring(1).split(/(=|>=|<=|>|<|!=)/);
116
+ if (parts.length === 3) {
117
+ if (!this._compare(parts[0].trim(), parts[2].trim(), parts[1])) {
118
+ return failWithDetail(`Condition ${key} does not match`, 'ignore');
119
+ }
120
+ return succeedWithDetail({ matchType: 'match' }, 'deferred');
121
+ }
122
+ else if (parts.length === 1) {
123
+ if (parts[0].trim().length === 0) {
124
+ return failWithDetail(`Condition ${key} does not match`, 'ignore');
125
+ }
126
+ return succeedWithDetail({ matchType: 'match' }, 'deferred');
127
+ }
128
+ const message = `Malformed condition token ${key}`;
129
+ return state.failValidation('invalidPropertyName', message, (_a = this._options) === null || _a === void 0 ? void 0 : _a.validation);
130
+ }
131
+ else if (((_b = this._options) === null || _b === void 0 ? void 0 : _b.flattenUnconditionalValues) !== false && key.startsWith('!')) {
132
+ return succeedWithDetail({ matchType: 'unconditional' }, 'deferred');
133
+ }
134
+ return failWithDetail('inapplicable', 'inapplicable');
135
+ }
136
+ /**
137
+ * Compares two strings using a supplied operator.
138
+ * @param left - The first string to be compared.
139
+ * @param right - The second string to be compared.
140
+ * @param operator - The operator to be applied.
141
+ * @returns `true` if the condition is met, `false` otherwise.
142
+ * @internal
143
+ */
144
+ _compare(left, right, operator) {
145
+ switch (operator) {
146
+ case '=':
147
+ return left === right;
148
+ case '>':
149
+ return left > right;
150
+ case '<':
151
+ return left < right;
152
+ case '>=':
153
+ return left >= right;
154
+ case '<=':
155
+ return left <= right;
156
+ case '!=':
157
+ return left !== right;
158
+ }
159
+ /* c8 ignore next 2 - unreachable */
160
+ return false;
161
+ }
162
+ }
163
+ //# sourceMappingURL=conditional.js.map
@@ -0,0 +1,26 @@
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
+ export * from './conditional';
23
+ export * from './multivalue';
24
+ export * from './references';
25
+ export * from './templates';
26
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,137 @@
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
+ import { allSucceed, captureResult, failWithDetail, succeed, succeedWithDetail } from '@fgv/ts-utils';
23
+ import { JsonEditorRuleBase } from '../jsonEditorRule';
24
+ /**
25
+ * The {@link EditorRules.MultiValueJsonEditorRule | Multi-Value JSON editor rule}
26
+ * expands matching keys multiple times, projecting the value into the template
27
+ * context for any child objects rendered by the rule.
28
+ *
29
+ * The default syntax for a multi-value key is:
30
+ * "[[var]]=value1,value2,value3"
31
+ * Where "var" is the name of the variable that will be passed to
32
+ * child template resolution, and "value1,value2,value3" is a
33
+ * comma-separated list of values to be expanded.
34
+ * @public
35
+ */
36
+ export class MultiValueJsonEditorRule extends JsonEditorRuleBase {
37
+ /**
38
+ * Creates a new {@link EditorRules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.
39
+ * @param options - Optional {@link IJsonEditorOptions | configuration options}.
40
+ */
41
+ constructor(options) {
42
+ super();
43
+ this._options = options;
44
+ }
45
+ /**
46
+ * Creates a new {@link EditorRules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.
47
+ * @param options - Optional {@link IJsonEditorOptions | configuration options}.
48
+ */
49
+ static create(options) {
50
+ return captureResult(() => new MultiValueJsonEditorRule(options));
51
+ }
52
+ /**
53
+ * Evaluates a property for multi-value expansion.
54
+ * @param key - The key of the property to be considered
55
+ * @param value - The `JsonValue` of the property to be considered.
56
+ * @param state - The {@link JsonEditorState | editor state} for the object being edited.
57
+ * @returns `Success` with an object containing the fully-resolved child values to be merged for
58
+ * matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs or
59
+ * with detail `'inapplicable'` if the property key is not a conditional property.
60
+ */
61
+ editProperty(key, value, state) {
62
+ const json = {};
63
+ const result = this._tryParse(key, state).onSuccess((parts) => {
64
+ return allSucceed(parts.propertyValues.map((pv) => {
65
+ return this._deriveContext(state, [parts.propertyVariable, pv]).onSuccess((ctx) => {
66
+ return state.editor.clone(value, ctx).onSuccess((cloned) => {
67
+ json[pv] = cloned;
68
+ return succeedWithDetail(cloned);
69
+ });
70
+ });
71
+ }), json)
72
+ .onSuccess(() => {
73
+ if (parts.asArray) {
74
+ const arrayRtrn = {};
75
+ arrayRtrn[parts.propertyVariable] = Array.from(Object.values(json));
76
+ return succeed(arrayRtrn);
77
+ }
78
+ return succeed(json);
79
+ })
80
+ .withFailureDetail('error');
81
+ });
82
+ if (result.isFailure() && result.detail === 'error') {
83
+ return state.failValidation('invalidPropertyName', result.message);
84
+ }
85
+ return result;
86
+ }
87
+ /**
88
+ * Extends the {@link IJsonContext | current context} with a supplied state and values.
89
+ * @param state - The {@link JsonEditorState | editor state} for the object being edited.
90
+ * @param values - An array of {@link VariableValue | VariableValue} to be added to the
91
+ * context.
92
+ * @returns The extended {@link IJsonContext | context}.
93
+ * @public
94
+ */
95
+ _deriveContext(state, ...values) {
96
+ var _a;
97
+ return state.extendContext((_a = this._options) === null || _a === void 0 ? void 0 : _a.context, { vars: values });
98
+ }
99
+ /**
100
+ * Determines if a given property key is multi-value. Derived classes can override this
101
+ * method to use a different format for multi-value properties.
102
+ * @param value - The `JsonValue` of the property to be considered.
103
+ * @param state - The {@link JsonEditorState | editor state} for the object being edited.
104
+ * @returns `Success` with detail `'deferred'` and an
105
+ * {@link EditorRules.IMultiValuePropertyParts | IMultiValuePropertyParts}
106
+ * describing the match for matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs
107
+ * or with detail `'inapplicable'` if the key does not represent a multi-value property.
108
+ * @public
109
+ */
110
+ _tryParse(token, state) {
111
+ var _a;
112
+ let parts = [];
113
+ let asArray = false;
114
+ if (token.startsWith('[[')) {
115
+ parts = token.substring(2).split(']]=');
116
+ asArray = true;
117
+ }
118
+ else if (token.startsWith('*')) {
119
+ parts = token.substring(1).split('=');
120
+ asArray = false;
121
+ }
122
+ else {
123
+ return failWithDetail(token, 'inapplicable');
124
+ }
125
+ if (parts.length !== 2) {
126
+ const message = `Malformed multi-value property: ${token}`;
127
+ return state.failValidation('invalidPropertyName', message, (_a = this._options) === null || _a === void 0 ? void 0 : _a.validation);
128
+ }
129
+ if (parts[1].includes('{{')) {
130
+ return failWithDetail('unresolved template', 'inapplicable');
131
+ }
132
+ const propertyVariable = parts[0];
133
+ const propertyValues = parts[1].split(',');
134
+ return succeedWithDetail({ token, propertyVariable, propertyValues, asArray });
135
+ }
136
+ }
137
+ //# sourceMappingURL=multivalue.js.map