@fgv/ts-json 5.0.0-21 → 5.0.0-23
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.
- package/package.json +6 -6
- package/src/index.ts +0 -29
- package/src/packlets/context/compositeJsonMap.ts +0 -120
- package/src/packlets/context/contextHelpers.ts +0 -221
- package/src/packlets/context/index.ts +0 -33
- package/src/packlets/context/jsonContext.ts +0 -133
- package/src/packlets/converters/converters.ts +0 -117
- package/src/packlets/converters/index.ts +0 -26
- package/src/packlets/converters/jsonConverter.ts +0 -476
- package/src/packlets/diff/detailedDiff.ts +0 -585
- package/src/packlets/diff/index.ts +0 -24
- package/src/packlets/diff/threeWayDiff.ts +0 -420
- package/src/packlets/diff/utils.ts +0 -66
- package/src/packlets/editor/common.ts +0 -125
- package/src/packlets/editor/index.ts +0 -36
- package/src/packlets/editor/jsonEditor.ts +0 -523
- package/src/packlets/editor/jsonEditorRule.ts +0 -117
- package/src/packlets/editor/jsonEditorState.ts +0 -225
- package/src/packlets/editor/jsonReferenceMap.ts +0 -516
- package/src/packlets/editor/rules/conditional.ts +0 -222
- package/src/packlets/editor/rules/index.ts +0 -25
- package/src/packlets/editor/rules/multivalue.ts +0 -206
- package/src/packlets/editor/rules/references.ts +0 -177
- package/src/packlets/editor/rules/templates.ts +0 -159
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2020 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { JsonObject, JsonValue } from '@fgv/ts-json-base';
|
|
24
|
-
import { DetailedResult, Result, captureResult, failWithDetail, succeedWithDetail } from '@fgv/ts-utils';
|
|
25
|
-
import { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';
|
|
26
|
-
import { JsonEditorRuleBase } from '../jsonEditorRule';
|
|
27
|
-
import { JsonEditorState } from '../jsonEditorState';
|
|
28
|
-
|
|
29
|
-
import Mustache from 'mustache';
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Configuration options for the {@link EditorRules.TemplatedJsonEditorRule | Templated JSON editor rule}.
|
|
33
|
-
* @public
|
|
34
|
-
*/
|
|
35
|
-
export interface ITemplatedJsonRuleOptions extends Partial<IJsonEditorOptions> {
|
|
36
|
-
/**
|
|
37
|
-
* If `true` (default) then templates in property names are rendered
|
|
38
|
-
*/
|
|
39
|
-
useNameTemplates?: boolean;
|
|
40
|
-
/**
|
|
41
|
-
* If `true` (default) then templates in property values are rendered
|
|
42
|
-
*/
|
|
43
|
-
useValueTemplates?: boolean;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* The {@link EditorRules.TemplatedJsonEditorRule | Templated JSON editor rule} applies mustache rendering as
|
|
48
|
-
* appropriate to any keys or values in the object being edited.
|
|
49
|
-
* @public
|
|
50
|
-
*/
|
|
51
|
-
export class TemplatedJsonEditorRule extends JsonEditorRuleBase {
|
|
52
|
-
/**
|
|
53
|
-
* Fully-resolved {@link EditorRules.ITemplatedJsonRuleOptions | configuration options} for this rule.
|
|
54
|
-
* @public
|
|
55
|
-
*/
|
|
56
|
-
protected _options?: ITemplatedJsonRuleOptions;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Creates a new {@link EditorRules.TemplatedJsonEditorRule | TemplatedJsonEditorRule}.
|
|
60
|
-
* @param options - Optional {@link EditorRules.ITemplatedJsonRuleOptions | configuration options}
|
|
61
|
-
* for this rule.
|
|
62
|
-
*/
|
|
63
|
-
public constructor(options?: ITemplatedJsonRuleOptions) {
|
|
64
|
-
super();
|
|
65
|
-
this._options = options;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Creates a new {@link EditorRules.TemplatedJsonEditorRule | TemplatedJsonEditorRule}.
|
|
70
|
-
* @param options - Optional {@link EditorRules.ITemplatedJsonRuleOptions | configuration options}
|
|
71
|
-
* for this rule.
|
|
72
|
-
*/
|
|
73
|
-
public static create(options?: ITemplatedJsonRuleOptions): Result<TemplatedJsonEditorRule> {
|
|
74
|
-
return captureResult(() => new TemplatedJsonEditorRule(options));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Evaluates a property name for template rendering.
|
|
79
|
-
* @param key - The key of the property to be considered.
|
|
80
|
-
* @param value - The `JsonValue` of the property to be considered.
|
|
81
|
-
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
82
|
-
* @returns `Success` with detail `'edited'` and an `JsonObject` to
|
|
83
|
-
* be flattened and merged if the key contained a template. Returns `Failure` with detail `'error'`
|
|
84
|
-
* if an error occurred or with detail `'inapplicable'` if the property key does not contain
|
|
85
|
-
* a template or if name rendering is disabled.
|
|
86
|
-
*/
|
|
87
|
-
public editProperty(
|
|
88
|
-
key: string,
|
|
89
|
-
value: JsonValue,
|
|
90
|
-
state: JsonEditorState
|
|
91
|
-
): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {
|
|
92
|
-
/* c8 ignore next 2 */
|
|
93
|
-
const validation = this._options?.validation;
|
|
94
|
-
const useNameTemplates = this._options?.useNameTemplates !== false;
|
|
95
|
-
|
|
96
|
-
if (useNameTemplates !== false) {
|
|
97
|
-
const result = this._render(key, state).onSuccess((newKey) => {
|
|
98
|
-
if (newKey.length < 1) {
|
|
99
|
-
return state.failValidation('invalidPropertyName', `Template "${key}" renders empty name.`);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const rtrn: JsonObject = {};
|
|
103
|
-
rtrn[newKey] = value;
|
|
104
|
-
return succeedWithDetail<JsonObject, JsonEditFailureReason>(rtrn, 'edited');
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
if (result.isFailure() && result.detail === 'error') {
|
|
108
|
-
const message = `Cannot render name ${key}: ${result.message}`;
|
|
109
|
-
return state.failValidation('invalidPropertyName', message, validation);
|
|
110
|
-
}
|
|
111
|
-
return result;
|
|
112
|
-
}
|
|
113
|
-
return failWithDetail('inapplicable', 'inapplicable');
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Evaluates a property, array or literal value for template rendering.
|
|
118
|
-
* @param value - The `JsonValue` to be edited.
|
|
119
|
-
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
120
|
-
* @returns `Success` with detail `'edited'` if the value contained a template and was edited.
|
|
121
|
-
* Returns `Failure` with `'ignore'` if the rendered value should be ignored, with `'error'` if
|
|
122
|
-
* an error occurs, or with `'inapplicable'` if the value was not a string with a template.
|
|
123
|
-
*/
|
|
124
|
-
public editValue(
|
|
125
|
-
value: JsonValue,
|
|
126
|
-
state: JsonEditorState
|
|
127
|
-
): DetailedResult<JsonValue, JsonEditFailureReason> {
|
|
128
|
-
if (this._options?.useValueTemplates !== false && typeof value === 'string' && value.includes('{{')) {
|
|
129
|
-
const renderResult = this._render(value, state).onSuccess((newValue) => {
|
|
130
|
-
return succeedWithDetail(newValue, 'edited');
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
if (renderResult.isFailure() && renderResult.detail === 'error') {
|
|
134
|
-
const message = `Cannot render value: ${renderResult.message}`;
|
|
135
|
-
/* c8 ignore next */
|
|
136
|
-
return state.failValidation('invalidPropertyValue', message, this._options?.validation);
|
|
137
|
-
}
|
|
138
|
-
return renderResult;
|
|
139
|
-
}
|
|
140
|
-
return failWithDetail('inapplicable', 'inapplicable');
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Renders a single template string for a supplied {@link JsonEditorState | editor state}.
|
|
145
|
-
* @param template - The mustache template to be rendered.
|
|
146
|
-
* @param state - The {@link JsonEditorState | editor state} used to render the template.
|
|
147
|
-
* @returns `Success` if the template is rendered. Returns `Failure` with detail `'error'` if the
|
|
148
|
-
* template could not be rendered (e.g. due to syntax errors) or with detail `'inapplicable'` if the
|
|
149
|
-
* string is not a template.
|
|
150
|
-
* @internal
|
|
151
|
-
*/
|
|
152
|
-
protected _render(template: string, state: JsonEditorState): DetailedResult<string, JsonEditFailureReason> {
|
|
153
|
-
const vars = state.getVars(this._options?.context);
|
|
154
|
-
if (vars && template.includes('{{')) {
|
|
155
|
-
return captureResult(() => Mustache.render(template, vars)).withDetail('error', 'edited');
|
|
156
|
-
}
|
|
157
|
-
return failWithDetail('inapplicable', 'inapplicable');
|
|
158
|
-
}
|
|
159
|
-
}
|