@fgv/ts-json 1.9.6 → 2.0.1-alpha.0
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/dist/ts-json.d.ts +1826 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +42 -0
- package/lib/index.js.map +1 -0
- package/lib/packlets/context/compositeJsonMap.d.ts +60 -0
- package/lib/packlets/context/compositeJsonMap.d.ts.map +1 -0
- package/lib/packlets/context/compositeJsonMap.js +105 -0
- package/lib/packlets/context/compositeJsonMap.js.map +1 -0
- package/lib/packlets/context/contextHelpers.d.ts +116 -0
- package/lib/packlets/context/contextHelpers.d.ts.map +1 -0
- package/lib/packlets/context/contextHelpers.js +191 -0
- package/lib/packlets/context/contextHelpers.js.map +1 -0
- package/lib/packlets/context/index.d.ts +4 -0
- package/lib/packlets/context/index.d.ts.map +1 -0
- package/lib/packlets/context/index.js +31 -0
- package/lib/packlets/context/index.js.map +1 -0
- package/lib/packlets/context/jsonContext.d.ts +84 -0
- package/lib/packlets/context/jsonContext.d.ts.map +1 -0
- package/lib/packlets/context/jsonContext.js +41 -0
- package/lib/packlets/context/jsonContext.js.map +1 -0
- package/lib/packlets/converters/converters.d.ts +54 -0
- package/lib/packlets/converters/converters.d.ts.map +1 -0
- package/lib/packlets/converters/converters.js +105 -0
- package/lib/packlets/converters/converters.js.map +1 -0
- package/lib/packlets/converters/file.d.ts +83 -0
- package/lib/packlets/converters/file.d.ts.map +1 -0
- package/lib/packlets/converters/file.js +162 -0
- package/lib/packlets/converters/file.js.map +1 -0
- package/lib/packlets/converters/index.d.ts +5 -0
- package/lib/packlets/converters/index.d.ts.map +1 -0
- package/{jsonEditor → lib/packlets/converters}/index.js +10 -10
- package/lib/packlets/converters/index.js.map +1 -0
- package/lib/packlets/converters/jsonConverter.d.ts +276 -0
- package/lib/packlets/converters/jsonConverter.d.ts.map +1 -0
- package/lib/packlets/converters/jsonConverter.js +310 -0
- package/lib/packlets/converters/jsonConverter.js.map +1 -0
- package/lib/packlets/editor/common.d.ts +67 -0
- package/lib/packlets/editor/common.d.ts.map +1 -0
- package/lib/packlets/editor/common.js +3 -0
- package/lib/packlets/editor/common.js.map +1 -0
- package/lib/packlets/editor/index.d.ts +8 -0
- package/lib/packlets/editor/index.d.ts.map +1 -0
- package/lib/packlets/editor/index.js +58 -0
- package/lib/packlets/editor/index.js.map +1 -0
- package/lib/packlets/editor/jsonEditor.d.ts +155 -0
- package/lib/packlets/editor/jsonEditor.d.ts.map +1 -0
- package/lib/packlets/editor/jsonEditor.js +333 -0
- package/lib/packlets/editor/jsonEditor.js.map +1 -0
- package/lib/packlets/editor/jsonEditorRule.d.ts +64 -0
- package/lib/packlets/editor/jsonEditorRule.d.ts.map +1 -0
- package/lib/packlets/editor/jsonEditorRule.js +54 -0
- package/lib/packlets/editor/jsonEditorRule.js.map +1 -0
- package/lib/packlets/editor/jsonEditorState.d.ts +125 -0
- package/lib/packlets/editor/jsonEditorState.d.ts.map +1 -0
- package/lib/packlets/editor/jsonEditorState.js +177 -0
- package/lib/packlets/editor/jsonEditorState.js.map +1 -0
- package/lib/packlets/editor/jsonReferenceMap.d.ts +303 -0
- package/lib/packlets/editor/jsonReferenceMap.d.ts.map +1 -0
- package/lib/packlets/editor/jsonReferenceMap.js +324 -0
- package/lib/packlets/editor/jsonReferenceMap.js.map +1 -0
- package/lib/packlets/editor/rules/conditional.d.ts +107 -0
- package/lib/packlets/editor/rules/conditional.d.ts.map +1 -0
- package/lib/packlets/editor/rules/conditional.js +167 -0
- package/lib/packlets/editor/rules/conditional.js.map +1 -0
- package/{jsonEditor → lib/packlets/editor}/rules/index.d.ts +1 -0
- package/lib/packlets/editor/rules/index.d.ts.map +1 -0
- package/lib/packlets/editor/rules/index.js +42 -0
- package/lib/packlets/editor/rules/index.js.map +1 -0
- package/lib/packlets/editor/rules/multivalue.d.ts +94 -0
- package/lib/packlets/editor/rules/multivalue.d.ts.map +1 -0
- package/lib/packlets/editor/rules/multivalue.js +141 -0
- package/lib/packlets/editor/rules/multivalue.js.map +1 -0
- package/lib/packlets/editor/rules/references.d.ts +69 -0
- package/lib/packlets/editor/rules/references.d.ts.map +1 -0
- package/lib/packlets/editor/rules/references.js +160 -0
- package/lib/packlets/editor/rules/references.js.map +1 -0
- package/lib/packlets/editor/rules/templates.d.ts +74 -0
- package/lib/packlets/editor/rules/templates.d.ts.map +1 -0
- package/lib/packlets/editor/rules/templates.js +127 -0
- package/lib/packlets/editor/rules/templates.js.map +1 -0
- package/lib/packlets/json/common.d.ts +85 -0
- package/lib/packlets/json/common.d.ts.map +1 -0
- package/lib/packlets/json/common.js +123 -0
- package/lib/packlets/json/common.js.map +1 -0
- package/lib/packlets/json/index.d.ts +2 -0
- package/lib/packlets/json/index.d.ts.map +1 -0
- package/lib/packlets/json/index.js +39 -0
- package/lib/packlets/json/index.js.map +1 -0
- package/package.json +57 -49
- package/common.d.ts +0 -58
- package/common.js +0 -116
- package/compositeJsonMap.d.ts +0 -47
- package/compositeJsonMap.js +0 -97
- package/contextHelpers.d.ts +0 -21
- package/contextHelpers.js +0 -101
- package/converters.d.ts +0 -37
- package/converters.js +0 -92
- package/file.d.ts +0 -60
- package/file.js +0 -140
- package/index.d.ts +0 -10
- package/index.js +0 -62
- package/jsonContext.d.ts +0 -74
- package/jsonContext.js +0 -40
- package/jsonConverter.d.ts +0 -229
- package/jsonConverter.js +0 -292
- package/jsonEditor/common.d.ts +0 -36
- package/jsonEditor/common.js +0 -3
- package/jsonEditor/index.d.ts +0 -5
- package/jsonEditor/jsonEditor.d.ts +0 -74
- package/jsonEditor/jsonEditor.js +0 -259
- package/jsonEditor/jsonEditorRule.d.ts +0 -47
- package/jsonEditor/jsonEditorRule.js +0 -44
- package/jsonEditor/jsonEditorState.d.ts +0 -24
- package/jsonEditor/jsonEditorState.js +0 -86
- package/jsonEditor/rules/conditional.d.ts +0 -84
- package/jsonEditor/rules/conditional.js +0 -146
- package/jsonEditor/rules/index.js +0 -21
- package/jsonEditor/rules/multivalue.d.ts +0 -78
- package/jsonEditor/rules/multivalue.js +0 -129
- package/jsonEditor/rules/references.d.ts +0 -62
- package/jsonEditor/rules/references.js +0 -158
- package/jsonEditor/rules/templates.d.ts +0 -56
- package/jsonEditor/rules/templates.js +0 -115
- package/jsonReferenceMap.d.ts +0 -138
- package/jsonReferenceMap.js +0 -202
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { DetailedResult, Result } from '@fgv/ts-utils';
|
|
2
|
+
import { JsonObject, JsonValue } from '../../json';
|
|
3
|
+
import { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';
|
|
4
|
+
import { JsonEditorRuleBase } from '../jsonEditorRule';
|
|
5
|
+
import { JsonEditorState } from '../jsonEditorState';
|
|
6
|
+
/**
|
|
7
|
+
* Configuration options for the {@link Editor.Rules.TemplatedJsonEditorRule | Templated JSON editor rule}.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export interface ITemplatedJsonRuleOptions extends Partial<IJsonEditorOptions> {
|
|
11
|
+
/**
|
|
12
|
+
* If `true` (default) then templates in property names are rendered
|
|
13
|
+
*/
|
|
14
|
+
useNameTemplates?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* If `true` (default) then templates in property values are rendered
|
|
17
|
+
*/
|
|
18
|
+
useValueTemplates?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The {@link Editor.Rules.TemplatedJsonEditorRule | Templated JSON editor rule} applies mustache rendering as
|
|
22
|
+
* appropriate to any keys or values in the object being edited.
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export declare class TemplatedJsonEditorRule extends JsonEditorRuleBase {
|
|
26
|
+
/**
|
|
27
|
+
* Fully-resolved {@link Editor.Rules.ITemplatedJsonRuleOptions | configuration options} for this rule.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
protected _options?: ITemplatedJsonRuleOptions;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new {@link Editor.Rules.TemplatedJsonEditorRule | TemplatedJsonEditorRule}.
|
|
33
|
+
* @param options - Optional {@link Editor.Rules.ITemplatedJsonRuleOptions | configuration options}
|
|
34
|
+
* for this rule.
|
|
35
|
+
*/
|
|
36
|
+
constructor(options?: ITemplatedJsonRuleOptions);
|
|
37
|
+
/**
|
|
38
|
+
* Creates a new {@link Editor.Rules.TemplatedJsonEditorRule | TemplatedJsonEditorRule}.
|
|
39
|
+
* @param options - Optional {@link Editor.Rules.ITemplatedJsonRuleOptions | configuration options}
|
|
40
|
+
* for this rule.
|
|
41
|
+
*/
|
|
42
|
+
static create(options?: ITemplatedJsonRuleOptions): Result<TemplatedJsonEditorRule>;
|
|
43
|
+
/**
|
|
44
|
+
* Evaluates a property name for template rendering.
|
|
45
|
+
* @param key - The key of the property to be considered.
|
|
46
|
+
* @param value - The {@link JsonValue | value} of the property to be considered.
|
|
47
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
48
|
+
* @returns `Success` with detail `'edited'` and an {@link JsonObject | object} to
|
|
49
|
+
* be flattened and merged if the key contained a template. Returns `Failure` with detail `'error'`
|
|
50
|
+
* if an error occurred or with detail `'inapplicable'` if the property key does not contain
|
|
51
|
+
* a template or if name rendering is disabled.
|
|
52
|
+
*/
|
|
53
|
+
editProperty(key: string, value: JsonValue, state: JsonEditorState): DetailedResult<JsonObject, JsonPropertyEditFailureReason>;
|
|
54
|
+
/**
|
|
55
|
+
* Evaluates a property, array or literal value for template rendering.
|
|
56
|
+
* @param value - The {@link JsonValue | value} to be edited.
|
|
57
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
58
|
+
* @returns `Success` with detail `'edited'` if the value contained a template and was edited.
|
|
59
|
+
* Returns `Failure` with `'ignore'` if the rendered value should be ignored, with `'error'` if
|
|
60
|
+
* an error occurs, or with `'inapplicable'` if the value was not a string with a template.
|
|
61
|
+
*/
|
|
62
|
+
editValue(value: JsonValue, state: JsonEditorState): DetailedResult<JsonValue, JsonEditFailureReason>;
|
|
63
|
+
/**
|
|
64
|
+
* Renders a single template string for a supplied {@link Editor.JsonEditorState | editor state}.
|
|
65
|
+
* @param template - The mustache template to be rendered.
|
|
66
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} used to render the template.
|
|
67
|
+
* @returns `Success` if the template is rendered. Returns `Failure` with detail `'error'` if the
|
|
68
|
+
* template could not be rendered (e.g. due to syntax errors) or with detail `'inapplicable'` if the
|
|
69
|
+
* string is not a template.
|
|
70
|
+
* @internal
|
|
71
|
+
*/
|
|
72
|
+
protected _render(template: string, state: JsonEditorState): DetailedResult<string, JsonEditFailureReason>;
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=templates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/templates.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,cAAc,EAAE,MAAM,EAAoD,MAAM,eAAe,CAAC;AACzG,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACnD,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;AAIrD;;;GAGG;AACH,MAAM,WAAW,yBAA0B,SAAQ,OAAO,CAAC,kBAAkB,CAAC;IAC5E;;OAEG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B;;OAEG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED;;;;GAIG;AACH,qBAAa,uBAAwB,SAAQ,kBAAkB;IAC7D;;;OAGG;IACH,SAAS,CAAC,QAAQ,CAAC,EAAE,yBAAyB,CAAC;IAE/C;;;;OAIG;gBACgB,OAAO,CAAC,EAAE,yBAAyB;IAKtD;;;;OAIG;WACW,MAAM,CAAC,OAAO,CAAC,EAAE,yBAAyB,GAAG,MAAM,CAAC,uBAAuB,CAAC;IAI1F;;;;;;;;;OASG;IACI,YAAY,CACjB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,SAAS,EAChB,KAAK,EAAE,eAAe,GACrB,cAAc,CAAC,UAAU,EAAE,6BAA6B,CAAC;IAwB5D;;;;;;;OAOG;IACI,SAAS,CACd,KAAK,EAAE,SAAS,EAChB,KAAK,EAAE,eAAe,GACrB,cAAc,CAAC,SAAS,EAAE,qBAAqB,CAAC;IAgBnD;;;;;;;;OAQG;IACH,SAAS,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,eAAe,GAAG,cAAc,CAAC,MAAM,EAAE,qBAAqB,CAAC;CAO3G"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2020 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
24
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
25
|
+
};
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.TemplatedJsonEditorRule = void 0;
|
|
28
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
29
|
+
const jsonEditorRule_1 = require("../jsonEditorRule");
|
|
30
|
+
const mustache_1 = __importDefault(require("mustache"));
|
|
31
|
+
/**
|
|
32
|
+
* The {@link Editor.Rules.TemplatedJsonEditorRule | Templated JSON editor rule} applies mustache rendering as
|
|
33
|
+
* appropriate to any keys or values in the object being edited.
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
class TemplatedJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
37
|
+
/**
|
|
38
|
+
* Creates a new {@link Editor.Rules.TemplatedJsonEditorRule | TemplatedJsonEditorRule}.
|
|
39
|
+
* @param options - Optional {@link Editor.Rules.ITemplatedJsonRuleOptions | configuration options}
|
|
40
|
+
* for this rule.
|
|
41
|
+
*/
|
|
42
|
+
constructor(options) {
|
|
43
|
+
super();
|
|
44
|
+
this._options = options;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Creates a new {@link Editor.Rules.TemplatedJsonEditorRule | TemplatedJsonEditorRule}.
|
|
48
|
+
* @param options - Optional {@link Editor.Rules.ITemplatedJsonRuleOptions | configuration options}
|
|
49
|
+
* for this rule.
|
|
50
|
+
*/
|
|
51
|
+
static create(options) {
|
|
52
|
+
return (0, ts_utils_1.captureResult)(() => new TemplatedJsonEditorRule(options));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Evaluates a property name for template rendering.
|
|
56
|
+
* @param key - The key of the property to be considered.
|
|
57
|
+
* @param value - The {@link JsonValue | value} of the property to be considered.
|
|
58
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
59
|
+
* @returns `Success` with detail `'edited'` and an {@link JsonObject | object} to
|
|
60
|
+
* be flattened and merged if the key contained a template. Returns `Failure` with detail `'error'`
|
|
61
|
+
* if an error occurred or with detail `'inapplicable'` if the property key does not contain
|
|
62
|
+
* a template or if name rendering is disabled.
|
|
63
|
+
*/
|
|
64
|
+
editProperty(key, value, state) {
|
|
65
|
+
var _a, _b;
|
|
66
|
+
// istanbul ignore next
|
|
67
|
+
const validation = (_a = this._options) === null || _a === void 0 ? void 0 : _a.validation;
|
|
68
|
+
if (((_b = this._options) === null || _b === void 0 ? void 0 : _b.useNameTemplates) !== false) {
|
|
69
|
+
const result = this._render(key, state).onSuccess((newKey) => {
|
|
70
|
+
if (newKey.length < 1) {
|
|
71
|
+
return state.failValidation('invalidPropertyName', `Template "${key}" renders empty name.`);
|
|
72
|
+
}
|
|
73
|
+
const rtrn = {};
|
|
74
|
+
rtrn[newKey] = value;
|
|
75
|
+
return (0, ts_utils_1.succeedWithDetail)(rtrn, 'edited');
|
|
76
|
+
});
|
|
77
|
+
if (result.isFailure() && result.detail === 'error') {
|
|
78
|
+
const message = `Cannot render name ${key}: ${result.message}`;
|
|
79
|
+
return state.failValidation('invalidPropertyName', message, validation);
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
return (0, ts_utils_1.failWithDetail)('inapplicable', 'inapplicable');
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Evaluates a property, array or literal value for template rendering.
|
|
87
|
+
* @param value - The {@link JsonValue | value} to be edited.
|
|
88
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
89
|
+
* @returns `Success` with detail `'edited'` if the value contained a template and was edited.
|
|
90
|
+
* Returns `Failure` with `'ignore'` if the rendered value should be ignored, with `'error'` if
|
|
91
|
+
* an error occurs, or with `'inapplicable'` if the value was not a string with a template.
|
|
92
|
+
*/
|
|
93
|
+
editValue(value, state) {
|
|
94
|
+
var _a, _b;
|
|
95
|
+
if (((_a = this._options) === null || _a === void 0 ? void 0 : _a.useValueTemplates) !== false && typeof value === 'string' && value.includes('{{')) {
|
|
96
|
+
const renderResult = this._render(value, state).onSuccess((newValue) => {
|
|
97
|
+
return (0, ts_utils_1.succeedWithDetail)(newValue, 'edited');
|
|
98
|
+
});
|
|
99
|
+
if (renderResult.isFailure() && renderResult.detail === 'error') {
|
|
100
|
+
const message = `Cannot render value: ${renderResult.message}`;
|
|
101
|
+
// istanbul ignore next
|
|
102
|
+
return state.failValidation('invalidPropertyValue', message, (_b = this._options) === null || _b === void 0 ? void 0 : _b.validation);
|
|
103
|
+
}
|
|
104
|
+
return renderResult;
|
|
105
|
+
}
|
|
106
|
+
return (0, ts_utils_1.failWithDetail)('inapplicable', 'inapplicable');
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Renders a single template string for a supplied {@link Editor.JsonEditorState | editor state}.
|
|
110
|
+
* @param template - The mustache template to be rendered.
|
|
111
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} used to render the template.
|
|
112
|
+
* @returns `Success` if the template is rendered. Returns `Failure` with detail `'error'` if the
|
|
113
|
+
* template could not be rendered (e.g. due to syntax errors) or with detail `'inapplicable'` if the
|
|
114
|
+
* string is not a template.
|
|
115
|
+
* @internal
|
|
116
|
+
*/
|
|
117
|
+
_render(template, state) {
|
|
118
|
+
var _a;
|
|
119
|
+
const vars = state.getVars((_a = this._options) === null || _a === void 0 ? void 0 : _a.context);
|
|
120
|
+
if (vars && template.includes('{{')) {
|
|
121
|
+
return (0, ts_utils_1.captureResult)(() => mustache_1.default.render(template, vars)).withDetail('error', 'edited');
|
|
122
|
+
}
|
|
123
|
+
return (0, ts_utils_1.failWithDetail)('inapplicable', 'inapplicable');
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
exports.TemplatedJsonEditorRule = TemplatedJsonEditorRule;
|
|
127
|
+
//# sourceMappingURL=templates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templates.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/templates.ts"],"names":[],"mappings":"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","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 // istanbul 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 // istanbul 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"]}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Result } from '@fgv/ts-utils';
|
|
2
|
+
/**
|
|
3
|
+
* Primitive (terminal) values allowed in by JSON.
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export type JsonPrimitive = boolean | number | string | null;
|
|
7
|
+
/**
|
|
8
|
+
* A {@link JsonObject | JsonObject} is a string-keyed object
|
|
9
|
+
* containing only valid {@link JsonValue | JSON values}.
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
export interface JsonObject {
|
|
13
|
+
[key: string]: JsonValue;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A {@link JsonValue | JsonValue} is one of: a {@link JsonPrimitive | JsonPrimitive},
|
|
17
|
+
* a {@link JsonObject | JsonObject} or an {@link JsonArray | JsonArray}.
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
|
|
21
|
+
/**
|
|
22
|
+
* A {@link JsonArray | JsonArray} is an array containing only valid {@link JsonValue | JsonValues}.
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export interface JsonArray extends Array<JsonValue> {
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Classes of {@link JsonValue | JsonValue}.
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export type JsonValueType = 'primitive' | 'object' | 'array';
|
|
32
|
+
/**
|
|
33
|
+
* Test if an `unknown` is a {@link JsonValue | JsonValue}.
|
|
34
|
+
* @param from - The `unknown` to be tested
|
|
35
|
+
* @returns `true` if the supplied parameter is a valid {@link JsonPrimitive | JsonPrimitive},
|
|
36
|
+
* `false` otherwise.
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
export declare function isJsonPrimitive(from: unknown): from is JsonPrimitive;
|
|
40
|
+
/**
|
|
41
|
+
* Test if an `unknown` is potentially a {@link JsonObject | JsonObject}.
|
|
42
|
+
* @param from - The `unknown` to be tested.
|
|
43
|
+
* @returns `true` if the supplied parameter is a non-array, non-special object,
|
|
44
|
+
* `false` otherwise.
|
|
45
|
+
* @public
|
|
46
|
+
*/
|
|
47
|
+
export declare function isJsonObject(from: unknown): from is JsonObject;
|
|
48
|
+
/**
|
|
49
|
+
* Test if an `unknown` is potentially a {@link JsonArray | JsonArray}.
|
|
50
|
+
* @param from - The `unknown` to be tested.
|
|
51
|
+
* @returns `true` if the supplied parameter is an array object,
|
|
52
|
+
* `false` otherwise.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export declare function isJsonArray(from: unknown): from is JsonArray;
|
|
56
|
+
/**
|
|
57
|
+
* Identifies whether some `unknown` value is a {@link JsonPrimitive | primitive},
|
|
58
|
+
* {@link JsonObject | object} or {@link JsonArray | array}. Fails for any value
|
|
59
|
+
* that cannot be converted to JSON (e.g. a function) _but_ this is a shallow test -
|
|
60
|
+
* it does not test the properties of an object or elements in an array.
|
|
61
|
+
* @param from - The `unknown` value to be tested
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
export declare function classifyJsonValue(from: unknown): Result<JsonValueType>;
|
|
65
|
+
/**
|
|
66
|
+
* Picks a nested field from a supplied {@link JsonObject | JsonObject}.
|
|
67
|
+
* @param src - The {@link JsonObject | object} from which the field is to be picked.
|
|
68
|
+
* @param path - Dot-separated path of the member to be picked.
|
|
69
|
+
* @returns `Success` with the property if the path is valid, `Failure`
|
|
70
|
+
* with an error message otherwise.
|
|
71
|
+
* @public
|
|
72
|
+
*/
|
|
73
|
+
export declare function pickJsonValue(src: JsonObject, path: string): Result<JsonValue>;
|
|
74
|
+
/**
|
|
75
|
+
* Picks a nested {@link JsonObject | JsonObject} from a supplied
|
|
76
|
+
* {@link JsonObject | JsonObject}.
|
|
77
|
+
* @param src - The {@link JsonObject | object} from which the field is
|
|
78
|
+
* to be picked.
|
|
79
|
+
* @param path - Dot-separated path of the member to be picked.
|
|
80
|
+
* @returns `Success` with the property if the path is valid and the value
|
|
81
|
+
* is an object. Returns `Failure` with details if an error occurs.
|
|
82
|
+
* @public
|
|
83
|
+
*/
|
|
84
|
+
export declare function pickJsonObject(src: JsonObject, path: string): Result<JsonObject>;
|
|
85
|
+
//# sourceMappingURL=common.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../../src/packlets/json/common.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,MAAM,EAAiB,MAAM,eAAe,CAAC;AAItD;;;GAGG;AAEH,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;AAE7D;;;;GAIG;AAEH,MAAM,WAAW,UAAU;IACzB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1B;AAED;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG,aAAa,GAAG,UAAU,GAAG,SAAS,CAAC;AAE/D;;;GAGG;AAEH,MAAM,WAAW,SAAU,SAAQ,KAAK,CAAC,SAAS,CAAC;CAAG;AAEtD;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,WAAW,GAAG,QAAQ,GAAG,OAAO,CAAC;AAE7D;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,aAAa,CAEpE;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,UAAU,CAI9D;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,SAAS,CAE5D;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC,aAAa,CAAC,CAStE;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,CAa9E;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC,CAOhF"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2020 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.pickJsonObject = exports.pickJsonValue = exports.classifyJsonValue = exports.isJsonArray = exports.isJsonObject = exports.isJsonPrimitive = void 0;
|
|
25
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
26
|
+
/**
|
|
27
|
+
* Test if an `unknown` is a {@link JsonValue | JsonValue}.
|
|
28
|
+
* @param from - The `unknown` to be tested
|
|
29
|
+
* @returns `true` if the supplied parameter is a valid {@link JsonPrimitive | JsonPrimitive},
|
|
30
|
+
* `false` otherwise.
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
function isJsonPrimitive(from) {
|
|
34
|
+
return typeof from === 'boolean' || typeof from === 'number' || typeof from === 'string' || from === null;
|
|
35
|
+
}
|
|
36
|
+
exports.isJsonPrimitive = isJsonPrimitive;
|
|
37
|
+
/**
|
|
38
|
+
* Test if an `unknown` is potentially a {@link JsonObject | JsonObject}.
|
|
39
|
+
* @param from - The `unknown` to be tested.
|
|
40
|
+
* @returns `true` if the supplied parameter is a non-array, non-special object,
|
|
41
|
+
* `false` otherwise.
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
function isJsonObject(from) {
|
|
45
|
+
return (typeof from === 'object' && !Array.isArray(from) && !(from instanceof RegExp) && !(from instanceof Date));
|
|
46
|
+
}
|
|
47
|
+
exports.isJsonObject = isJsonObject;
|
|
48
|
+
/**
|
|
49
|
+
* Test if an `unknown` is potentially a {@link JsonArray | JsonArray}.
|
|
50
|
+
* @param from - The `unknown` to be tested.
|
|
51
|
+
* @returns `true` if the supplied parameter is an array object,
|
|
52
|
+
* `false` otherwise.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
function isJsonArray(from) {
|
|
56
|
+
return typeof from === 'object' && Array.isArray(from);
|
|
57
|
+
}
|
|
58
|
+
exports.isJsonArray = isJsonArray;
|
|
59
|
+
/**
|
|
60
|
+
* Identifies whether some `unknown` value is a {@link JsonPrimitive | primitive},
|
|
61
|
+
* {@link JsonObject | object} or {@link JsonArray | array}. Fails for any value
|
|
62
|
+
* that cannot be converted to JSON (e.g. a function) _but_ this is a shallow test -
|
|
63
|
+
* it does not test the properties of an object or elements in an array.
|
|
64
|
+
* @param from - The `unknown` value to be tested
|
|
65
|
+
* @public
|
|
66
|
+
*/
|
|
67
|
+
function classifyJsonValue(from) {
|
|
68
|
+
if (isJsonPrimitive(from)) {
|
|
69
|
+
return (0, ts_utils_1.succeed)('primitive');
|
|
70
|
+
}
|
|
71
|
+
else if (isJsonObject(from)) {
|
|
72
|
+
return (0, ts_utils_1.succeed)('object');
|
|
73
|
+
}
|
|
74
|
+
else if (isJsonArray(from)) {
|
|
75
|
+
return (0, ts_utils_1.succeed)('array');
|
|
76
|
+
}
|
|
77
|
+
return (0, ts_utils_1.fail)(`Invalid JSON: ${from}`);
|
|
78
|
+
}
|
|
79
|
+
exports.classifyJsonValue = classifyJsonValue;
|
|
80
|
+
/**
|
|
81
|
+
* Picks a nested field from a supplied {@link JsonObject | JsonObject}.
|
|
82
|
+
* @param src - The {@link JsonObject | object} from which the field is to be picked.
|
|
83
|
+
* @param path - Dot-separated path of the member to be picked.
|
|
84
|
+
* @returns `Success` with the property if the path is valid, `Failure`
|
|
85
|
+
* with an error message otherwise.
|
|
86
|
+
* @public
|
|
87
|
+
*/
|
|
88
|
+
function pickJsonValue(src, path) {
|
|
89
|
+
let result = src;
|
|
90
|
+
for (const part of path.split('.')) {
|
|
91
|
+
if (result && isJsonObject(result)) {
|
|
92
|
+
result = result[part];
|
|
93
|
+
if (result === undefined) {
|
|
94
|
+
return (0, ts_utils_1.fail)(`${path}: child '${part}' does not exist`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
return (0, ts_utils_1.fail)(`${path}: child '${part}' does not exist`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return (0, ts_utils_1.succeed)(result);
|
|
102
|
+
}
|
|
103
|
+
exports.pickJsonValue = pickJsonValue;
|
|
104
|
+
/**
|
|
105
|
+
* Picks a nested {@link JsonObject | JsonObject} from a supplied
|
|
106
|
+
* {@link JsonObject | JsonObject}.
|
|
107
|
+
* @param src - The {@link JsonObject | object} from which the field is
|
|
108
|
+
* to be picked.
|
|
109
|
+
* @param path - Dot-separated path of the member to be picked.
|
|
110
|
+
* @returns `Success` with the property if the path is valid and the value
|
|
111
|
+
* is an object. Returns `Failure` with details if an error occurs.
|
|
112
|
+
* @public
|
|
113
|
+
*/
|
|
114
|
+
function pickJsonObject(src, path) {
|
|
115
|
+
return pickJsonValue(src, path).onSuccess((v) => {
|
|
116
|
+
if (!isJsonObject(v)) {
|
|
117
|
+
return (0, ts_utils_1.fail)(`${path}: not an object`);
|
|
118
|
+
}
|
|
119
|
+
return (0, ts_utils_1.succeed)(v);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
exports.pickJsonObject = pickJsonObject;
|
|
123
|
+
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/packlets/json/common.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAAsD;AAyCtD;;;;;;GAMG;AACH,SAAgB,eAAe,CAAC,IAAa;IAC3C,OAAO,OAAO,IAAI,KAAK,SAAS,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,CAAC;AAC5G,CAAC;AAFD,0CAEC;AAED;;;;;;GAMG;AACH,SAAgB,YAAY,CAAC,IAAa;IACxC,OAAO,CACL,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,YAAY,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,YAAY,IAAI,CAAC,CACzG,CAAC;AACJ,CAAC;AAJD,oCAIC;AAED;;;;;;GAMG;AACH,SAAgB,WAAW,CAAC,IAAa;IACvC,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;AACzD,CAAC;AAFD,kCAEC;AAED;;;;;;;GAOG;AACH,SAAgB,iBAAiB,CAAC,IAAa;IAC7C,IAAI,eAAe,CAAC,IAAI,CAAC,EAAE;QACzB,OAAO,IAAA,kBAAO,EAAC,WAAW,CAAC,CAAC;KAC7B;SAAM,IAAI,YAAY,CAAC,IAAI,CAAC,EAAE;QAC7B,OAAO,IAAA,kBAAO,EAAC,QAAQ,CAAC,CAAC;KAC1B;SAAM,IAAI,WAAW,CAAC,IAAI,CAAC,EAAE;QAC5B,OAAO,IAAA,kBAAO,EAAC,OAAO,CAAC,CAAC;KACzB;IACD,OAAO,IAAA,eAAI,EAAC,iBAAiB,IAAI,EAAE,CAAC,CAAC;AACvC,CAAC;AATD,8CASC;AAED;;;;;;;GAOG;AACH,SAAgB,aAAa,CAAC,GAAe,EAAE,IAAY;IACzD,IAAI,MAAM,GAAc,GAAG,CAAC;IAC5B,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;QAClC,IAAI,MAAM,IAAI,YAAY,CAAC,MAAM,CAAC,EAAE;YAClC,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;YACtB,IAAI,MAAM,KAAK,SAAS,EAAE;gBACxB,OAAO,IAAA,eAAI,EAAC,GAAG,IAAI,YAAY,IAAI,kBAAkB,CAAC,CAAC;aACxD;SACF;aAAM;YACL,OAAO,IAAA,eAAI,EAAC,GAAG,IAAI,YAAY,IAAI,kBAAkB,CAAC,CAAC;SACxD;KACF;IACD,OAAO,IAAA,kBAAO,EAAC,MAAM,CAAC,CAAC;AACzB,CAAC;AAbD,sCAaC;AAED;;;;;;;;;GASG;AACH,SAAgB,cAAc,CAAC,GAAe,EAAE,IAAY;IAC1D,OAAO,aAAa,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE;QAC9C,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE;YACpB,OAAO,IAAA,eAAI,EAAC,GAAG,IAAI,iBAAiB,CAAC,CAAC;SACvC;QACD,OAAO,IAAA,kBAAO,EAAC,CAAC,CAAC,CAAC;IACpB,CAAC,CAAC,CAAC;AACL,CAAC;AAPD,wCAOC","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 { Result, fail, succeed } from '@fgv/ts-utils';\n\n/* eslint-disable no-use-before-define */\n\n/**\n * Primitive (terminal) values allowed in by JSON.\n * @public\n */\n// eslint-disable-next-line @rushstack/no-new-null\nexport type JsonPrimitive = boolean | number | string | null;\n\n/**\n * A {@link JsonObject | JsonObject} is a string-keyed object\n * containing only valid {@link JsonValue | JSON values}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface JsonObject {\n [key: string]: JsonValue;\n}\n\n/**\n * A {@link JsonValue | JsonValue} is one of: a {@link JsonPrimitive | JsonPrimitive},\n * a {@link JsonObject | JsonObject} or an {@link JsonArray | JsonArray}.\n * @public\n */\nexport type JsonValue = JsonPrimitive | JsonObject | JsonArray;\n\n/**\n * A {@link JsonArray | JsonArray} is an array containing only valid {@link JsonValue | JsonValues}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/naming-convention\nexport interface JsonArray extends Array<JsonValue> {}\n\n/**\n * Classes of {@link JsonValue | JsonValue}.\n * @public\n */\nexport type JsonValueType = 'primitive' | 'object' | 'array';\n\n/**\n * Test if an `unknown` is a {@link JsonValue | JsonValue}.\n * @param from - The `unknown` to be tested\n * @returns `true` if the supplied parameter is a valid {@link JsonPrimitive | JsonPrimitive},\n * `false` otherwise.\n * @public\n */\nexport function isJsonPrimitive(from: unknown): from is JsonPrimitive {\n return typeof from === 'boolean' || typeof from === 'number' || typeof from === 'string' || from === null;\n}\n\n/**\n * Test if an `unknown` is potentially a {@link JsonObject | JsonObject}.\n * @param from - The `unknown` to be tested.\n * @returns `true` if the supplied parameter is a non-array, non-special object,\n * `false` otherwise.\n * @public\n */\nexport function isJsonObject(from: unknown): from is JsonObject {\n return (\n typeof from === 'object' && !Array.isArray(from) && !(from instanceof RegExp) && !(from instanceof Date)\n );\n}\n\n/**\n * Test if an `unknown` is potentially a {@link JsonArray | JsonArray}.\n * @param from - The `unknown` to be tested.\n * @returns `true` if the supplied parameter is an array object,\n * `false` otherwise.\n * @public\n */\nexport function isJsonArray(from: unknown): from is JsonArray {\n return typeof from === 'object' && Array.isArray(from);\n}\n\n/**\n * Identifies whether some `unknown` value is a {@link JsonPrimitive | primitive},\n * {@link JsonObject | object} or {@link JsonArray | array}. Fails for any value\n * that cannot be converted to JSON (e.g. a function) _but_ this is a shallow test -\n * it does not test the properties of an object or elements in an array.\n * @param from - The `unknown` value to be tested\n * @public\n */\nexport function classifyJsonValue(from: unknown): Result<JsonValueType> {\n if (isJsonPrimitive(from)) {\n return succeed('primitive');\n } else if (isJsonObject(from)) {\n return succeed('object');\n } else if (isJsonArray(from)) {\n return succeed('array');\n }\n return fail(`Invalid JSON: ${from}`);\n}\n\n/**\n * Picks a nested field from a supplied {@link JsonObject | JsonObject}.\n * @param src - The {@link JsonObject | object} from which the field is to be picked.\n * @param path - Dot-separated path of the member to be picked.\n * @returns `Success` with the property if the path is valid, `Failure`\n * with an error message otherwise.\n * @public\n */\nexport function pickJsonValue(src: JsonObject, path: string): Result<JsonValue> {\n let result: JsonValue = src;\n for (const part of path.split('.')) {\n if (result && isJsonObject(result)) {\n result = result[part];\n if (result === undefined) {\n return fail(`${path}: child '${part}' does not exist`);\n }\n } else {\n return fail(`${path}: child '${part}' does not exist`);\n }\n }\n return succeed(result);\n}\n\n/**\n * Picks a nested {@link JsonObject | JsonObject} from a supplied\n * {@link JsonObject | JsonObject}.\n * @param src - The {@link JsonObject | object} from which the field is\n * to be picked.\n * @param path - Dot-separated path of the member to be picked.\n * @returns `Success` with the property if the path is valid and the value\n * is an object. Returns `Failure` with details if an error occurs.\n * @public\n */\nexport function pickJsonObject(src: JsonObject, path: string): Result<JsonObject> {\n return pickJsonValue(src, path).onSuccess((v) => {\n if (!isJsonObject(v)) {\n return fail(`${path}: not an object`);\n }\n return succeed(v);\n });\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/json/index.ts"],"names":[],"mappings":"AAsBA,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2020 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
24
|
+
if (k2 === undefined) k2 = k;
|
|
25
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
26
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
27
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
28
|
+
}
|
|
29
|
+
Object.defineProperty(o, k2, desc);
|
|
30
|
+
}) : (function(o, m, k, k2) {
|
|
31
|
+
if (k2 === undefined) k2 = k;
|
|
32
|
+
o[k2] = m[k];
|
|
33
|
+
}));
|
|
34
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
35
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
__exportStar(require("./common"), exports);
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/json/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;AAEH,2CAAyB","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\nexport * from './common';\n"]}
|
package/package.json
CHANGED
|
@@ -1,51 +1,59 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
2
|
+
"name": "@fgv/ts-json",
|
|
3
|
+
"version": "2.0.1-alpha.0",
|
|
4
|
+
"description": "Typescript utilities for working with JSON",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/DidjaRedo/ts-json.git"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"typescript",
|
|
12
|
+
"json"
|
|
13
|
+
],
|
|
14
|
+
"author": "Erik Fortune",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@fgv/ts-utils": "2.0.1-alpha.0",
|
|
18
|
+
"@fgv/ts-utils-jest": "2.0.1-alpha.0",
|
|
19
|
+
"@types/jest": "^29.5.0",
|
|
20
|
+
"@types/mustache": "^4.2.2",
|
|
21
|
+
"@types/node": "^18.15.10",
|
|
22
|
+
"@typescript-eslint/eslint-plugin": "^5.58.0",
|
|
23
|
+
"@typescript-eslint/parser": "^5.58.0",
|
|
24
|
+
"eslint": "^8.38.0",
|
|
25
|
+
"eslint-config-standard": "^17.0.0",
|
|
26
|
+
"eslint-plugin-import": "^2.27.5",
|
|
27
|
+
"eslint-plugin-node": "^11.1.0",
|
|
28
|
+
"eslint-plugin-promise": "^6.1.1",
|
|
29
|
+
"jest": "^29.5.0",
|
|
30
|
+
"jest-extended": "^3.2.4",
|
|
31
|
+
"mustache": "^4.2.0",
|
|
32
|
+
"rimraf": "^5.0.0",
|
|
33
|
+
"ts-jest": "^29.0.5",
|
|
34
|
+
"ts-node": "^10.9.1",
|
|
35
|
+
"typescript": "^5.0.4",
|
|
36
|
+
"eslint-plugin-n": "^15.0.0",
|
|
37
|
+
"@rushstack/heft-node-rig": "~1.12.6",
|
|
38
|
+
"@rushstack/heft": "~0.50.0",
|
|
39
|
+
"heft-jest": "~1.0.2",
|
|
40
|
+
"@types/heft-jest": "1.0.3",
|
|
41
|
+
"@microsoft/api-documenter": "^7.21.7"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@fgv/ts-utils": "^2.0.1-alpha.0",
|
|
45
|
+
"mustache": "^4.2.0"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "heft test --clean",
|
|
49
|
+
"clean": "heft clean",
|
|
50
|
+
"test": "heft test",
|
|
51
|
+
"build-docs": "api-documenter markdown --input-folder ./temp --output-folder docs",
|
|
52
|
+
"build-all": "rushx build; rushx build-docs",
|
|
53
|
+
"test-handles": "jest --runInBand --detectOpenHandles",
|
|
54
|
+
"clean-jest": "jest --clear-cache",
|
|
55
|
+
"coverage": "jest --coverage --no-cache",
|
|
56
|
+
"lint": "eslint src --ext .ts",
|
|
57
|
+
"fixlint": "eslint src --ext .ts --fix"
|
|
58
|
+
}
|
|
51
59
|
}
|