@fgv/ts-json 2.1.0 → 2.1.1-alpha.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.
Files changed (71) hide show
  1. package/CHANGELOG.json +9 -3
  2. package/CHANGELOG.md +5 -3
  3. package/dist/ts-json.d.ts +3 -198
  4. package/dist/tsdoc-metadata.json +1 -1
  5. package/lib/index.d.ts +0 -1
  6. package/lib/index.d.ts.map +1 -1
  7. package/lib/index.js +0 -1
  8. package/lib/index.js.map +1 -1
  9. package/lib/packlets/context/compositeJsonMap.d.ts +1 -1
  10. package/lib/packlets/context/compositeJsonMap.d.ts.map +1 -1
  11. package/lib/packlets/context/compositeJsonMap.js +2 -2
  12. package/lib/packlets/context/compositeJsonMap.js.map +1 -1
  13. package/lib/packlets/context/contextHelpers.js.map +1 -1
  14. package/lib/packlets/context/jsonContext.d.ts +1 -1
  15. package/lib/packlets/context/jsonContext.d.ts.map +1 -1
  16. package/lib/packlets/context/jsonContext.js.map +1 -1
  17. package/lib/packlets/converters/converters.d.ts +1 -1
  18. package/lib/packlets/converters/converters.d.ts.map +1 -1
  19. package/lib/packlets/converters/converters.js.map +1 -1
  20. package/lib/packlets/converters/index.d.ts +1 -2
  21. package/lib/packlets/converters/index.d.ts.map +1 -1
  22. package/lib/packlets/converters/index.js +1 -3
  23. package/lib/packlets/converters/index.js.map +1 -1
  24. package/lib/packlets/converters/jsonConverter.d.ts +1 -1
  25. package/lib/packlets/converters/jsonConverter.d.ts.map +1 -1
  26. package/lib/packlets/converters/jsonConverter.js +2 -2
  27. package/lib/packlets/converters/jsonConverter.js.map +1 -1
  28. package/lib/packlets/editor/common.d.ts +1 -1
  29. package/lib/packlets/editor/common.d.ts.map +1 -1
  30. package/lib/packlets/editor/common.js.map +1 -1
  31. package/lib/packlets/editor/jsonEditor.d.ts +1 -1
  32. package/lib/packlets/editor/jsonEditor.d.ts.map +1 -1
  33. package/lib/packlets/editor/jsonEditor.js +9 -9
  34. package/lib/packlets/editor/jsonEditor.js.map +1 -1
  35. package/lib/packlets/editor/jsonEditorRule.d.ts +1 -1
  36. package/lib/packlets/editor/jsonEditorRule.d.ts.map +1 -1
  37. package/lib/packlets/editor/jsonEditorRule.js.map +1 -1
  38. package/lib/packlets/editor/jsonEditorState.d.ts +1 -1
  39. package/lib/packlets/editor/jsonEditorState.d.ts.map +1 -1
  40. package/lib/packlets/editor/jsonEditorState.js.map +1 -1
  41. package/lib/packlets/editor/jsonReferenceMap.d.ts +1 -1
  42. package/lib/packlets/editor/jsonReferenceMap.d.ts.map +1 -1
  43. package/lib/packlets/editor/jsonReferenceMap.js +2 -2
  44. package/lib/packlets/editor/jsonReferenceMap.js.map +1 -1
  45. package/lib/packlets/editor/rules/conditional.d.ts +1 -1
  46. package/lib/packlets/editor/rules/conditional.d.ts.map +1 -1
  47. package/lib/packlets/editor/rules/conditional.js +3 -3
  48. package/lib/packlets/editor/rules/conditional.js.map +1 -1
  49. package/lib/packlets/editor/rules/multivalue.d.ts +1 -1
  50. package/lib/packlets/editor/rules/multivalue.d.ts.map +1 -1
  51. package/lib/packlets/editor/rules/multivalue.js.map +1 -1
  52. package/lib/packlets/editor/rules/references.d.ts +1 -1
  53. package/lib/packlets/editor/rules/references.d.ts.map +1 -1
  54. package/lib/packlets/editor/rules/references.js +3 -3
  55. package/lib/packlets/editor/rules/references.js.map +1 -1
  56. package/lib/packlets/editor/rules/templates.d.ts +1 -1
  57. package/lib/packlets/editor/rules/templates.d.ts.map +1 -1
  58. package/lib/packlets/editor/rules/templates.js.map +1 -1
  59. package/package.json +25 -22
  60. package/lib/packlets/converters/file.d.ts +0 -83
  61. package/lib/packlets/converters/file.d.ts.map +0 -1
  62. package/lib/packlets/converters/file.js +0 -162
  63. package/lib/packlets/converters/file.js.map +0 -1
  64. package/lib/packlets/json/common.d.ts +0 -85
  65. package/lib/packlets/json/common.d.ts.map +0 -1
  66. package/lib/packlets/json/common.js +0 -123
  67. package/lib/packlets/json/common.js.map +0 -1
  68. package/lib/packlets/json/index.d.ts +0 -2
  69. package/lib/packlets/json/index.d.ts.map +0 -1
  70. package/lib/packlets/json/index.js +0 -39
  71. package/lib/packlets/json/index.js.map +0 -1
@@ -23,7 +23,7 @@
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.PrefixedJsonMap = exports.SimpleJsonMap = exports.SimpleJsonMapBase = exports.PrefixKeyPolicy = exports.ReferenceMapKeyPolicy = void 0;
25
25
  const ts_utils_1 = require("@fgv/ts-utils");
26
- const json_1 = require("../json");
26
+ const ts_json_base_1 = require("@fgv/ts-json-base");
27
27
  const jsonEditor_1 = require("./jsonEditor");
28
28
  /**
29
29
  * Policy object responsible for validating or correcting
@@ -214,7 +214,7 @@ class SimpleJsonMapBase {
214
214
  */
215
215
  getJsonObject(key, context) {
216
216
  return this.getJsonValue(key, context).onSuccess((jv) => {
217
- if (!(0, json_1.isJsonObject)(jv)) {
217
+ if (!(0, ts_json_base_1.isJsonObject)(jv)) {
218
218
  return (0, ts_utils_1.failWithDetail)(`${key}: not an object`, 'error');
219
219
  }
220
220
  return (0, ts_utils_1.succeedWithDetail)(jv);
@@ -1 +1 @@
1
- {"version":3,"file":"jsonReferenceMap.js","sourceRoot":"","sources":["../../../src/packlets/editor/jsonReferenceMap.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAUuB;AAGvB,kCAA8D;AAC9D,6CAA0C;AAc1C;;;;GAIG;AACH,MAAa,qBAAqB;IAWhC;;;;;OAKG;IACH,YACE,OAA+C,EAC/C,OAA4C;QAE5C,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC;QAC/B,IAAI,CAAC,QAAQ,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,qBAAqB,CAAC,mBAAmB,CAAC;IACvE,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,mBAAmB,CAAC,GAAW;QAC3C,OAAO,GAAG,CAAC,MAAM,GAAG,CA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,OAAO,EAAE,OAAO,CAAC,CAAC;aAChD;YACD,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC;SAC7B;QACD,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;IACvE,CAAC;CACF;AA7ED,sCA6EC;AAkBD;;;;GAIG;AACH,MAAa,eAAgB,SAAQ,aAAa;IAChD;;;;;;;OAOG;IACH,YACE,MAA+B,EAC/B,OAAsB,EACtB,OAA+B;QAE/B,KAAK,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IAClC,CAAC;IAgCM,MAAM,CAAC,cAAc,CAC1B,aAAyC,EACzC,MAA+B,EAC/B,OAAsB,EACtB,MAAmB;QAEnB,OAAO,IAAA,wBAAa,EAClB,GAAG,EAAE,CAAC,IAAI,eAAe,CAAC,MAAM,EAAE,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC,CACjG,CAAC;IACJ,CAAC;IAED;;;;;;;;OAQG;IACO,MAAM,CAAC,SAAS,CAAC,aAAyC;QAClE,IAAI,OAAO,aAAa,KAAK,QAAQ,EAAE;YACrC,OAAO,IAAI,eAAe,CAAC,aAAa,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;SAChE;QACD,OAAO,IAAI,eAAe,CAAC,aAAa,CAAC,MAAM,EAAE,EAAE,SAAS,EAAE,aAAa,CAAC,SAAS,KAAK,KAAK,EAAE,CAAC,CAAC;IACrG,CAAC;CACF;AAzED,0CAyEC","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 {\n DetailedResult,\n Result,\n captureResult,\n fail,\n failWithDetail,\n mapResults,\n recordToMap,\n succeed,\n succeedWithDetail\n} from '@fgv/ts-utils';\n\nimport { IJsonContext, IJsonReferenceMap, JsonReferenceMapFailureReason } from '../context';\nimport { JsonObject, JsonValue, isJsonObject } from '../json';\nimport { JsonEditor } from './jsonEditor';\n\n/**\n * Options for creating a {@link ReferenceMapKeyPolicy | ReferenceMapKeyPolicy} object.\n * @public\n */\nexport interface IReferenceMapKeyPolicyValidateOptions {\n /**\n * If `true`, the validator coerces keys to some valid value.\n * If `false`, invalid keys cause an error.\n */\n makeValid?: boolean;\n}\n\n/**\n * Policy object responsible for validating or correcting\n * keys in a {@link IJsonReferenceMap | reference map}.\n * @public\n */\nexport class ReferenceMapKeyPolicy<T> {\n /**\n * @internal\n */\n protected readonly _defaultOptions?: IReferenceMapKeyPolicyValidateOptions;\n\n /**\n * @internal\n */\n protected readonly _isValid: (key: string, item?: T) => boolean;\n\n /**\n * Constructs a new {@link ReferenceMapKeyPolicy | ReferenceMapKeyPolicy}.\n * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options}\n * used to construct the {@link ReferenceMapKeyPolicy}.\n * @param isValid - An optional predicate to test a supplied key for validity.\n */\n public constructor(\n options?: IReferenceMapKeyPolicyValidateOptions,\n isValid?: (key: string, item?: T) => boolean\n ) {\n this._defaultOptions = options;\n this._isValid = isValid ?? ReferenceMapKeyPolicy.defaultKeyPredicate;\n }\n\n /**\n * The static default key name validation predicate rejects keys that contain\n * mustache templates or which start with the default conditional prefix\n * `'?'`.\n * @param key - The key to test.\n * @returns `true` if the key is valid, `false` otherwise.\n */\n public static defaultKeyPredicate(key: string): boolean {\n return key.length > 0 && !key.includes('{{') && !key.startsWith('?');\n }\n\n /**\n * Determines if a supplied key and item are valid according to the current policy.\n * @param key - The key to be tested.\n * @param item - The item to be tested.\n * @returns `true` if the key and value are valid, `false` otherwise.\n */\n public isValid(key: string, item?: T): boolean {\n return this._isValid(key, item);\n }\n\n /**\n * Determines if a supplied key and item are valid according to the current policy.\n * @param key - The key to be tested.\n * @param item - The item to be tested.\n * @returns `Success` with the key if valid, `Failure` with an error message if invalid.\n */\n public validate(key: string, item?: T, __options?: IReferenceMapKeyPolicyValidateOptions): Result<string> {\n return this.isValid(key, item) ? succeed(key) : fail(`${key}: invalid key`);\n }\n\n /**\n * Validates an array of entries using the validation rules for this policy.\n * @param items - The array of entries to be validated.\n * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to control\n * validation.\n * @returns `Success` with an array of validated entries, or `Failure` with an error message\n * if validation fails.\n */\n public validateItems(\n items: [string, T][],\n options?: IReferenceMapKeyPolicyValidateOptions\n ): Result<[string, T][]> {\n return mapResults(\n items.map((item) => {\n return this.validate(...item, options).onSuccess((valid) => {\n return succeed([valid, item[1]]);\n });\n })\n );\n }\n\n /**\n * Validates a `Map\\<string, T\\>` using the validation rules for this policy.\n * @param items - The `Map\\<string, T\\>` to be validated.\n * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to control\n * validation.\n * @returns `Success` with a new `Map\\<string, T\\>`, or `Failure` with an error message\n * if validation fails.\n */\n public validateMap(\n map: Map<string, T>,\n options?: IReferenceMapKeyPolicyValidateOptions\n ): Result<Map<string, T>> {\n return this.validateItems(Array.from(map.entries()), options).onSuccess((valid) => {\n return captureResult(() => new Map(valid));\n });\n }\n}\n\n/**\n * A {@link PrefixKeyPolicy | PrefixKeyPolicy} enforces that all keys start with a supplied\n * prefix, optionally adding the prefix as necessary.\n * @public\n */\nexport class PrefixKeyPolicy<T> extends ReferenceMapKeyPolicy<T> {\n /**\n * The string prefix to be enforced by this policy.\n */\n public readonly prefix: string;\n\n /**\n * Constructs a new {@link PrefixKeyPolicy | PrefixKeyPolicy}.\n * @param prefix - The string prefix to be enforced or applied.\n * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to\n * configure the policy.\n */\n public constructor(prefix: string, options?: IReferenceMapKeyPolicyValidateOptions) {\n super(options);\n this.prefix = prefix;\n }\n\n /**\n * Determines if a key is valid according to policy.\n * @param key - The key to be tested.\n * @param __item - The item to be tested.\n * @returns `true` if the key starts with the expected prefix, `false` otherwise.\n */\n public isValid(key: string, __item?: T): boolean {\n return (\n key.startsWith(this.prefix) && key !== this.prefix && ReferenceMapKeyPolicy.defaultKeyPredicate(key)\n );\n }\n\n /**\n * Determines if a key is valid according to policy, optionally coercing to a valid value by\n * adding the required prefix.\n * @param key - The key to be tested.\n * @param item - The item which corresponds to the key.\n * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to guide\n * validation.\n * @returns `Success` with a valid key name if the supplied key is valid or if `makeValid` is set\n * in the policy options. Returns `Failure` with an error message if an error occurs.\n */\n public validate(key: string, item?: T, options?: IReferenceMapKeyPolicyValidateOptions): Result<string> {\n /* c8 ignore next */\n const makeValid = (options ?? this._defaultOptions)?.makeValid === true;\n if (this.isValid(key, item)) {\n return succeed(key);\n } else if (makeValid && ReferenceMapKeyPolicy.defaultKeyPredicate(key)) {\n return succeed(`${this.prefix}${key}`);\n }\n return fail(`${key}: invalid key`);\n }\n}\n\n/**\n * Type representing either a `Map\\<string, T\\>` or a `Record\\<string, T\\>`.\n * @public\n */\nexport type MapOrRecord<T> = Map<string, T> | Record<string, T>;\n\n/**\n * Abstract base class with common functionality for simple\n * {@link IJsonReferenceMap | reference map} implementations.\n * {@link JsonValue | json values}.\n * @public\n */\nexport abstract class SimpleJsonMapBase<T> implements IJsonReferenceMap {\n /**\n * The {@link ReferenceMapKeyPolicy | key policy} in effect for this map.\n * @internal\n */\n protected readonly _keyPolicy: ReferenceMapKeyPolicy<T>;\n\n /**\n * A map containing keys and values already present in this map.\n * @internal\n */\n protected readonly _values: Map<string, T>;\n\n /**\n * An optional {@link IJsonContext | IJsonContext} used for any conversions\n * involving items in this map.\n * @internal\n */\n protected readonly _context?: IJsonContext;\n\n /**\n * Constructs a new {@link SimpleJsonMap | SimpleJsonMap}.\n * @param values - Initial values for the map.\n * @param context - An optional {@link IJsonContext | IJsonContext} used for any conversions\n * involving items in this map.\n * @param keyPolicy - The {@link ReferenceMapKeyPolicy | key policy} to use for this map.\n * @internal\n */\n protected constructor(\n values?: MapOrRecord<T>,\n context?: IJsonContext,\n keyPolicy?: ReferenceMapKeyPolicy<T>\n ) {\n values = SimpleJsonMapBase._toMap(values).orThrow();\n this._keyPolicy = keyPolicy ?? new ReferenceMapKeyPolicy();\n this._values = this._keyPolicy.validateMap(values).orThrow();\n this._context = context;\n }\n\n /**\n * Returns a `Map\\<string, T\\>` derived from a supplied {@link MapOrRecord | MapOrRecord}\n * @param values - The {@link MapOrRecord | MapOrRecord} to be returned as a map.\n * @returns `Success` with the corresponding `Map\\<string, T\\>` or `Failure` with a\n * message if an error occurs.\n * @internal\n */\n protected static _toMap<T>(values?: MapOrRecord<T>): Result<Map<string, T>> {\n if (values === undefined) {\n return captureResult(() => new Map<string, T>());\n } else if (!(values instanceof Map)) {\n return recordToMap(values, (__k, v) => succeed(v));\n }\n return succeed(values);\n }\n\n /**\n * Determine if a key might be valid for this map but does not determine if key actually\n * exists. Allows key range to be constrained.\n * @param key - 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._keyPolicy.isValid(key);\n }\n\n /**\n * Determines if an entry with the specified key actually exists in the map.\n * @param key - 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._values.has(key);\n }\n\n /**\n * Gets a {@link JsonObject | JSON object} specified by key.\n * @param key - key of the object to be retrieved\n * @param context - optional {@link IJsonContext | JSON context} used to format the\n * returned object.\n * @returns {@link ts-utils#Success | `Success`} with the formatted object if successful.\n * {@link ts-utils#Failure | `Failure`} with detail 'unknown' if no such object exists,\n * or {@link ts-utils#Failure | `Failure`} with detail 'error' if the object was found\n * but 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 - key of the value to be retrieved\n * @param context - 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 abstract getJsonValue(\n key: string,\n context?: IJsonContext\n ): DetailedResult<JsonValue, JsonReferenceMapFailureReason>;\n}\n\n/**\n * Initialization options for a {@link SimpleJsonMap | SimpleJsonMap}.\n * @public\n */\nexport interface ISimpleJsonMapOptions {\n keyPolicy?: ReferenceMapKeyPolicy<JsonValue>;\n editor?: JsonEditor;\n}\n\n/**\n * A {@link SimpleJsonMap | SimpleJsonMap } presents a view of a simple map\n * of {@link JsonValue | JSON values}.\n * @public\n */\nexport class SimpleJsonMap extends SimpleJsonMapBase<JsonValue> {\n /**\n * @internal\n */\n protected _editor?: JsonEditor;\n\n /**\n * Constructs a new {@link SimpleJsonMap | SimpleJsonMap} from the supplied objects\n * @param values - A string-keyed `Map` or `Record` of the {@link JsonValue | JSON values}\n * to be returned.\n * @param context - Optional {@link IJsonContext | IJsonContext} used to format returned values.\n * @param options - Optional {@link ISimpleJsonMapOptions | ISimpleJsonMapOptions} for initialization.\n * @public\n */\n protected constructor(\n values?: MapOrRecord<JsonValue>,\n context?: IJsonContext,\n options?: ISimpleJsonMapOptions\n ) {\n super(values, context, options?.keyPolicy);\n this._editor = options?.editor;\n }\n\n /**\n * Creates a new {@link SimpleJsonMap | SimpleJsonMap} from the supplied objects\n * @param values - A string-keyed `Map` or `Record` of the {@link JsonValue | JSON values}\n * to be returned.\n * @param context - Optional {@link IJsonContext | IJsonContext} used to format returned values.\n * @param options - Optional {@link ISimpleJsonMapOptions | ISimpleJsonMapOptions} for initialization.\n * @returns `Success` with a {@link SimpleJsonMap | SimpleJsonMap} or `Failure` with a message if\n * an error occurs.\n */\n public static createSimple(\n values?: MapOrRecord<JsonValue>,\n context?: IJsonContext,\n options?: ISimpleJsonMapOptions\n ): Result<SimpleJsonMap> {\n return captureResult(() => new SimpleJsonMap(values, context, options));\n }\n\n /**\n * Gets a {@link JsonValue | JSON value} specified by key.\n * @param key - key of the value to be retrieved\n * @param context - 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 public getJsonValue(\n key: string,\n context?: IJsonContext\n ): DetailedResult<JsonValue, JsonReferenceMapFailureReason> {\n context = context ?? this._context;\n const value = this._values.get(key);\n if (!value) {\n return failWithDetail(`${key}: JSON value not found`, 'unknown');\n }\n return this._clone(value, context);\n }\n\n /**\n * @internal\n */\n protected _clone(\n value: JsonValue,\n context?: IJsonContext\n ): DetailedResult<JsonValue, JsonReferenceMapFailureReason> {\n if (!this._editor) {\n const result = JsonEditor.create();\n /* c8 ignore next 3 - nearly impossible to reproduce */\n if (result.isFailure()) {\n return failWithDetail(result.message, 'error');\n }\n this._editor = result.value;\n }\n return this._editor.clone(value, context).withFailureDetail('error');\n }\n}\n\n/**\n * Initialization options for a {@link PrefixedJsonMap | PrefixedJsonMap}\n * @public\n */\nexport interface IKeyPrefixOptions {\n /**\n * Indicates whether the prefix should be added automatically as needed (default true)\n */\n addPrefix?: boolean;\n\n /**\n * The prefix to be enforced\n */\n prefix: string;\n}\n\n/**\n * A {@link PrefixedJsonMap | PrefixedJsonMap} enforces a supplied prefix for all contained values,\n * optionally adding the prefix as necessary (default `true`).\n * @public\n */\nexport class PrefixedJsonMap extends SimpleJsonMap {\n /**\n * Constructs a new {@link PrefixedJsonMap | PrefixedJsonMap} from the supplied values\n * @param prefix - A string prefix to be enforced for and added to key names as necessary\n * @param values - A string-keyed Map or Record of the {@link JsonValue | JsonValue} to be returned\n * @param context - Optional {@link IJsonContext | JSON Context} used to format returned values\n * @param editor - Optional {@link Editor.JsonEditor | JsonEditor} used to format returned values\n * @public\n */\n protected constructor(\n values?: MapOrRecord<JsonValue>,\n context?: IJsonContext,\n options?: ISimpleJsonMapOptions\n ) {\n super(values, context, options);\n }\n\n /**\n * Creates a new {@link PrefixedJsonMap | PrefixedJsonMap} from the supplied values\n * @param prefix - A string prefix to be enforced for and added to key names as necessary\n * @param values - A string-keyed Map or Record of the {@link JsonValue | JsonValue} to be returned\n * @param context - Optional {@link IJsonContext | JSON Context} used to format returned values\n * @param editor - Optional {@link Editor.JsonEditor | JsonEditor} used to format returned values\n * @returns `Success` with a {@link PrefixedJsonMap | PrefixedJsonMap} or `Failure` with a message\n * if an error occurs.\n */\n public static createPrefixed(\n prefix: string,\n values?: MapOrRecord<JsonValue>,\n context?: IJsonContext,\n editor?: JsonEditor\n ): Result<PrefixedJsonMap>;\n\n /**\n * Creates a new {@link PrefixedJsonMap | PrefixedJsonMap} from the supplied values\n * @param prefixOptions - A KeyPrefixOptions indicating the prefix to enforce and whether that prefix should\n * be added automatically if necessary (default true)\n * @param values - A string-keyed Map or record of the {@link JsonValue | JsonValue} to be returned\n * @param context - Optional {@link IJsonContext | JSON Context} used to format returned values\n * @param editor - Optional {@link Editor.JsonEditor | JsonEditor} used to format returned values\n */\n public static createPrefixed(\n prefixOptions: IKeyPrefixOptions,\n values?: MapOrRecord<JsonValue>,\n context?: IJsonContext,\n editor?: JsonEditor\n ): Result<PrefixedJsonMap>;\n public static createPrefixed(\n prefixOptions: string | IKeyPrefixOptions,\n values?: MapOrRecord<JsonValue>,\n context?: IJsonContext,\n editor?: JsonEditor\n ): Result<PrefixedJsonMap> {\n return captureResult(\n () => new PrefixedJsonMap(values, context, { keyPolicy: this._toPolicy(prefixOptions), editor })\n );\n }\n\n /**\n * Constructs a new {@link PrefixKeyPolicy | PrefixKeyPolicy} from a supplied prefix\n * or set of {@link IKeyPrefixOptions | prefix options}.\n * @param prefixOptions - The prefix or {@link IKeyPrefixOptions | prefix options} or options\n * for the new policy.\n * @returns A new {@link ReferenceMapKeyPolicy | ReferenceMapKeyPolicy} which enforces the\n * supplied prefix or options.\n * @internal\n */\n protected static _toPolicy(prefixOptions: string | IKeyPrefixOptions): ReferenceMapKeyPolicy<JsonValue> {\n if (typeof prefixOptions === 'string') {\n return new PrefixKeyPolicy(prefixOptions, { makeValid: true });\n }\n return new PrefixKeyPolicy(prefixOptions.prefix, { makeValid: prefixOptions.addPrefix !== false });\n }\n}\n"]}
1
+ {"version":3,"file":"jsonReferenceMap.js","sourceRoot":"","sources":["../../../src/packlets/editor/jsonReferenceMap.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAUuB;AAEvB,oDAAwE;AAExE,6CAA0C;AAc1C;;;;GAIG;AACH,MAAa,qBAAqB;IAWhC;;;;;OAKG;IACH,YACE,OAA+C,EAC/C,OAA4C;QAE5C,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC;QAC/B,IAAI,CAAC,QAAQ,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,qBAAqB,CAAC,mBAAmB,CAAC;IACv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CACF;AAzED,0CAyEC","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 {\n DetailedResult,\n Result,\n captureResult,\n fail,\n failWithDetail,\n mapResults,\n recordToMap,\n succeed,\n succeedWithDetail\n} from '@fgv/ts-utils';\n\nimport { JsonObject, JsonValue, isJsonObject } from '@fgv/ts-json-base';\nimport { IJsonContext, IJsonReferenceMap, JsonReferenceMapFailureReason } from '../context';\nimport { JsonEditor } from './jsonEditor';\n\n/**\n * Options for creating a {@link ReferenceMapKeyPolicy | ReferenceMapKeyPolicy} object.\n * @public\n */\nexport interface IReferenceMapKeyPolicyValidateOptions {\n /**\n * If `true`, the validator coerces keys to some valid value.\n * If `false`, invalid keys cause an error.\n */\n makeValid?: boolean;\n}\n\n/**\n * Policy object responsible for validating or correcting\n * keys in a {@link IJsonReferenceMap | reference map}.\n * @public\n */\nexport class ReferenceMapKeyPolicy<T> {\n /**\n * @internal\n */\n protected readonly _defaultOptions?: IReferenceMapKeyPolicyValidateOptions;\n\n /**\n * @internal\n */\n protected readonly _isValid: (key: string, item?: T) => boolean;\n\n /**\n * Constructs a new {@link ReferenceMapKeyPolicy | ReferenceMapKeyPolicy}.\n * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options}\n * used to construct the {@link ReferenceMapKeyPolicy}.\n * @param isValid - An optional predicate to test a supplied key for validity.\n */\n public constructor(\n options?: IReferenceMapKeyPolicyValidateOptions,\n isValid?: (key: string, item?: T) => boolean\n ) {\n this._defaultOptions = options;\n this._isValid = isValid ?? ReferenceMapKeyPolicy.defaultKeyPredicate;\n }\n\n /**\n * The static default key name validation predicate rejects keys that contain\n * mustache templates or which start with the default conditional prefix\n * `'?'`.\n * @param key - The key to test.\n * @returns `true` if the key is valid, `false` otherwise.\n */\n public static defaultKeyPredicate(key: string): boolean {\n return key.length > 0 && !key.includes('{{') && !key.startsWith('?');\n }\n\n /**\n * Determines if a supplied key and item are valid according to the current policy.\n * @param key - The key to be tested.\n * @param item - The item to be tested.\n * @returns `true` if the key and value are valid, `false` otherwise.\n */\n public isValid(key: string, item?: T): boolean {\n return this._isValid(key, item);\n }\n\n /**\n * Determines if a supplied key and item are valid according to the current policy.\n * @param key - The key to be tested.\n * @param item - The item to be tested.\n * @returns `Success` with the key if valid, `Failure` with an error message if invalid.\n */\n public validate(key: string, item?: T, __options?: IReferenceMapKeyPolicyValidateOptions): Result<string> {\n return this.isValid(key, item) ? succeed(key) : fail(`${key}: invalid key`);\n }\n\n /**\n * Validates an array of entries using the validation rules for this policy.\n * @param items - The array of entries to be validated.\n * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to control\n * validation.\n * @returns `Success` with an array of validated entries, or `Failure` with an error message\n * if validation fails.\n */\n public validateItems(\n items: [string, T][],\n options?: IReferenceMapKeyPolicyValidateOptions\n ): Result<[string, T][]> {\n return mapResults(\n items.map((item) => {\n return this.validate(...item, options).onSuccess((valid) => {\n return succeed([valid, item[1]]);\n });\n })\n );\n }\n\n /**\n * Validates a `Map\\<string, T\\>` using the validation rules for this policy.\n * @param items - The `Map\\<string, T\\>` to be validated.\n * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to control\n * validation.\n * @returns `Success` with a new `Map\\<string, T\\>`, or `Failure` with an error message\n * if validation fails.\n */\n public validateMap(\n map: Map<string, T>,\n options?: IReferenceMapKeyPolicyValidateOptions\n ): Result<Map<string, T>> {\n return this.validateItems(Array.from(map.entries()), options).onSuccess((valid) => {\n return captureResult(() => new Map(valid));\n });\n }\n}\n\n/**\n * A {@link PrefixKeyPolicy | PrefixKeyPolicy} enforces that all keys start with a supplied\n * prefix, optionally adding the prefix as necessary.\n * @public\n */\nexport class PrefixKeyPolicy<T> extends ReferenceMapKeyPolicy<T> {\n /**\n * The string prefix to be enforced by this policy.\n */\n public readonly prefix: string;\n\n /**\n * Constructs a new {@link PrefixKeyPolicy | PrefixKeyPolicy}.\n * @param prefix - The string prefix to be enforced or applied.\n * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to\n * configure the policy.\n */\n public constructor(prefix: string, options?: IReferenceMapKeyPolicyValidateOptions) {\n super(options);\n this.prefix = prefix;\n }\n\n /**\n * Determines if a key is valid according to policy.\n * @param key - The key to be tested.\n * @param __item - The item to be tested.\n * @returns `true` if the key starts with the expected prefix, `false` otherwise.\n */\n public isValid(key: string, __item?: T): boolean {\n return (\n key.startsWith(this.prefix) && key !== this.prefix && ReferenceMapKeyPolicy.defaultKeyPredicate(key)\n );\n }\n\n /**\n * Determines if a key is valid according to policy, optionally coercing to a valid value by\n * adding the required prefix.\n * @param key - The key to be tested.\n * @param item - The item which corresponds to the key.\n * @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to guide\n * validation.\n * @returns `Success` with a valid key name if the supplied key is valid or if `makeValid` is set\n * in the policy options. Returns `Failure` with an error message if an error occurs.\n */\n public validate(key: string, item?: T, options?: IReferenceMapKeyPolicyValidateOptions): Result<string> {\n /* c8 ignore next */\n const makeValid = (options ?? this._defaultOptions)?.makeValid === true;\n if (this.isValid(key, item)) {\n return succeed(key);\n } else if (makeValid && ReferenceMapKeyPolicy.defaultKeyPredicate(key)) {\n return succeed(`${this.prefix}${key}`);\n }\n return fail(`${key}: invalid key`);\n }\n}\n\n/**\n * Type representing either a `Map\\<string, T\\>` or a `Record\\<string, T\\>`.\n * @public\n */\nexport type MapOrRecord<T> = Map<string, T> | Record<string, T>;\n\n/**\n * Abstract base class with common functionality for simple\n * {@link IJsonReferenceMap | reference map} implementations.\n * {@link JsonValue | json values}.\n * @public\n */\nexport abstract class SimpleJsonMapBase<T> implements IJsonReferenceMap {\n /**\n * The {@link ReferenceMapKeyPolicy | key policy} in effect for this map.\n * @internal\n */\n protected readonly _keyPolicy: ReferenceMapKeyPolicy<T>;\n\n /**\n * A map containing keys and values already present in this map.\n * @internal\n */\n protected readonly _values: Map<string, T>;\n\n /**\n * An optional {@link IJsonContext | IJsonContext} used for any conversions\n * involving items in this map.\n * @internal\n */\n protected readonly _context?: IJsonContext;\n\n /**\n * Constructs a new {@link SimpleJsonMap | SimpleJsonMap}.\n * @param values - Initial values for the map.\n * @param context - An optional {@link IJsonContext | IJsonContext} used for any conversions\n * involving items in this map.\n * @param keyPolicy - The {@link ReferenceMapKeyPolicy | key policy} to use for this map.\n * @internal\n */\n protected constructor(\n values?: MapOrRecord<T>,\n context?: IJsonContext,\n keyPolicy?: ReferenceMapKeyPolicy<T>\n ) {\n values = SimpleJsonMapBase._toMap(values).orThrow();\n this._keyPolicy = keyPolicy ?? new ReferenceMapKeyPolicy();\n this._values = this._keyPolicy.validateMap(values).orThrow();\n this._context = context;\n }\n\n /**\n * Returns a `Map\\<string, T\\>` derived from a supplied {@link MapOrRecord | MapOrRecord}\n * @param values - The {@link MapOrRecord | MapOrRecord} to be returned as a map.\n * @returns `Success` with the corresponding `Map\\<string, T\\>` or `Failure` with a\n * message if an error occurs.\n * @internal\n */\n protected static _toMap<T>(values?: MapOrRecord<T>): Result<Map<string, T>> {\n if (values === undefined) {\n return captureResult(() => new Map<string, T>());\n } else if (!(values instanceof Map)) {\n return recordToMap(values, (__k, v) => succeed(v));\n }\n return succeed(values);\n }\n\n /**\n * Determine if a key might be valid for this map but does not determine if key actually\n * exists. Allows key range to be constrained.\n * @param key - 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._keyPolicy.isValid(key);\n }\n\n /**\n * Determines if an entry with the specified key actually exists in the map.\n * @param key - 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._values.has(key);\n }\n\n /**\n * Gets a {@link JsonObject | JSON object} specified by key.\n * @param key - key of the object to be retrieved\n * @param context - optional {@link IJsonContext | JSON context} used to format the\n * returned object.\n * @returns {@link ts-utils#Success | `Success`} with the formatted object if successful.\n * {@link ts-utils#Failure | `Failure`} with detail 'unknown' if no such object exists,\n * or {@link ts-utils#Failure | `Failure`} with detail 'error' if the object was found\n * but 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 - key of the value to be retrieved\n * @param context - 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 abstract getJsonValue(\n key: string,\n context?: IJsonContext\n ): DetailedResult<JsonValue, JsonReferenceMapFailureReason>;\n}\n\n/**\n * Initialization options for a {@link SimpleJsonMap | SimpleJsonMap}.\n * @public\n */\nexport interface ISimpleJsonMapOptions {\n keyPolicy?: ReferenceMapKeyPolicy<JsonValue>;\n editor?: JsonEditor;\n}\n\n/**\n * A {@link SimpleJsonMap | SimpleJsonMap } presents a view of a simple map\n * of {@link JsonValue | JSON values}.\n * @public\n */\nexport class SimpleJsonMap extends SimpleJsonMapBase<JsonValue> {\n /**\n * @internal\n */\n protected _editor?: JsonEditor;\n\n /**\n * Constructs a new {@link SimpleJsonMap | SimpleJsonMap} from the supplied objects\n * @param values - A string-keyed `Map` or `Record` of the {@link JsonValue | JSON values}\n * to be returned.\n * @param context - Optional {@link IJsonContext | IJsonContext} used to format returned values.\n * @param options - Optional {@link ISimpleJsonMapOptions | ISimpleJsonMapOptions} for initialization.\n * @public\n */\n protected constructor(\n values?: MapOrRecord<JsonValue>,\n context?: IJsonContext,\n options?: ISimpleJsonMapOptions\n ) {\n super(values, context, options?.keyPolicy);\n this._editor = options?.editor;\n }\n\n /**\n * Creates a new {@link SimpleJsonMap | SimpleJsonMap} from the supplied objects\n * @param values - A string-keyed `Map` or `Record` of the {@link JsonValue | JSON values}\n * to be returned.\n * @param context - Optional {@link IJsonContext | IJsonContext} used to format returned values.\n * @param options - Optional {@link ISimpleJsonMapOptions | ISimpleJsonMapOptions} for initialization.\n * @returns `Success` with a {@link SimpleJsonMap | SimpleJsonMap} or `Failure` with a message if\n * an error occurs.\n */\n public static createSimple(\n values?: MapOrRecord<JsonValue>,\n context?: IJsonContext,\n options?: ISimpleJsonMapOptions\n ): Result<SimpleJsonMap> {\n return captureResult(() => new SimpleJsonMap(values, context, options));\n }\n\n /**\n * Gets a {@link JsonValue | JSON value} specified by key.\n * @param key - key of the value to be retrieved\n * @param context - 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 public getJsonValue(\n key: string,\n context?: IJsonContext\n ): DetailedResult<JsonValue, JsonReferenceMapFailureReason> {\n context = context ?? this._context;\n const value = this._values.get(key);\n if (!value) {\n return failWithDetail(`${key}: JSON value not found`, 'unknown');\n }\n return this._clone(value, context);\n }\n\n /**\n * @internal\n */\n protected _clone(\n value: JsonValue,\n context?: IJsonContext\n ): DetailedResult<JsonValue, JsonReferenceMapFailureReason> {\n if (!this._editor) {\n const result = JsonEditor.create();\n /* c8 ignore next 3 - nearly impossible to reproduce */\n if (result.isFailure()) {\n return failWithDetail(result.message, 'error');\n }\n this._editor = result.value;\n }\n return this._editor.clone(value, context).withFailureDetail('error');\n }\n}\n\n/**\n * Initialization options for a {@link PrefixedJsonMap | PrefixedJsonMap}\n * @public\n */\nexport interface IKeyPrefixOptions {\n /**\n * Indicates whether the prefix should be added automatically as needed (default true)\n */\n addPrefix?: boolean;\n\n /**\n * The prefix to be enforced\n */\n prefix: string;\n}\n\n/**\n * A {@link PrefixedJsonMap | PrefixedJsonMap} enforces a supplied prefix for all contained values,\n * optionally adding the prefix as necessary (default `true`).\n * @public\n */\nexport class PrefixedJsonMap extends SimpleJsonMap {\n /**\n * Constructs a new {@link PrefixedJsonMap | PrefixedJsonMap} from the supplied values\n * @param prefix - A string prefix to be enforced for and added to key names as necessary\n * @param values - A string-keyed Map or Record of the {@link JsonValue | JsonValue} to be returned\n * @param context - Optional {@link IJsonContext | JSON Context} used to format returned values\n * @param editor - Optional {@link Editor.JsonEditor | JsonEditor} used to format returned values\n * @public\n */\n protected constructor(\n values?: MapOrRecord<JsonValue>,\n context?: IJsonContext,\n options?: ISimpleJsonMapOptions\n ) {\n super(values, context, options);\n }\n\n /**\n * Creates a new {@link PrefixedJsonMap | PrefixedJsonMap} from the supplied values\n * @param prefix - A string prefix to be enforced for and added to key names as necessary\n * @param values - A string-keyed Map or Record of the {@link JsonValue | JsonValue} to be returned\n * @param context - Optional {@link IJsonContext | JSON Context} used to format returned values\n * @param editor - Optional {@link Editor.JsonEditor | JsonEditor} used to format returned values\n * @returns `Success` with a {@link PrefixedJsonMap | PrefixedJsonMap} or `Failure` with a message\n * if an error occurs.\n */\n public static createPrefixed(\n prefix: string,\n values?: MapOrRecord<JsonValue>,\n context?: IJsonContext,\n editor?: JsonEditor\n ): Result<PrefixedJsonMap>;\n\n /**\n * Creates a new {@link PrefixedJsonMap | PrefixedJsonMap} from the supplied values\n * @param prefixOptions - A KeyPrefixOptions indicating the prefix to enforce and whether that prefix should\n * be added automatically if necessary (default true)\n * @param values - A string-keyed Map or record of the {@link JsonValue | JsonValue} to be returned\n * @param context - Optional {@link IJsonContext | JSON Context} used to format returned values\n * @param editor - Optional {@link Editor.JsonEditor | JsonEditor} used to format returned values\n */\n public static createPrefixed(\n prefixOptions: IKeyPrefixOptions,\n values?: MapOrRecord<JsonValue>,\n context?: IJsonContext,\n editor?: JsonEditor\n ): Result<PrefixedJsonMap>;\n public static createPrefixed(\n prefixOptions: string | IKeyPrefixOptions,\n values?: MapOrRecord<JsonValue>,\n context?: IJsonContext,\n editor?: JsonEditor\n ): Result<PrefixedJsonMap> {\n return captureResult(\n () => new PrefixedJsonMap(values, context, { keyPolicy: this._toPolicy(prefixOptions), editor })\n );\n }\n\n /**\n * Constructs a new {@link PrefixKeyPolicy | PrefixKeyPolicy} from a supplied prefix\n * or set of {@link IKeyPrefixOptions | prefix options}.\n * @param prefixOptions - The prefix or {@link IKeyPrefixOptions | prefix options} or options\n * for the new policy.\n * @returns A new {@link ReferenceMapKeyPolicy | ReferenceMapKeyPolicy} which enforces the\n * supplied prefix or options.\n * @internal\n */\n protected static _toPolicy(prefixOptions: string | IKeyPrefixOptions): ReferenceMapKeyPolicy<JsonValue> {\n if (typeof prefixOptions === 'string') {\n return new PrefixKeyPolicy(prefixOptions, { makeValid: true });\n }\n return new PrefixKeyPolicy(prefixOptions.prefix, { makeValid: prefixOptions.addPrefix !== false });\n }\n}\n"]}
@@ -1,5 +1,5 @@
1
+ import { JsonObject, JsonValue } from '@fgv/ts-json-base';
1
2
  import { DetailedResult, Result } from '@fgv/ts-utils';
2
- import { JsonObject, JsonValue } from '../../json';
3
3
  import { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';
4
4
  import { JsonEditorRuleBase } from '../jsonEditorRule';
5
5
  import { JsonEditorState } from '../jsonEditorState';
@@ -1 +1 @@
1
- {"version":3,"file":"conditional.d.ts","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/conditional.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,cAAc,EAAE,MAAM,EAAoD,MAAM,eAAe,CAAC;AACzG,OAAO,EAAE,UAAU,EAAE,SAAS,EAAgB,MAAM,YAAY,CAAC;AACjE,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,6BAA6B,EAAE,MAAM,WAAW,CAAC;AACrG,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAErD;;;;GAIG;AACH,MAAM,WAAW,yBAA0B,SAAQ,UAAU;IAC3D,SAAS,EAAE,SAAS,GAAG,OAAO,GAAG,eAAe,CAAC;CAClD;AAED;;;;;GAKG;AACH,MAAM,WAAW,8BAA+B,SAAQ,yBAAyB;IAC/E,KAAK,EAAE,SAAS,CAAC;CAClB;AAED;;;GAGG;AACH,MAAM,WAAW,2BAA4B,SAAQ,OAAO,CAAC,kBAAkB,CAAC;IAC9E;;;OAGG;IACH,0BAA0B,CAAC,EAAE,OAAO,CAAC;CACtC;AAED;;;;;;;;;;GAUG;AACH,qBAAa,yBAA0B,SAAQ,kBAAkB;IAC/D;;;;OAIG;IACH,SAAS,CAAC,QAAQ,CAAC,EAAE,2BAA2B,CAAC;IAEjD;;;;OAIG;gBACgB,OAAO,CAAC,EAAE,2BAA2B;IAKxD;;;;OAIG;WACW,MAAM,CAAC,OAAO,CAAC,EAAE,2BAA2B,GAAG,MAAM,CAAC,yBAAyB,CAAC;IAI9F;;;;;;;;;;OAUG;IACI,YAAY,CACjB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,SAAS,EAChB,KAAK,EAAE,eAAe,GACrB,cAAc,CAAC,UAAU,EAAE,6BAA6B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;;;;OAIG;IACH,SAAS,CAAC,QAAQ,CAAC,EAAE,2BAA2B,CAAC;IAEjD;;;;OAIG;gBACgB,OAAO,CAAC,EAAE,2BAA2B;IAKxD;;;;OAIG;WACW,MAAM,CAAC,OAAO,CAAC,EAAE,2BAA2B,GAAG,MAAM,CAAC,yBAAyB,CAAC;IAI9F;;;;;;;;;;OAUG;IACI,YAAY,CACjB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,SAAS,EAChB,KAAK,EAAE,eAAe,GACrB,cAAc,CAAC,UAAU,EAAE,6BAA6B,CAAC;IAmB5D;;;;;;OAMG;IACI,kBAAkB,CACvB,SAAS,EAAE,UAAU,EAAE,EACvB,OAAO,EAAE,eAAe,GACvB,cAAc,CAAC,UAAU,EAAE,EAAE,qBAAqB,CAAC;IAUtD;;;;;;;;;;;OAWG;IACH,SAAS,CAAC,kBAAkB,CAC1B,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,eAAe,GACrB,cAAc,CAAC,yBAAyB,EAAE,6BAA6B,CAAC;IA6B3E;;;;;;;OAOG;IACH,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO;CAkB3E"}
@@ -22,8 +22,8 @@
22
22
  */
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.ConditionalJsonEditorRule = void 0;
25
+ const ts_json_base_1 = require("@fgv/ts-json-base");
25
26
  const ts_utils_1 = require("@fgv/ts-utils");
26
- const json_1 = require("../../json");
27
27
  const jsonEditorRule_1 = require("../jsonEditorRule");
28
28
  /**
29
29
  * The {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule} evaluates
@@ -68,7 +68,7 @@ class ConditionalJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
68
68
  editProperty(key, value, state) {
69
69
  var _a;
70
70
  const result = this._tryParseCondition(key, state).onSuccess((deferred) => {
71
- if ((0, json_1.isJsonObject)(value)) {
71
+ if ((0, ts_json_base_1.isJsonObject)(value)) {
72
72
  const rtrn = Object.assign(Object.assign({}, deferred), { value });
73
73
  return (0, ts_utils_1.succeedWithDetail)(rtrn, 'deferred');
74
74
  }
@@ -93,7 +93,7 @@ class ConditionalJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
93
93
  toMerge = finalized.filter((o) => o.matchType === 'match' || o.matchType === 'unconditional');
94
94
  }
95
95
  }
96
- return (0, ts_utils_1.succeedWithDetail)(toMerge.map((o) => o.value).filter(json_1.isJsonObject), 'edited');
96
+ return (0, ts_utils_1.succeedWithDetail)(toMerge.map((o) => o.value).filter(ts_json_base_1.isJsonObject), 'edited');
97
97
  }
98
98
  /**
99
99
  * Determines if a given property key is conditional. Derived classes can override this
@@ -1 +1 @@
1
- {"version":3,"file":"conditional.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/conditional.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAAyG;AACzG,qCAAiE;AAEjE,sDAAuD;AAkCvD;;;;;;;;;;GAUG;AACH,MAAa,yBAA0B,SAAQ,mCAAkB;IAQ/D;;;;OAIG;IACH,YAAmB,OAAqC;QACtD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,OAAqC;QACxD,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,yBAAyB,CAAC,OAAO,CAAC,CAAC,CAAC;IACrE,CAAC;IAED;;;;;;;;;;OAUG;IACI,YAAY,CACjB,GAAW,EACX,KAAgB,EAChB,KAAsB;;QAEtB,MAAM,MAAM,GAAG,IAAI,CAAC,kBAAkB,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE,EAAE;YACxE,IAAI,IAAA,mBAAY,EAAC,KAAK,CAAC,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QAAQ,0CAAE,0BAA0B,MAAK,KAAK,IAAI,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;YACrF,OAAO,IAAA,4BAAiB,EAAC,EAAE,SAAS,EAAE,eAAe,EAAE,EAAE,UAAU,CAAC,CAAC;SACtE;QACD,OAAO,IAAA,yBAAc,EAAC,cAAc,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC;IAED;;;;;;;OAOG;IACO,QAAQ,CAAC,IAAY,EAAE,KAAa,EAAE,QAAgB;QAC9D,QAAQ,QAAQ,EAAE;YAChB,KAAK,GAAG;gBACN,OAAO,IAAI,KAAK,KAAK,CAAC;YACxB,KAAK,GAAG;gBACN,OAAO,IAAI,GAAG,KAAK,CAAC;YACtB,KAAK,GAAG;gBACN,OAAO,IAAI,GAAG,KAAK,CAAC;YACtB,KAAK,IAAI;gBACP,OAAO,IAAI,IAAI,KAAK,CAAC;YACvB,KAAK,IAAI;gBACP,OAAO,IAAI,IAAI,KAAK,CAAC;YACvB,KAAK,IAAI;gBACP,OAAO,IAAI,KAAK,KAAK,CAAC;SACzB;QACD,oCAAoC;QACpC,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAvJD,8DAuJC","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 { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * Returned by {@link Editor.Rules.ConditionalJsonEditorRule._tryParseCondition | ConditionalJsonEditorRule._tryParseCondition}\n * to indicate whether a successful match was due to a matching condition or a default value.\n * @public\n */\nexport interface IConditionalJsonKeyResult extends JsonObject {\n matchType: 'default' | 'match' | 'unconditional';\n}\n\n/**\n * On a successful match, the {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}\n * stores a {@link Editor.Rules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject} describing the\n * matching result, to be resolved at finalization time.\n * @public\n */\nexport interface IConditionalJsonDeferredObject extends IConditionalJsonKeyResult {\n value: JsonValue;\n}\n\n/**\n * Configuration options for the {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @public\n */\nexport interface IConditionalJsonRuleOptions extends Partial<IJsonEditorOptions> {\n /**\n * If true (default) then properties with unconditional names\n * (which start with !) are flattened.\n */\n flattenUnconditionalValues?: boolean;\n}\n\n/**\n * The {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule} evaluates\n * properties with conditional keys, omitting non-matching keys and merging keys that match,\n * or default keys only if no other keys match.\n *\n * The default syntax for a conditional key is:\n * \"?value1=value2\" - matches if value1 and value2 are the same, is ignored otherwise.\n * \"?value\" - matches if value is a non-empty, non-whitespace string. Is ignored otherwise.\n * \"?default\" - matches only if no other conditional blocks in the same object were matched.\n * @public\n */\nexport class ConditionalJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link Editor.Rules.IConditionalJsonRuleOptions | options} for this\n * rule.\n * @public\n */\n protected _options?: IConditionalJsonRuleOptions;\n\n /**\n * Creates a new {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @param options - Optional {@link Editor.Rules.IConditionalJsonRuleOptions | configuration options}\n * used for this rule.\n */\n public constructor(options?: IConditionalJsonRuleOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @param options - Optional {@link Editor.Rules.IConditionalJsonRuleOptions | configuration options}\n * used for this rule.\n */\n public static create(options?: IConditionalJsonRuleOptions): Result<ConditionalJsonEditorRule> {\n return captureResult(() => new ConditionalJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for conditional application.\n * @param key - The key of the property to be considered\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns Returns `Success` with detail `'deferred'` and a\n * {@link Editor.Rules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject}.\n * for a matching, default or unconditional key. Returns `Failure` with detail `'ignore'` for\n * a non-matching conditional, or with detail `'error'` if an error occurs. Otherwise\n * fails with detail `'inapplicable'`.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n const result = this._tryParseCondition(key, state).onSuccess((deferred) => {\n if (isJsonObject(value)) {\n const rtrn: IConditionalJsonDeferredObject = { ...deferred, value };\n return succeedWithDetail(rtrn, 'deferred');\n }\n return failWithDetail<JsonObject, JsonPropertyEditFailureReason>(\n `${key}: conditional body must be object`,\n 'error'\n );\n });\n\n if (result.isFailure() && result.detail === 'error') {\n return state.failValidation('invalidPropertyName', result.message, this._options?.validation);\n }\n\n return result;\n }\n\n /**\n * Finalizes any deferred conditional properties. If the only deferred property is\n * default, that property is emitted. Otherwise all matching properties are emitted.\n * @param finalized - The deferred properties to be considered for merge.\n * @param __state - The {@link Editor.JsonEditorState | editor state} for the object\n * being edited.\n */\n public finalizeProperties(\n finalized: JsonObject[],\n __state: JsonEditorState\n ): DetailedResult<JsonObject[], JsonEditFailureReason> {\n let toMerge = finalized;\n if (finalized.length > 1) {\n if (finalized.find((o) => o.matchType === 'match') !== undefined) {\n toMerge = finalized.filter((o) => o.matchType === 'match' || o.matchType === 'unconditional');\n }\n }\n return succeedWithDetail(toMerge.map((o) => o.value).filter(isJsonObject), 'edited');\n }\n\n /**\n * Determines if a given property key is conditional. Derived classes can override this\n * method to use a different format for conditional properties.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'deferred'` and a\n * {@link Editor.Rules.IConditionalJsonKeyResult | IConditionalJsonKeyResult} describing the\n * match for a default or matching conditional property. Returns `Failure` with detail `'ignore'`\n * for a non-matching conditional property. Fails with detail `'error'` if an error occurs\n * or with detail `'inapplicable'` if the key does not represent a conditional property.\n * @public\n */\n protected _tryParseCondition(\n key: string,\n state: JsonEditorState\n ): DetailedResult<IConditionalJsonKeyResult, JsonPropertyEditFailureReason> {\n if (key.startsWith('?')) {\n // ignore everything after any #\n key = key.split('#')[0].trim();\n\n if (key === '?default') {\n return succeedWithDetail({ matchType: 'default' }, 'deferred');\n }\n\n const parts = key.substring(1).split(/(=|>=|<=|>|<|!=)/);\n if (parts.length === 3) {\n if (!this._compare(parts[0].trim(), parts[2].trim(), parts[1])) {\n return failWithDetail(`Condition ${key} does not match`, 'ignore');\n }\n return succeedWithDetail({ matchType: 'match' }, 'deferred');\n } else if (parts.length === 1) {\n if (parts[0].trim().length === 0) {\n return failWithDetail(`Condition ${key} does not match`, 'ignore');\n }\n return succeedWithDetail({ matchType: 'match' }, 'deferred');\n }\n const message = `Malformed condition token ${key}`;\n return state.failValidation('invalidPropertyName', message, this._options?.validation);\n } else if (this._options?.flattenUnconditionalValues !== false && key.startsWith('!')) {\n return succeedWithDetail({ matchType: 'unconditional' }, 'deferred');\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Compares two strings using a supplied operator.\n * @param left - The first string to be compared.\n * @param right - The second string to be compared.\n * @param operator - The operator to be applied.\n * @returns `true` if the condition is met, `false` otherwise.\n * @internal\n */\n protected _compare(left: string, right: string, operator: string): boolean {\n switch (operator) {\n case '=':\n return left === right;\n case '>':\n return left > right;\n case '<':\n return left < right;\n case '>=':\n return left >= right;\n case '<=':\n return left <= right;\n case '!=':\n return left !== right;\n }\n /* c8 ignore next 2 - unreachable */\n return false;\n }\n}\n"]}
1
+ {"version":3,"file":"conditional.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/conditional.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,oDAAwE;AACxE,4CAAyG;AAEzG,sDAAuD;AAkCvD;;;;;;;;;;GAUG;AACH,MAAa,yBAA0B,SAAQ,mCAAkB;IAQ/D;;;;OAIG;IACH,YAAmB,OAAqC;QACtD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,OAAqC;QACxD,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,yBAAyB,CAAC,OAAO,CAAC,CAAC,CAAC;IACrE,CAAC;IAED;;;;;;;;;;OAUG;IACI,YAAY,CACjB,GAAW,EACX,KAAgB,EAChB,KAAsB;;QAEtB,MAAM,MAAM,GAAG,IAAI,CAAC,kBAAkB,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE,EAAE;YACxE,IAAI,IAAA,2BAAY,EAAC,KAAK,CAAC,EAAE,CAA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AO,EAAE,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,CAAC,CAAC;QACzF,CAAC;aAAM,IAAI,CAAA,MAAA,IAAI,CAAC,QAAQ,0CAAE,0BAA0B,MAAK,KAAK,IAAI,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YACtF,OAAO,IAAA,4BAAiB,EAAC,EAAE,SAAS,EAAE,eAAe,EAAE,EAAE,UAAU,CAAC,CAAC;QACvE,CAAC;QACD,OAAO,IAAA,yBAAc,EAAC,cAAc,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC;IAED;;;;;;;OAOG;IACO,QAAQ,CAAC,IAAY,EAAE,KAAa,EAAE,QAAgB;QAC9D,QAAQ,QAAQ,EAAE,CAAC;YACjB,KAAK,GAAG;gBACN,OAAO,IAAI,KAAK,KAAK,CAAC;YACxB,KAAK,GAAG;gBACN,OAAO,IAAI,GAAG,KAAK,CAAC;YACtB,KAAK,GAAG;gBACN,OAAO,IAAI,GAAG,KAAK,CAAC;YACtB,KAAK,IAAI;gBACP,OAAO,IAAI,IAAI,KAAK,CAAC;YACvB,KAAK,IAAI;gBACP,OAAO,IAAI,IAAI,KAAK,CAAC;YACvB,KAAK,IAAI;gBACP,OAAO,IAAI,KAAK,KAAK,CAAC;QAC1B,CAAC;QACD,oCAAoC;QACpC,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAvJD,8DAuJC","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 { JsonObject, JsonValue, isJsonObject } from '@fgv/ts-json-base';\nimport { DetailedResult, Result, captureResult, failWithDetail, succeedWithDetail } from '@fgv/ts-utils';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * Returned by {@link Editor.Rules.ConditionalJsonEditorRule._tryParseCondition | ConditionalJsonEditorRule._tryParseCondition}\n * to indicate whether a successful match was due to a matching condition or a default value.\n * @public\n */\nexport interface IConditionalJsonKeyResult extends JsonObject {\n matchType: 'default' | 'match' | 'unconditional';\n}\n\n/**\n * On a successful match, the {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}\n * stores a {@link Editor.Rules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject} describing the\n * matching result, to be resolved at finalization time.\n * @public\n */\nexport interface IConditionalJsonDeferredObject extends IConditionalJsonKeyResult {\n value: JsonValue;\n}\n\n/**\n * Configuration options for the {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @public\n */\nexport interface IConditionalJsonRuleOptions extends Partial<IJsonEditorOptions> {\n /**\n * If true (default) then properties with unconditional names\n * (which start with !) are flattened.\n */\n flattenUnconditionalValues?: boolean;\n}\n\n/**\n * The {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule} evaluates\n * properties with conditional keys, omitting non-matching keys and merging keys that match,\n * or default keys only if no other keys match.\n *\n * The default syntax for a conditional key is:\n * \"?value1=value2\" - matches if value1 and value2 are the same, is ignored otherwise.\n * \"?value\" - matches if value is a non-empty, non-whitespace string. Is ignored otherwise.\n * \"?default\" - matches only if no other conditional blocks in the same object were matched.\n * @public\n */\nexport class ConditionalJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link Editor.Rules.IConditionalJsonRuleOptions | options} for this\n * rule.\n * @public\n */\n protected _options?: IConditionalJsonRuleOptions;\n\n /**\n * Creates a new {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @param options - Optional {@link Editor.Rules.IConditionalJsonRuleOptions | configuration options}\n * used for this rule.\n */\n public constructor(options?: IConditionalJsonRuleOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @param options - Optional {@link Editor.Rules.IConditionalJsonRuleOptions | configuration options}\n * used for this rule.\n */\n public static create(options?: IConditionalJsonRuleOptions): Result<ConditionalJsonEditorRule> {\n return captureResult(() => new ConditionalJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for conditional application.\n * @param key - The key of the property to be considered\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns Returns `Success` with detail `'deferred'` and a\n * {@link Editor.Rules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject}.\n * for a matching, default or unconditional key. Returns `Failure` with detail `'ignore'` for\n * a non-matching conditional, or with detail `'error'` if an error occurs. Otherwise\n * fails with detail `'inapplicable'`.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n const result = this._tryParseCondition(key, state).onSuccess((deferred) => {\n if (isJsonObject(value)) {\n const rtrn: IConditionalJsonDeferredObject = { ...deferred, value };\n return succeedWithDetail(rtrn, 'deferred');\n }\n return failWithDetail<JsonObject, JsonPropertyEditFailureReason>(\n `${key}: conditional body must be object`,\n 'error'\n );\n });\n\n if (result.isFailure() && result.detail === 'error') {\n return state.failValidation('invalidPropertyName', result.message, this._options?.validation);\n }\n\n return result;\n }\n\n /**\n * Finalizes any deferred conditional properties. If the only deferred property is\n * default, that property is emitted. Otherwise all matching properties are emitted.\n * @param finalized - The deferred properties to be considered for merge.\n * @param __state - The {@link Editor.JsonEditorState | editor state} for the object\n * being edited.\n */\n public finalizeProperties(\n finalized: JsonObject[],\n __state: JsonEditorState\n ): DetailedResult<JsonObject[], JsonEditFailureReason> {\n let toMerge = finalized;\n if (finalized.length > 1) {\n if (finalized.find((o) => o.matchType === 'match') !== undefined) {\n toMerge = finalized.filter((o) => o.matchType === 'match' || o.matchType === 'unconditional');\n }\n }\n return succeedWithDetail(toMerge.map((o) => o.value).filter(isJsonObject), 'edited');\n }\n\n /**\n * Determines if a given property key is conditional. Derived classes can override this\n * method to use a different format for conditional properties.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'deferred'` and a\n * {@link Editor.Rules.IConditionalJsonKeyResult | IConditionalJsonKeyResult} describing the\n * match for a default or matching conditional property. Returns `Failure` with detail `'ignore'`\n * for a non-matching conditional property. Fails with detail `'error'` if an error occurs\n * or with detail `'inapplicable'` if the key does not represent a conditional property.\n * @public\n */\n protected _tryParseCondition(\n key: string,\n state: JsonEditorState\n ): DetailedResult<IConditionalJsonKeyResult, JsonPropertyEditFailureReason> {\n if (key.startsWith('?')) {\n // ignore everything after any #\n key = key.split('#')[0].trim();\n\n if (key === '?default') {\n return succeedWithDetail({ matchType: 'default' }, 'deferred');\n }\n\n const parts = key.substring(1).split(/(=|>=|<=|>|<|!=)/);\n if (parts.length === 3) {\n if (!this._compare(parts[0].trim(), parts[2].trim(), parts[1])) {\n return failWithDetail(`Condition ${key} does not match`, 'ignore');\n }\n return succeedWithDetail({ matchType: 'match' }, 'deferred');\n } else if (parts.length === 1) {\n if (parts[0].trim().length === 0) {\n return failWithDetail(`Condition ${key} does not match`, 'ignore');\n }\n return succeedWithDetail({ matchType: 'match' }, 'deferred');\n }\n const message = `Malformed condition token ${key}`;\n return state.failValidation('invalidPropertyName', message, this._options?.validation);\n } else if (this._options?.flattenUnconditionalValues !== false && key.startsWith('!')) {\n return succeedWithDetail({ matchType: 'unconditional' }, 'deferred');\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Compares two strings using a supplied operator.\n * @param left - The first string to be compared.\n * @param right - The second string to be compared.\n * @param operator - The operator to be applied.\n * @returns `true` if the condition is met, `false` otherwise.\n * @internal\n */\n protected _compare(left: string, right: string, operator: string): boolean {\n switch (operator) {\n case '=':\n return left === right;\n case '>':\n return left > right;\n case '<':\n return left < right;\n case '>=':\n return left >= right;\n case '<=':\n return left <= right;\n case '!=':\n return left !== right;\n }\n /* c8 ignore next 2 - unreachable */\n return false;\n }\n}\n"]}
@@ -1,6 +1,6 @@
1
+ import { JsonObject, JsonValue } from '@fgv/ts-json-base';
1
2
  import { DetailedResult, Result } from '@fgv/ts-utils';
2
3
  import { IJsonContext, VariableValue } from '../../context';
3
- import { JsonObject, JsonValue } from '../../json';
4
4
  import { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';
5
5
  import { JsonEditorRuleBase } from '../jsonEditorRule';
6
6
  import { JsonEditorState } from '../jsonEditorState';
@@ -1 +1 @@
1
- {"version":3,"file":"multivalue.d.ts","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/multivalue.ts"],"names":[],"mappings":"AAsBA,OAAO,EACL,cAAc,EACd,MAAM,EAMP,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,kBAAkB,E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AK,CAAC,CAAC;YACxC,OAAO,GAAG,IAAI,CAAC;SAChB;aAAM,IAAI,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;YAChC,KAAK,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACtC,OAAO,GAAG,KAAK,CAAC;SACjB;aAAM;YACL,OAAO,IAAA,yBAAc,EAAC,KAAK,EAAE,cAAc,CAAC,CAAC;SAC9C;QAED,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE;YACtB,MAAM,OAAO,GAAG,mCAAmC,KAAK,EAAE,CAAC;YAC3D,OAAO,KAAK,CAAC,cAAc,CAAC,qBAAqB,EAAE,OAAO,EAAE,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,CAAC,CAAC;SACxF;QAED,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;YAC3B,OAAO,IAAA,yBAAc,EAAC,qBAAqB,EAAE,cAAc,CAAC,CAAC;SAC9D;QAED,MAAM,gBAAgB,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QAClC,MAAM,cAAc,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC3C,OAAO,IAAA,4BAAiB,EAAC,EAAE,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,OAAO,EAAE,CAAC,CAAC;IACjF,CAAC;CACF;AA7HD,4DA6HC","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 {\n DetailedResult,\n Result,\n allSucceed,\n captureResult,\n failWithDetail,\n succeed,\n succeedWithDetail\n} from '@fgv/ts-utils';\nimport { IJsonContext, VariableValue } from '../../context';\nimport { JsonObject, JsonValue } from '../../json';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\n\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * Represents the parts of a multi-value property key.\n * @public\n */\nexport interface IMultiValuePropertyParts {\n /**\n * The original matched token.\n */\n readonly token: string;\n\n /**\n * The name of the variable used to project each possible\n * property value into the child values or objects being\n * resolved.\n */\n readonly propertyVariable: string;\n\n /**\n * The set of property values to be expanded.\n */\n readonly propertyValues: string[];\n\n /**\n * If `true`, the resolved values are added as an array\n * with the name of the {@link Editor.Rules.IMultiValuePropertyParts.propertyVariable | propertyVariable}.\n * If false, values are added as individual properties with names that correspond the value.\n */\n readonly asArray: boolean;\n}\n\n/**\n * The {@link Editor.Rules.MultiValueJsonEditorRule | Multi-Value JSON editor rule}\n * expands matching keys multiple times, projecting the value into the template\n * context for any child objects rendered by the rule.\n *\n * The default syntax for a multi-value key is:\n * \"[[var]]=value1,value2,value3\"\n * Where \"var\" is the name of the variable that will be passed to\n * child template resolution, and \"value1,value2,value3\" is a\n * comma-separated list of values to be expanded.\n * @public\n */\nexport class MultiValueJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link Editor.IJsonEditorOptions | editor options}\n * for this rule.\n * @public\n */\n protected _options?: IJsonEditorOptions;\n\n /**\n * Creates a new {@link Editor.Rules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options}.\n */\n public constructor(options?: IJsonEditorOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options}.\n */\n public static create(options?: IJsonEditorOptions): Result<MultiValueJsonEditorRule> {\n return captureResult(() => new MultiValueJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for multi-value expansion.\n * @param key - The key of the property to be considered\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with an object containing the fully-resolved child values to be merged for\n * matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs or\n * with detail `'inapplicable'` if the property key is not a conditional property.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n const json: JsonObject = {};\n const result = this._tryParse(key, state).onSuccess((parts) => {\n return allSucceed(\n parts.propertyValues.map((pv) => {\n return this._deriveContext(state, [parts.propertyVariable, pv]).onSuccess((ctx) => {\n return state.editor.clone(value, ctx).onSuccess((cloned) => {\n json[pv] = cloned;\n return succeedWithDetail(cloned);\n });\n });\n }),\n json\n )\n .onSuccess(() => {\n if (parts.asArray) {\n const arrayRtrn: JsonObject = {};\n arrayRtrn[parts.propertyVariable] = Array.from(Object.values(json));\n return succeed(arrayRtrn);\n }\n return succeed(json);\n })\n .withFailureDetail('error');\n });\n\n if (result.isFailure() && result.detail === 'error') {\n return state.failValidation('invalidPropertyName', result.message);\n }\n return result;\n }\n\n /**\n * Extends the {@link IJsonContext | current context} with a supplied state and values.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @param values - An array of {@link VariableValue | VariableValue} to be added to the\n * context.\n * @returns The extended {@link IJsonContext | context}.\n * @public\n */\n protected _deriveContext(\n state: JsonEditorState,\n ...values: VariableValue[]\n ): Result<IJsonContext | undefined> {\n return state.extendContext(this._options?.context, { vars: values });\n }\n\n /**\n * Determines if a given property key is multi-value. Derived classes can override this\n * method to use a different format for multi-value properties.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'deferred'` and an\n * {@link Editor.Rules.IMultiValuePropertyParts | IMultiValuePropertyParts}\n * describing the match for matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs\n * or with detail `'inapplicable'` if the key does not represent a multi-value property.\n * @public\n */\n protected _tryParse(\n token: string,\n state: JsonEditorState\n ): DetailedResult<IMultiValuePropertyParts, JsonEditFailureReason> {\n let parts: string[] = [];\n let asArray = false;\n\n if (token.startsWith('[[')) {\n parts = token.substring(2).split(']]=');\n asArray = true;\n } else if (token.startsWith('*')) {\n parts = token.substring(1).split('=');\n asArray = false;\n } else {\n return failWithDetail(token, 'inapplicable');\n }\n\n if (parts.length !== 2) {\n const message = `Malformed multi-value property: ${token}`;\n return state.failValidation('invalidPropertyName', message, this._options?.validation);\n }\n\n if (parts[1].includes('{{')) {\n return failWithDetail('unresolved template', 'inapplicable');\n }\n\n const propertyVariable = parts[0];\n const propertyValues = parts[1].split(',');\n return succeedWithDetail({ token, propertyVariable, propertyValues, asArray });\n }\n}\n"]}
1
+ {"version":3,"file":"multivalue.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/multivalue.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAGH,4CAQuB;AAIvB,sDAAuD;AAiCvD;;;;;;;;;;;GAWG;AACH,MAAa,wBAAyB,SAAQ,m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AC/C,CAAC;QAED,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,OAAO,GAAG,mCAAmC,KAAK,EAAE,CAAC;YAC3D,OAAO,KAAK,CAAC,cAAc,CAAC,qBAAqB,EAAE,OAAO,EAAE,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,CAAC,CAAC;QACzF,CAAC;QAED,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YAC5B,OAAO,IAAA,yBAAc,EAAC,qBAAqB,EAAE,cAAc,CAAC,CAAC;QAC/D,CAAC;QAED,MAAM,gBAAgB,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QAClC,MAAM,cAAc,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC3C,OAAO,IAAA,4BAAiB,EAAC,EAAE,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,OAAO,EAAE,CAAC,CAAC;IACjF,CAAC;CACF;AA7HD,4DA6HC","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 { JsonObject, JsonValue } from '@fgv/ts-json-base';\nimport {\n DetailedResult,\n Result,\n allSucceed,\n captureResult,\n failWithDetail,\n succeed,\n succeedWithDetail\n} from '@fgv/ts-utils';\nimport { IJsonContext, VariableValue } from '../../context';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\n\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * Represents the parts of a multi-value property key.\n * @public\n */\nexport interface IMultiValuePropertyParts {\n /**\n * The original matched token.\n */\n readonly token: string;\n\n /**\n * The name of the variable used to project each possible\n * property value into the child values or objects being\n * resolved.\n */\n readonly propertyVariable: string;\n\n /**\n * The set of property values to be expanded.\n */\n readonly propertyValues: string[];\n\n /**\n * If `true`, the resolved values are added as an array\n * with the name of the {@link Editor.Rules.IMultiValuePropertyParts.propertyVariable | propertyVariable}.\n * If false, values are added as individual properties with names that correspond the value.\n */\n readonly asArray: boolean;\n}\n\n/**\n * The {@link Editor.Rules.MultiValueJsonEditorRule | Multi-Value JSON editor rule}\n * expands matching keys multiple times, projecting the value into the template\n * context for any child objects rendered by the rule.\n *\n * The default syntax for a multi-value key is:\n * \"[[var]]=value1,value2,value3\"\n * Where \"var\" is the name of the variable that will be passed to\n * child template resolution, and \"value1,value2,value3\" is a\n * comma-separated list of values to be expanded.\n * @public\n */\nexport class MultiValueJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link Editor.IJsonEditorOptions | editor options}\n * for this rule.\n * @public\n */\n protected _options?: IJsonEditorOptions;\n\n /**\n * Creates a new {@link Editor.Rules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options}.\n */\n public constructor(options?: IJsonEditorOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options}.\n */\n public static create(options?: IJsonEditorOptions): Result<MultiValueJsonEditorRule> {\n return captureResult(() => new MultiValueJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for multi-value expansion.\n * @param key - The key of the property to be considered\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with an object containing the fully-resolved child values to be merged for\n * matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs or\n * with detail `'inapplicable'` if the property key is not a conditional property.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n const json: JsonObject = {};\n const result = this._tryParse(key, state).onSuccess((parts) => {\n return allSucceed(\n parts.propertyValues.map((pv) => {\n return this._deriveContext(state, [parts.propertyVariable, pv]).onSuccess((ctx) => {\n return state.editor.clone(value, ctx).onSuccess((cloned) => {\n json[pv] = cloned;\n return succeedWithDetail(cloned);\n });\n });\n }),\n json\n )\n .onSuccess(() => {\n if (parts.asArray) {\n const arrayRtrn: JsonObject = {};\n arrayRtrn[parts.propertyVariable] = Array.from(Object.values(json));\n return succeed(arrayRtrn);\n }\n return succeed(json);\n })\n .withFailureDetail('error');\n });\n\n if (result.isFailure() && result.detail === 'error') {\n return state.failValidation('invalidPropertyName', result.message);\n }\n return result;\n }\n\n /**\n * Extends the {@link IJsonContext | current context} with a supplied state and values.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @param values - An array of {@link VariableValue | VariableValue} to be added to the\n * context.\n * @returns The extended {@link IJsonContext | context}.\n * @public\n */\n protected _deriveContext(\n state: JsonEditorState,\n ...values: VariableValue[]\n ): Result<IJsonContext | undefined> {\n return state.extendContext(this._options?.context, { vars: values });\n }\n\n /**\n * Determines if a given property key is multi-value. Derived classes can override this\n * method to use a different format for multi-value properties.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'deferred'` and an\n * {@link Editor.Rules.IMultiValuePropertyParts | IMultiValuePropertyParts}\n * describing the match for matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs\n * or with detail `'inapplicable'` if the key does not represent a multi-value property.\n * @public\n */\n protected _tryParse(\n token: string,\n state: JsonEditorState\n ): DetailedResult<IMultiValuePropertyParts, JsonEditFailureReason> {\n let parts: string[] = [];\n let asArray = false;\n\n if (token.startsWith('[[')) {\n parts = token.substring(2).split(']]=');\n asArray = true;\n } else if (token.startsWith('*')) {\n parts = token.substring(1).split('=');\n asArray = false;\n } else {\n return failWithDetail(token, 'inapplicable');\n }\n\n if (parts.length !== 2) {\n const message = `Malformed multi-value property: ${token}`;\n return state.failValidation('invalidPropertyName', message, this._options?.validation);\n }\n\n if (parts[1].includes('{{')) {\n return failWithDetail('unresolved template', 'inapplicable');\n }\n\n const propertyVariable = parts[0];\n const propertyValues = parts[1].split(',');\n return succeedWithDetail({ token, propertyVariable, propertyValues, asArray });\n }\n}\n"]}
@@ -1,6 +1,6 @@
1
+ import { JsonObject, JsonValue } from '@fgv/ts-json-base';
1
2
  import { DetailedResult, Result } from '@fgv/ts-utils';
2
3
  import { IJsonContext } from '../../context';
3
- import { JsonObject, JsonValue } from '../../json';
4
4
  import { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';
5
5
  import { JsonEditorRuleBase } from '../jsonEditorRule';
6
6
  import { JsonEditorState } from '../jsonEditorState';
@@ -1 +1 @@
1
- {"version":3,"file":"references.d.ts","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/references.ts"],"names":[],"mappings":"AAsBA,OAAO,EACL,cAAc,EACd,MAAM,EAKP,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,MA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E,qBAAqB,EAAE,6BAA6B,EAAE,MAAM,WAAW,CAAC;AACrG,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,uBAAwB,SAAQ,kBAAkB;IAC7D;;;OAGG;IACH,SAAS,CAAC,QAAQ,CAAC,EAAE,kBAAkB,CAAC;IAExC;;;OAGG;gBACgB,OAAO,CAAC,EAAE,kBAAkB;IAK/C;;;OAGG;WACW,MAAM,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,MAAM,CAAC,uBAAuB,CAAC;IAInF;;;;;;;;OAQG;IACI,YAAY,CACjB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,SAAS,EAChB,KAAK,EAAE,eAAe,GACrB,cAAc,CAAC,UAAU,EAAE,6BAA6B,CAAC;IAyC5D;;;;OAIG;IACI,SAAS,CACd,KAAK,EAAE,SAAS,EAChB,KAAK,EAAE,eAAe,GACrB,cAAc,CAAC,SAAS,EAAE,qBAAqB,CAAC;IAiBnD;;;;;;OAMG;IACH,SAAS,CAAC,cAAc,CAAC,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,SAAS,GAAG,MAAM,CAAC,YAAY,GAAG,SAAS,CAAC;CASxG"}
@@ -22,8 +22,8 @@
22
22
  */
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.ReferenceJsonEditorRule = void 0;
25
+ const ts_json_base_1 = require("@fgv/ts-json-base");
25
26
  const ts_utils_1 = require("@fgv/ts-utils");
26
- const json_1 = require("../../json");
27
27
  const jsonEditorRule_1 = require("../jsonEditorRule");
28
28
  /**
29
29
  * The {@link Editor.Rules.ReferenceJsonEditorRule | Reference JSON editor rule} replaces property
@@ -95,7 +95,7 @@ class ReferenceJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
95
95
  if (typeof value !== 'string' || value === 'default') {
96
96
  return (0, ts_utils_1.succeedWithDetail)(objResult.value, 'edited');
97
97
  }
98
- const pickResult = (0, json_1.pickJsonObject)(objResult.value, value);
98
+ const pickResult = (0, ts_json_base_1.pickJsonObject)(objResult.value, value);
99
99
  if (pickResult.isFailure()) {
100
100
  const message = `${key}: ${pickResult.message}`;
101
101
  return state.failValidation('invalidPropertyName', message, validation);
@@ -146,7 +146,7 @@ class ReferenceJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
146
146
  _extendContext(state, supplied) {
147
147
  var _a;
148
148
  const add = {};
149
- if ((0, json_1.isJsonObject)(supplied)) {
149
+ if ((0, ts_json_base_1.isJsonObject)(supplied)) {
150
150
  add.vars = Object.entries(supplied);
151
151
  }
152
152
  else if (typeof supplied !== 'string') {
@@ -1 +1 @@
1
- {"version":3,"file":"references.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/references.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAOuB;AAEvB,qCAAiF;AAEjF,sDAAuD;AAGvD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,uBAAwB,SAAQ,mCAAkB;IAO7D;;;OAGG;IACH,YAAmB,OAA4B;QAC7C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,MAAM,CAAC,OAA4B;QAC/C,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,uBAAuB,CAAC,OAAO,CAAC,CAAC,CAAC;IACnE,CAAC;IAED;;;;;;;;OAQG;IACI,YAAY,CACjB,GAAW,EACX,KAAgB,EAChB,KAAsB;;QAEtB,sBAAsB;QACtB,MAAM,UAAU,GAAG,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,CAAC;QAC7C,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,MAAA,IAAI,CAAC,QAAQ,0CAAE,O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QACD,OAAO,IAAA,yBAAc,EAAC,cAAc,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC;IAED;;;;;;OAMG;IACO,cAAc,CAAC,KAAsB,EAAE,QAAmB;;QAClE,MAAM,GAAG,GAA4B,EAAE,CAAC;QACxC,IAAI,IAAA,mBAAY,EAAC,QAAQ,CAAC,EAAE;YAC1B,GAAG,CAAC,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;SACrC;aAAM,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;YACvC,OAAO,IAAA,eAAI,EAAC,sCAAsC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;SAChF;QACD,OAAO,KAAK,CAAC,aAAa,CAAC,MAAA,IAAI,CAAC,QAAQ,0CAAE,OAAO,EAAE,GAAG,CAAC,CAAC;IAC1D,CAAC;CACF;AAvHD,0DAuHC","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 {\n DetailedResult,\n Result,\n captureResult,\n fail,\n failWithDetail,\n succeedWithDetail\n} from '@fgv/ts-utils';\nimport { IJsonContext } from '../../context';\nimport { JsonObject, JsonValue, isJsonObject, pickJsonObject } from '../../json';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * The {@link Editor.Rules.ReferenceJsonEditorRule | Reference JSON editor rule} replaces property\n * keys or values that match some known object with a copy of that referenced object, formatted\n * according to the current context.\n *\n * A property key is matched if it matches any known referenced value.\n * - If the value of the matched key is `'default'`, then the entire object is formatted\n * with the current context, flattened and merged into the current object.\n * - If the value of the matched key is some other string, then the entire\n * object is formatted with the current context, and the child of the resulting\n * object at the specified path is flattened and merged into the current object.\n * - If the value of the matched key is an object, then the entire object is\n * formatted with the current context extended to include any properties of\n * that object, flattened, and merged into the current object.\n * - It is an error if the referenced value is not an object.\n *\n * Any property, array or literal value is matched if it matches any known\n * value reference. The referenced value is replaced by the referenced\n * value, formatted using the current editor context.\n * @public\n */\nexport class ReferenceJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link Editor.IJsonEditorOptions | editor options} for this rule.\n * @public\n */\n protected _options?: IJsonEditorOptions;\n\n /**\n * Creates a new {@link Editor.Rules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options} for this rule.\n */\n public constructor(options?: IJsonEditorOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options} for this rule.\n */\n public static create(options?: IJsonEditorOptions): Result<ReferenceJsonEditorRule> {\n return captureResult(() => new ReferenceJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for reference expansion.\n * @param key - The key of the property to be considered.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns If the reference is successful, returns `Success` with a {@link JsonObject | JsonObject}\n * to be flattened and merged into the current object. Returns `Failure` with detail `'inapplicable'`\n * for non-reference keys or with detail `'error'` if an error occurs.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n /* c8 ignore next 2 */\n const validation = this._options?.validation;\n const refs = state.getRefs(this._options?.context);\n if (refs?.has(key)) {\n // need to apply any rules to the value before we evaluate it\n const cloneResult = state.editor.clone(value, state.context);\n if (cloneResult.isSuccess()) {\n value = cloneResult.value;\n } else {\n const message = `${key}: ${cloneResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n\n const contextResult = this._extendContext(state, value);\n if (contextResult.isSuccess()) {\n const objResult = refs.getJsonObject(key, contextResult.value);\n // guarded by the has above so should never happen\n /* c8 ignore else */\n if (objResult.isSuccess()) {\n if (typeof value !== 'string' || value === 'default') {\n return succeedWithDetail<JsonObject, JsonEditFailureReason>(objResult.value, 'edited');\n }\n const pickResult = pickJsonObject(objResult.value, value);\n if (pickResult.isFailure()) {\n const message = `${key}: ${pickResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n return pickResult.withDetail('edited');\n } else if (objResult.detail !== 'unknown') {\n const message = `${key}: ${objResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n } else {\n const message = `${key}: ${contextResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Evaluates a property, array or literal value for reference replacement.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n */\n public editValue(\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonValue, JsonEditFailureReason> {\n /* c8 ignore next */\n const refs = state.getRefs(this._options?.context);\n\n if (refs && typeof value === 'string') {\n /* c8 ignore next */\n const context = state.getContext(this._options?.context);\n const result = refs.getJsonValue(value, context);\n if (result.isSuccess()) {\n return succeedWithDetail(result.value, 'edited');\n } else if (result.detail === 'error') {\n return state.failValidation('invalidPropertyValue', result.message, this._options?.validation);\n }\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Gets the template variables to use given the value of some property whose name matched a\n * resource plus the base template context.\n * @param state - The {@link Editor.JsonEditorState | editor state} to be extended.\n * @param supplied - The string or object supplied in the source json.\n * @internal\n */\n protected _extendContext(state: JsonEditorState, supplied: JsonValue): Result<IJsonContext | undefined> {\n const add: Record<string, unknown> = {};\n if (isJsonObject(supplied)) {\n add.vars = Object.entries(supplied);\n } else if (typeof supplied !== 'string') {\n return fail(`Invalid template path or context: \"${JSON.stringify(supplied)}\"`);\n }\n return state.extendContext(this._options?.context, add);\n }\n}\n"]}
1
+ {"version":3,"file":"references.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/references.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,oDAAwF;AACxF,4CAOuB;AAGvB,sDAAuD;AAGvD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,uBAAwB,SAAQ,mCAAkB;IAO7D;;;OAGG;IACH,YAAmB,OAA4B;QAC7C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,MAAM,CAAC,OAA4B;QAC/C,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,uBAAuB,CAAC,OAAO,CAAC,CAAC,CAAC;IACnE,CAAC;IAED;;;;;;;;OAQG;IACI,YAAY,CACjB,GAAW,EACX,KAAgB,EAChB,KAAsB;;QAEtB,sBAAsB;QACtB,MAAM,UAAU,GAAG,MAAA,IAAI,CAAC,QAAQ,0C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AC,KAAK,EAAE,QAAQ,CAAC,CAAC;YACnD,CAAC;iBAAM,IAAI,MAAM,CAAC,MAAM,KAAK,OAAO,EAAE,CAAC;gBACrC,OAAO,KAAK,CAAC,cAAc,CAAC,sBAAsB,EAAE,MAAM,CAAC,OAAO,EAAE,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,CAAC,CAAC;YACjG,CAAC;QACH,CAAC;QACD,OAAO,IAAA,yBAAc,EAAC,cAAc,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC;IAED;;;;;;OAMG;IACO,cAAc,CAAC,KAAsB,EAAE,QAAmB;;QAClE,MAAM,GAAG,GAA4B,EAAE,CAAC;QACxC,IAAI,IAAA,2BAAY,EAAC,QAAQ,CAAC,EAAE,CAAC;YAC3B,GAAG,CAAC,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACtC,CAAC;aAAM,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;YACxC,OAAO,IAAA,eAAI,EAAC,sCAAsC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACjF,CAAC;QACD,OAAO,KAAK,CAAC,aAAa,CAAC,MAAA,IAAI,CAAC,QAAQ,0CAAE,OAAO,EAAE,GAAG,CAAC,CAAC;IAC1D,CAAC;CACF;AAvHD,0DAuHC","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 { JsonObject, JsonValue, isJsonObject, pickJsonObject } from '@fgv/ts-json-base';\nimport {\n DetailedResult,\n Result,\n captureResult,\n fail,\n failWithDetail,\n succeedWithDetail\n} from '@fgv/ts-utils';\nimport { IJsonContext } from '../../context';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * The {@link Editor.Rules.ReferenceJsonEditorRule | Reference JSON editor rule} replaces property\n * keys or values that match some known object with a copy of that referenced object, formatted\n * according to the current context.\n *\n * A property key is matched if it matches any known referenced value.\n * - If the value of the matched key is `'default'`, then the entire object is formatted\n * with the current context, flattened and merged into the current object.\n * - If the value of the matched key is some other string, then the entire\n * object is formatted with the current context, and the child of the resulting\n * object at the specified path is flattened and merged into the current object.\n * - If the value of the matched key is an object, then the entire object is\n * formatted with the current context extended to include any properties of\n * that object, flattened, and merged into the current object.\n * - It is an error if the referenced value is not an object.\n *\n * Any property, array or literal value is matched if it matches any known\n * value reference. The referenced value is replaced by the referenced\n * value, formatted using the current editor context.\n * @public\n */\nexport class ReferenceJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link Editor.IJsonEditorOptions | editor options} for this rule.\n * @public\n */\n protected _options?: IJsonEditorOptions;\n\n /**\n * Creates a new {@link Editor.Rules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options} for this rule.\n */\n public constructor(options?: IJsonEditorOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options} for this rule.\n */\n public static create(options?: IJsonEditorOptions): Result<ReferenceJsonEditorRule> {\n return captureResult(() => new ReferenceJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for reference expansion.\n * @param key - The key of the property to be considered.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns If the reference is successful, returns `Success` with a {@link JsonObject | JsonObject}\n * to be flattened and merged into the current object. Returns `Failure` with detail `'inapplicable'`\n * for non-reference keys or with detail `'error'` if an error occurs.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n /* c8 ignore next 2 */\n const validation = this._options?.validation;\n const refs = state.getRefs(this._options?.context);\n if (refs?.has(key)) {\n // need to apply any rules to the value before we evaluate it\n const cloneResult = state.editor.clone(value, state.context);\n if (cloneResult.isSuccess()) {\n value = cloneResult.value;\n } else {\n const message = `${key}: ${cloneResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n\n const contextResult = this._extendContext(state, value);\n if (contextResult.isSuccess()) {\n const objResult = refs.getJsonObject(key, contextResult.value);\n // guarded by the has above so should never happen\n /* c8 ignore else */\n if (objResult.isSuccess()) {\n if (typeof value !== 'string' || value === 'default') {\n return succeedWithDetail<JsonObject, JsonEditFailureReason>(objResult.value, 'edited');\n }\n const pickResult = pickJsonObject(objResult.value, value);\n if (pickResult.isFailure()) {\n const message = `${key}: ${pickResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n return pickResult.withDetail('edited');\n } else if (objResult.detail !== 'unknown') {\n const message = `${key}: ${objResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n } else {\n const message = `${key}: ${contextResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Evaluates a property, array or literal value for reference replacement.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n */\n public editValue(\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonValue, JsonEditFailureReason> {\n /* c8 ignore next */\n const refs = state.getRefs(this._options?.context);\n\n if (refs && typeof value === 'string') {\n /* c8 ignore next */\n const context = state.getContext(this._options?.context);\n const result = refs.getJsonValue(value, context);\n if (result.isSuccess()) {\n return succeedWithDetail(result.value, 'edited');\n } else if (result.detail === 'error') {\n return state.failValidation('invalidPropertyValue', result.message, this._options?.validation);\n }\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Gets the template variables to use given the value of some property whose name matched a\n * resource plus the base template context.\n * @param state - The {@link Editor.JsonEditorState | editor state} to be extended.\n * @param supplied - The string or object supplied in the source json.\n * @internal\n */\n protected _extendContext(state: JsonEditorState, supplied: JsonValue): Result<IJsonContext | undefined> {\n const add: Record<string, unknown> = {};\n if (isJsonObject(supplied)) {\n add.vars = Object.entries(supplied);\n } else if (typeof supplied !== 'string') {\n return fail(`Invalid template path or context: \"${JSON.stringify(supplied)}\"`);\n }\n return state.extendContext(this._options?.context, add);\n }\n}\n"]}
@@ -1,5 +1,5 @@
1
+ import { JsonObject, JsonValue } from '@fgv/ts-json-base';
1
2
  import { DetailedResult, Result } from '@fgv/ts-utils';
2
- import { JsonObject, JsonValue } from '../../json';
3
3
  import { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';
4
4
  import { JsonEditorRuleBase } from '../jsonEditorRule';
5
5
  import { JsonEditorState } from '../jsonEditorState';
@@ -1 +1 @@
1
- {"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/templates.ts"],"names":[],"mappings":"AA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CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;YACnC,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,kBAAQ,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;SAC3F;QACD,OAAO,IAAA,yBAAc,EAAC,cAAc,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC;CACF;AA3GD,0DA2GC","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 } from '../../json';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\nimport Mustache from 'mustache';\n\n/**\n * Configuration options for the {@link Editor.Rules.TemplatedJsonEditorRule | Templated JSON editor rule}.\n * @public\n */\nexport interface ITemplatedJsonRuleOptions extends Partial<IJsonEditorOptions> {\n /**\n * If `true` (default) then templates in property names are rendered\n */\n useNameTemplates?: boolean;\n /**\n * If `true` (default) then templates in property values are rendered\n */\n useValueTemplates?: boolean;\n}\n\n/**\n * The {@link Editor.Rules.TemplatedJsonEditorRule | Templated JSON editor rule} applies mustache rendering as\n * appropriate to any keys or values in the object being edited.\n * @public\n */\nexport class TemplatedJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Fully-resolved {@link Editor.Rules.ITemplatedJsonRuleOptions | configuration options} for this rule.\n * @public\n */\n protected _options?: ITemplatedJsonRuleOptions;\n\n /**\n * Creates a new {@link Editor.Rules.TemplatedJsonEditorRule | TemplatedJsonEditorRule}.\n * @param options - Optional {@link Editor.Rules.ITemplatedJsonRuleOptions | configuration options}\n * for this rule.\n */\n public constructor(options?: ITemplatedJsonRuleOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.TemplatedJsonEditorRule | TemplatedJsonEditorRule}.\n * @param options - Optional {@link Editor.Rules.ITemplatedJsonRuleOptions | configuration options}\n * for this rule.\n */\n public static create(options?: ITemplatedJsonRuleOptions): Result<TemplatedJsonEditorRule> {\n return captureResult(() => new TemplatedJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property name for template rendering.\n * @param key - The key of the property to be considered.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'edited'` and an {@link JsonObject | object} to\n * be flattened and merged if the key contained a template. Returns `Failure` with detail `'error'`\n * if an error occurred or with detail `'inapplicable'` if the property key does not contain\n * a template or if name rendering is disabled.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n /* c8 ignore next */\n const validation = this._options?.validation;\n\n if (this._options?.useNameTemplates !== false) {\n const result = this._render(key, state).onSuccess((newKey) => {\n if (newKey.length < 1) {\n return state.failValidation('invalidPropertyName', `Template \"${key}\" renders empty name.`);\n }\n\n const rtrn: JsonObject = {};\n rtrn[newKey] = value;\n return succeedWithDetail<JsonObject, JsonEditFailureReason>(rtrn, 'edited');\n });\n\n if (result.isFailure() && result.detail === 'error') {\n const message = `Cannot render name ${key}: ${result.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n return result;\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Evaluates a property, array or literal value for template rendering.\n * @param value - The {@link JsonValue | value} to be edited.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'edited'` if the value contained a template and was edited.\n * Returns `Failure` with `'ignore'` if the rendered value should be ignored, with `'error'` if\n * an error occurs, or with `'inapplicable'` if the value was not a string with a template.\n */\n public editValue(\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonValue, JsonEditFailureReason> {\n if (this._options?.useValueTemplates !== false && typeof value === 'string' && value.includes('{{')) {\n const renderResult = this._render(value, state).onSuccess((newValue) => {\n return succeedWithDetail(newValue, 'edited');\n });\n\n if (renderResult.isFailure() && renderResult.detail === 'error') {\n const message = `Cannot render value: ${renderResult.message}`;\n /* c8 ignore next */\n return state.failValidation('invalidPropertyValue', message, this._options?.validation);\n }\n return renderResult;\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Renders a single template string for a supplied {@link Editor.JsonEditorState | editor state}.\n * @param template - The mustache template to be rendered.\n * @param state - The {@link Editor.JsonEditorState | editor state} used to render the template.\n * @returns `Success` if the template is rendered. Returns `Failure` with detail `'error'` if the\n * template could not be rendered (e.g. due to syntax errors) or with detail `'inapplicable'` if the\n * string is not a template.\n * @internal\n */\n protected _render(template: string, state: JsonEditorState): DetailedResult<string, JsonEditFailureReason> {\n const vars = state.getVars(this._options?.context);\n if (vars && template.includes('{{')) {\n return captureResult(() => Mustache.render(template, vars)).withDetail('error', 'edited');\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n}\n"]}
1
+ {"version":3,"file":"templates.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/templates.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;AAGH,4CAAyG;AAEzG,sDAAuD;AAGvD,wDAAgC;AAiBhC;;;;GAIG;AACH,MAAa,uBAAwB,SAAQ,mCAAkB;IAO7D;;;;OAIG;IACH,YAAmB,OAAmC;QACpD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,OAAmC;QACtD,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,uBAAuB,CAAC,OAAO,CAAC,CAAC,CAAC;IACnE,CAAC;IAED;;;;;;;;;OASG;IACI,YAAY,CACjB,GAAW,EACX,KAAgB,EAChB,KAAsB;;QAEtB,oBAAoB;QACpB,MAAM,UAAU,GAAG,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,CAAC;QAE7C,IAAI,CAAA,MAAA,IAAI,CAAC,QAAQ,0CAAE,gBAAgB,MAAK,KAAK,EAAE,CAAC;YAC9C,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,EAAE,EAAE;gBAC3D,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACtB,OAAO,KAAK,CAAC,cAAc,CAAC,qBAAqB,EAAE,aAAa,GAAG,uBAAuB,CAAC,CAAC;gBAC9F,CAAC;gBAED,MAAM,IAAI,GAAe,EAAE,CAAC;gBAC5B,IAAI,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC;gBACrB,OAAO,IAAA,4BAAiB,EAAoC,IAAI,EAAE,QAAQ,CAAC,CAAC;YAC9E,CAAC,CAAC,CA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AAE,KAAsB;;QACxD,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,MAAA,IAAI,CAAC,QAAQ,0CAAE,OAAO,CAAC,CAAC;QACnD,IAAI,IAAI,IAAI,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YACpC,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,kBAAQ,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QAC5F,CAAC;QACD,OAAO,IAAA,yBAAc,EAAC,cAAc,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC;CACF;AA3GD,0DA2GC","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 { JsonObject, JsonValue } from '@fgv/ts-json-base';\nimport { DetailedResult, Result, captureResult, failWithDetail, succeedWithDetail } from '@fgv/ts-utils';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\nimport Mustache from 'mustache';\n\n/**\n * Configuration options for the {@link Editor.Rules.TemplatedJsonEditorRule | Templated JSON editor rule}.\n * @public\n */\nexport interface ITemplatedJsonRuleOptions extends Partial<IJsonEditorOptions> {\n /**\n * If `true` (default) then templates in property names are rendered\n */\n useNameTemplates?: boolean;\n /**\n * If `true` (default) then templates in property values are rendered\n */\n useValueTemplates?: boolean;\n}\n\n/**\n * The {@link Editor.Rules.TemplatedJsonEditorRule | Templated JSON editor rule} applies mustache rendering as\n * appropriate to any keys or values in the object being edited.\n * @public\n */\nexport class TemplatedJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Fully-resolved {@link Editor.Rules.ITemplatedJsonRuleOptions | configuration options} for this rule.\n * @public\n */\n protected _options?: ITemplatedJsonRuleOptions;\n\n /**\n * Creates a new {@link Editor.Rules.TemplatedJsonEditorRule | TemplatedJsonEditorRule}.\n * @param options - Optional {@link Editor.Rules.ITemplatedJsonRuleOptions | configuration options}\n * for this rule.\n */\n public constructor(options?: ITemplatedJsonRuleOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.TemplatedJsonEditorRule | TemplatedJsonEditorRule}.\n * @param options - Optional {@link Editor.Rules.ITemplatedJsonRuleOptions | configuration options}\n * for this rule.\n */\n public static create(options?: ITemplatedJsonRuleOptions): Result<TemplatedJsonEditorRule> {\n return captureResult(() => new TemplatedJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property name for template rendering.\n * @param key - The key of the property to be considered.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'edited'` and an {@link JsonObject | object} to\n * be flattened and merged if the key contained a template. Returns `Failure` with detail `'error'`\n * if an error occurred or with detail `'inapplicable'` if the property key does not contain\n * a template or if name rendering is disabled.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n /* c8 ignore next */\n const validation = this._options?.validation;\n\n if (this._options?.useNameTemplates !== false) {\n const result = this._render(key, state).onSuccess((newKey) => {\n if (newKey.length < 1) {\n return state.failValidation('invalidPropertyName', `Template \"${key}\" renders empty name.`);\n }\n\n const rtrn: JsonObject = {};\n rtrn[newKey] = value;\n return succeedWithDetail<JsonObject, JsonEditFailureReason>(rtrn, 'edited');\n });\n\n if (result.isFailure() && result.detail === 'error') {\n const message = `Cannot render name ${key}: ${result.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n return result;\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Evaluates a property, array or literal value for template rendering.\n * @param value - The {@link JsonValue | value} to be edited.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'edited'` if the value contained a template and was edited.\n * Returns `Failure` with `'ignore'` if the rendered value should be ignored, with `'error'` if\n * an error occurs, or with `'inapplicable'` if the value was not a string with a template.\n */\n public editValue(\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonValue, JsonEditFailureReason> {\n if (this._options?.useValueTemplates !== false && typeof value === 'string' && value.includes('{{')) {\n const renderResult = this._render(value, state).onSuccess((newValue) => {\n return succeedWithDetail(newValue, 'edited');\n });\n\n if (renderResult.isFailure() && renderResult.detail === 'error') {\n const message = `Cannot render value: ${renderResult.message}`;\n /* c8 ignore next */\n return state.failValidation('invalidPropertyValue', message, this._options?.validation);\n }\n return renderResult;\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Renders a single template string for a supplied {@link Editor.JsonEditorState | editor state}.\n * @param template - The mustache template to be rendered.\n * @param state - The {@link Editor.JsonEditorState | editor state} used to render the template.\n * @returns `Success` if the template is rendered. Returns `Failure` with detail `'error'` if the\n * template could not be rendered (e.g. due to syntax errors) or with detail `'inapplicable'` if the\n * string is not a template.\n * @internal\n */\n protected _render(template: string, state: JsonEditorState): DetailedResult<string, JsonEditFailureReason> {\n const vars = state.getVars(this._options?.context);\n if (vars && template.includes('{{')) {\n return captureResult(() => Mustache.render(template, vars)).withDetail('error', 'edited');\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n}\n"]}
package/package.json CHANGED
@@ -1,30 +1,31 @@
1
1
  {
2
2
  "name": "@fgv/ts-json",
3
- "version": "2.1.0",
3
+ "version": "2.1.1-alpha.1",
4
4
  "description": "Typescript utilities for working with JSON",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-json.d.ts",
7
- "repository": {
8
- "type": "git",
9
- "url": "git+https://github.com/ErikFortune/fgv.git"
10
- },
11
7
  "keywords": [
12
8
  "typescript",
13
9
  "json"
14
10
  ],
15
11
  "author": "Erik Fortune",
16
12
  "license": "MIT",
13
+ "bugs": {
14
+ "url": "https://github.com/ErikFortune/fgv/issues"
15
+ },
16
+ "homepage": "https://github.com/ErikFortune/fgv/tree/main/libraries/ts-json#readme",
17
+ "sideEffects": false,
17
18
  "devDependencies": {
18
- "@fgv/ts-utils": "2.1.0",
19
- "@fgv/ts-utils-jest": "2.1.0",
20
- "@types/jest": "^29.5.7",
21
- "@types/mustache": "^4.2.4",
22
- "@types/node": "^20.8.9",
23
- "@typescript-eslint/eslint-plugin": "^6.9.1",
24
- "@typescript-eslint/parser": "^6.9.1",
25
- "eslint": "^8.52.0",
19
+ "@fgv/ts-utils": "2.1.1-alpha.1",
20
+ "@fgv/ts-utils-jest": "2.1.1-alpha.1",
21
+ "@types/jest": "^29.5.11",
22
+ "@types/mustache": "^4.2.5",
23
+ "@types/node": "^20.11.5",
24
+ "@typescript-eslint/eslint-plugin": "^6.19.0",
25
+ "@typescript-eslint/parser": "^6.19.0",
26
+ "eslint": "^8.56.0",
26
27
  "eslint-config-standard": "^17.1.0",
27
- "eslint-plugin-import": "^2.29.0",
28
+ "eslint-plugin-import": "^2.29.1",
28
29
  "eslint-plugin-node": "^11.1.0",
29
30
  "eslint-plugin-promise": "^6.1.1",
30
31
  "jest": "^29.7.0",
@@ -32,17 +33,19 @@
32
33
  "mustache": "^4.2.0",
33
34
  "rimraf": "^5.0.5",
34
35
  "ts-jest": "^29.1.1",
35
- "ts-node": "^10.9.1",
36
- "typescript": "^5.2.2",
37
- "eslint-plugin-n": "^16.2.0",
38
- "@rushstack/heft-node-rig": "~2.3.8",
39
- "@rushstack/heft": "~0.63.0",
36
+ "ts-node": "^10.9.2",
37
+ "typescript": "^5.3.3",
38
+ "eslint-plugin-n": "^16.6.2",
39
+ "@rushstack/heft-node-rig": "~2.4.0",
40
+ "@rushstack/heft": "~0.64.0",
40
41
  "heft-jest": "~1.0.2",
41
- "@types/heft-jest": "1.0.5",
42
- "@microsoft/api-documenter": "^7.23.10"
42
+ "@types/heft-jest": "1.0.6",
43
+ "@microsoft/api-documenter": "^7.23.17",
44
+ "@fgv/ts-json-base": "2.1.1-alpha.1"
43
45
  },
44
46
  "peerDependencies": {
45
- "@fgv/ts-utils": "^2.1.0",
47
+ "@fgv/ts-utils": "2.1.1-alpha.1",
48
+ "@fgv/ts-json-base": "2.1.1-alpha.1",
46
49
  "mustache": "^4.2.0"
47
50
  },
48
51
  "scripts": {