@fgv/ts-json 5.0.1-8 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.browser.js +24 -0
- package/dist/index.js +27 -0
- package/dist/packlets/context/compositeJsonMap.js +100 -0
- package/dist/packlets/context/contextHelpers.js +187 -0
- package/dist/packlets/context/index.js +25 -0
- package/dist/packlets/context/jsonContext.js +38 -0
- package/dist/packlets/converters/converters.js +99 -0
- package/dist/packlets/converters/index.js +25 -0
- package/dist/packlets/converters/jsonConverter.js +299 -0
- package/dist/packlets/diff/detailedDiff.js +338 -0
- package/dist/packlets/diff/index.js +24 -0
- package/dist/packlets/diff/threeWayDiff.js +258 -0
- package/dist/packlets/diff/utils.js +59 -0
- package/dist/packlets/editor/common.js +2 -0
- package/dist/packlets/editor/index.js +29 -0
- package/dist/packlets/editor/jsonEditor.js +416 -0
- package/dist/packlets/editor/jsonEditorRule.js +50 -0
- package/dist/packlets/editor/jsonEditorState.js +175 -0
- package/dist/packlets/editor/jsonReferenceMap.js +315 -0
- package/dist/packlets/editor/rules/conditional.js +163 -0
- package/dist/packlets/editor/rules/index.js +26 -0
- package/dist/packlets/editor/rules/multivalue.js +137 -0
- package/dist/packlets/editor/rules/references.js +155 -0
- package/dist/packlets/editor/rules/templates.js +121 -0
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/index.browser.d.ts +2 -0
- package/lib/index.browser.js +40 -0
- package/package.json +21 -6
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
import { isJsonObject } from '@fgv/ts-json-base';
|
|
23
|
+
import { Conversion, captureResult, fail, succeed } from '@fgv/ts-utils';
|
|
24
|
+
import { defaultExtendVars } from '../context';
|
|
25
|
+
import { EditorRules, JsonEditor } from '../editor';
|
|
26
|
+
/**
|
|
27
|
+
* Merges an optionally supplied partial set of {@link IJsonConverterOptions | options} with
|
|
28
|
+
* the default converter options and taking all dynamic rules into account (e.g. template usage enabled
|
|
29
|
+
* if variables are supplied and disabled if not), producing a fully-resolved set of
|
|
30
|
+
* {@link IJsonConverterOptions | IJsonConverterOptions}.
|
|
31
|
+
* @param partial - An optional partial {@link IJsonConverterOptions | IJsonConverterOptions}
|
|
32
|
+
* to be merged.
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export function mergeDefaultJsonConverterOptions(partial) {
|
|
36
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
37
|
+
const haveVars = (partial === null || partial === void 0 ? void 0 : partial.vars) !== undefined;
|
|
38
|
+
const haveRefs = (partial === null || partial === void 0 ? void 0 : partial.refs) !== undefined;
|
|
39
|
+
const extender = (partial === null || partial === void 0 ? void 0 : partial.hasOwnProperty('extendVars')) ? partial.extendVars : defaultExtendVars;
|
|
40
|
+
const haveExtender = extender !== undefined;
|
|
41
|
+
const namesDefault = (_a = partial === null || partial === void 0 ? void 0 : partial.useNameTemplates) !== null && _a !== void 0 ? _a : haveVars;
|
|
42
|
+
const conditionsDefault = (_b = partial === null || partial === void 0 ? void 0 : partial.useConditionalNames) !== null && _b !== void 0 ? _b : namesDefault;
|
|
43
|
+
const options = {
|
|
44
|
+
useValueTemplates: (_c = partial === null || partial === void 0 ? void 0 : partial.useValueTemplates) !== null && _c !== void 0 ? _c : haveVars,
|
|
45
|
+
useNameTemplates: namesDefault,
|
|
46
|
+
useConditionalNames: conditionsDefault,
|
|
47
|
+
flattenUnconditionalValues: (_d = partial === null || partial === void 0 ? void 0 : partial.flattenUnconditionalValues) !== null && _d !== void 0 ? _d : conditionsDefault,
|
|
48
|
+
useMultiValueTemplateNames: (_e = partial === null || partial === void 0 ? void 0 : partial.useMultiValueTemplateNames) !== null && _e !== void 0 ? _e : (haveExtender && namesDefault),
|
|
49
|
+
useReferences: (_f = partial === null || partial === void 0 ? void 0 : partial.useReferences) !== null && _f !== void 0 ? _f : haveRefs,
|
|
50
|
+
onInvalidPropertyName: (_g = partial === null || partial === void 0 ? void 0 : partial.onInvalidPropertyName) !== null && _g !== void 0 ? _g : 'error',
|
|
51
|
+
onInvalidPropertyValue: (_h = partial === null || partial === void 0 ? void 0 : partial.onInvalidPropertyValue) !== null && _h !== void 0 ? _h : 'error',
|
|
52
|
+
onUndefinedPropertyValue: (_j = partial === null || partial === void 0 ? void 0 : partial.onUndefinedPropertyValue) !== null && _j !== void 0 ? _j : 'ignore',
|
|
53
|
+
extendVars: extender
|
|
54
|
+
};
|
|
55
|
+
if (partial === null || partial === void 0 ? void 0 : partial.vars) {
|
|
56
|
+
options.vars = partial.vars;
|
|
57
|
+
}
|
|
58
|
+
if (partial === null || partial === void 0 ? void 0 : partial.refs) {
|
|
59
|
+
options.refs = partial.refs;
|
|
60
|
+
}
|
|
61
|
+
return options;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Creates a new {@link IJsonContext | JSON context} using values supplied in an optional partial
|
|
65
|
+
* {@link IJsonConverterOptions | converter options}.
|
|
66
|
+
* @param partial - Optional partial {@link IJsonConverterOptions | IJsonConverterOptions} used to
|
|
67
|
+
* populate the context.
|
|
68
|
+
* @public
|
|
69
|
+
*/
|
|
70
|
+
export function contextFromConverterOptions(partial) {
|
|
71
|
+
const context = {};
|
|
72
|
+
if (partial === null || partial === void 0 ? void 0 : partial.vars) {
|
|
73
|
+
context.vars = partial.vars;
|
|
74
|
+
}
|
|
75
|
+
if (partial === null || partial === void 0 ? void 0 : partial.refs) {
|
|
76
|
+
context.refs = partial.refs;
|
|
77
|
+
}
|
|
78
|
+
if (partial === null || partial === void 0 ? void 0 : partial.hasOwnProperty('extendVars')) {
|
|
79
|
+
context.extendVars = partial.extendVars;
|
|
80
|
+
}
|
|
81
|
+
return context.vars || context.refs || context.extendVars ? context : undefined;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Creates a new {@link JsonEditor | JsonEditor} from an optionally supplied partial
|
|
85
|
+
* {@link IJsonConverterOptions | JSON converter options}.
|
|
86
|
+
* Expands supplied options with default values and constructs an editor with
|
|
87
|
+
* matching configuration and defined rules.
|
|
88
|
+
* @param partial - Optional partial {@link IJsonConverterOptions | IJsonConverterOptions}
|
|
89
|
+
* used to create the editor.
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
export function converterOptionsToEditor(partial) {
|
|
93
|
+
const converterOptions = mergeDefaultJsonConverterOptions(partial);
|
|
94
|
+
const context = contextFromConverterOptions(partial);
|
|
95
|
+
const validation = {
|
|
96
|
+
onInvalidPropertyName: converterOptions.onInvalidPropertyName,
|
|
97
|
+
onInvalidPropertyValue: converterOptions.onInvalidPropertyValue,
|
|
98
|
+
onUndefinedPropertyValue: converterOptions.onUndefinedPropertyValue
|
|
99
|
+
};
|
|
100
|
+
const editorOptions = { context, validation };
|
|
101
|
+
const rules = [];
|
|
102
|
+
if (converterOptions.useNameTemplates || converterOptions.useValueTemplates) {
|
|
103
|
+
const templateOptions = Object.assign(Object.assign({}, editorOptions), { useNameTemplates: converterOptions.useNameTemplates, useValueTemplates: converterOptions.useValueTemplates });
|
|
104
|
+
rules.push(new EditorRules.TemplatedJsonEditorRule(templateOptions));
|
|
105
|
+
}
|
|
106
|
+
if (converterOptions.useConditionalNames || converterOptions.flattenUnconditionalValues) {
|
|
107
|
+
const conditionalOptions = Object.assign(Object.assign({}, editorOptions), { flattenUnconditionalValues: converterOptions.flattenUnconditionalValues });
|
|
108
|
+
rules.push(new EditorRules.ConditionalJsonEditorRule(conditionalOptions));
|
|
109
|
+
}
|
|
110
|
+
if (converterOptions.useMultiValueTemplateNames) {
|
|
111
|
+
rules.push(new EditorRules.MultiValueJsonEditorRule(editorOptions));
|
|
112
|
+
}
|
|
113
|
+
if (converterOptions.useReferences) {
|
|
114
|
+
rules.push(new EditorRules.ReferenceJsonEditorRule(editorOptions));
|
|
115
|
+
}
|
|
116
|
+
return JsonEditor.create(editorOptions, rules);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* A thin wrapper to allow an arbitrary {@link JsonEditor | JsonEditor} to be used via the
|
|
120
|
+
* \@fgv/ts-utils `Converter` pattern.
|
|
121
|
+
* @public
|
|
122
|
+
*/
|
|
123
|
+
export class JsonEditorConverter extends Conversion.BaseConverter {
|
|
124
|
+
/**
|
|
125
|
+
* Constructs a new {@link JsonEditor | JsonEditor}Converter which uses the supplied editor
|
|
126
|
+
* @param editor -
|
|
127
|
+
*/
|
|
128
|
+
constructor(editor) {
|
|
129
|
+
super((from, __self, context) => this._convert(from, context), editor.options.context);
|
|
130
|
+
this.editor = editor;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Constructs a new {@link JsonEditor | JsonEditor}Converter which uses the supplied editor
|
|
134
|
+
* @param editor -
|
|
135
|
+
*/
|
|
136
|
+
static createWithEditor(editor) {
|
|
137
|
+
return captureResult(() => new JsonEditorConverter(editor));
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Gets a derived converter which fails if the resulting converted
|
|
141
|
+
* `JsonValue` is not a `JsonObject`.
|
|
142
|
+
*/
|
|
143
|
+
object() {
|
|
144
|
+
return this.map((jv) => {
|
|
145
|
+
if (!isJsonObject(jv)) {
|
|
146
|
+
return fail(`Cannot convert "${JSON.stringify(jv)}" to JSON object.`);
|
|
147
|
+
}
|
|
148
|
+
return succeed(jv);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Gets a derived converter which fails if the resulting converted
|
|
153
|
+
* `JsonValue` is not a `JsonArray`.
|
|
154
|
+
*/
|
|
155
|
+
array() {
|
|
156
|
+
return this.map((jv) => {
|
|
157
|
+
if (!Array.isArray(jv) || typeof jv !== 'object') {
|
|
158
|
+
return fail(`Cannot convert "${JSON.stringify(jv)}" to JSON array.`);
|
|
159
|
+
}
|
|
160
|
+
return succeed(jv);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
_convert(from, context) {
|
|
164
|
+
return this.editor.clone(from, context);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* An \@fgv/ts-utils `Converter` from `unknown` to type-safe JSON, optionally
|
|
169
|
+
* rendering any string property names or values using mustache with a supplied view.
|
|
170
|
+
* @public
|
|
171
|
+
*/
|
|
172
|
+
export class JsonConverter extends JsonEditorConverter {
|
|
173
|
+
/**
|
|
174
|
+
* Constructs a new {@link JsonConverter | JsonConverter} with
|
|
175
|
+
* supplied or default options.
|
|
176
|
+
* @param options - Optional partial {@link IJsonConverterOptions | options}
|
|
177
|
+
* to configure the converter.
|
|
178
|
+
*/
|
|
179
|
+
constructor(options) {
|
|
180
|
+
const editor = converterOptionsToEditor(options).orThrow();
|
|
181
|
+
super(editor);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Creates a new {@link JsonConverter | JsonConverter}.
|
|
185
|
+
* @param options - Optional partial {@link IJsonConverterOptions | options}
|
|
186
|
+
* to configure the converter.
|
|
187
|
+
* @returns `Success` with a new {@link JsonConverter | JsonConverter}, or `Failure`
|
|
188
|
+
* with an informative message if an error occurs.
|
|
189
|
+
*/
|
|
190
|
+
static create(options) {
|
|
191
|
+
return captureResult(() => new JsonConverter(options));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* An \@fgv/ts-utils `Converter` from `unknown` to type-safe JSON
|
|
196
|
+
* with mustache template rendering and multi-value property name rules enabled
|
|
197
|
+
* regardless of initial context.
|
|
198
|
+
* @public
|
|
199
|
+
*/
|
|
200
|
+
export class TemplatedJsonConverter extends JsonEditorConverter {
|
|
201
|
+
/**
|
|
202
|
+
* Constructs a new {@link TemplatedJsonConverter | TemplatedJsonConverter} with
|
|
203
|
+
* supplied or default options.
|
|
204
|
+
* @param options - Optional partial {@link TemplatedJsonConverterOptions | options}
|
|
205
|
+
* to configure the converter.
|
|
206
|
+
*/
|
|
207
|
+
constructor(options) {
|
|
208
|
+
options = Object.assign(Object.assign({}, options), TemplatedJsonConverter.templateOptions);
|
|
209
|
+
const editor = converterOptionsToEditor(options).orThrow();
|
|
210
|
+
super(editor);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Constructs a new {@link TemplatedJsonConverter | TemplatedJsonConverter} with
|
|
214
|
+
* supplied or default options.
|
|
215
|
+
* @param options - Optional partial {@link TemplatedJsonConverterOptions | options}
|
|
216
|
+
* to configure the converter.
|
|
217
|
+
*/
|
|
218
|
+
static create(options) {
|
|
219
|
+
return captureResult(() => new TemplatedJsonConverter(options));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Default {@link IJsonConverterOptions | JSON converter options}
|
|
224
|
+
* to enable templated conversion.
|
|
225
|
+
*/
|
|
226
|
+
TemplatedJsonConverter.templateOptions = {
|
|
227
|
+
useNameTemplates: true,
|
|
228
|
+
useValueTemplates: true,
|
|
229
|
+
useMultiValueTemplateNames: true,
|
|
230
|
+
useConditionalNames: false,
|
|
231
|
+
flattenUnconditionalValues: false
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* An \@fgv/ts-utils `Converter` from `unknown` to type-safe JSON with mustache
|
|
235
|
+
* template rendering, multi-value property name and conditional property
|
|
236
|
+
* name rules enabled regardless of initial context.
|
|
237
|
+
* @public
|
|
238
|
+
*/
|
|
239
|
+
export class ConditionalJsonConverter extends JsonEditorConverter {
|
|
240
|
+
/**
|
|
241
|
+
* Constructs a new {@link ConditionalJsonConverter | ConditionalJsonConverter} with supplied or
|
|
242
|
+
* default options.
|
|
243
|
+
* @param options - Optional partial {@link ConditionalJsonConverterOptions | configuration or context}
|
|
244
|
+
* for the converter.
|
|
245
|
+
*/
|
|
246
|
+
constructor(options) {
|
|
247
|
+
options = Object.assign(Object.assign({}, options), ConditionalJsonConverter.conditionalOptions);
|
|
248
|
+
const editor = converterOptionsToEditor(options).orThrow();
|
|
249
|
+
super(editor);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Constructs a new {@link ConditionalJsonConverter | ConditionalJsonConverter} with supplied or
|
|
253
|
+
* default options.
|
|
254
|
+
* @param options - Optional partial {@link ConditionalJsonConverterOptions | configuration or context}
|
|
255
|
+
* for the converter.
|
|
256
|
+
*/
|
|
257
|
+
static create(options) {
|
|
258
|
+
return captureResult(() => new ConditionalJsonConverter(options));
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Default {@link IJsonConverterOptions | JSON converter options}
|
|
263
|
+
* to enable conditional conversion.
|
|
264
|
+
*/
|
|
265
|
+
ConditionalJsonConverter.conditionalOptions = Object.assign(Object.assign({}, TemplatedJsonConverter.templateOptions), { useConditionalNames: true, flattenUnconditionalValues: true });
|
|
266
|
+
/**
|
|
267
|
+
* A \@fgv/ts-utils `Converter` from `unknown` to type-safe JSON with mustache
|
|
268
|
+
* template rendering, multi-value property name, conditional property
|
|
269
|
+
* name, and external reference rules enabled regardless of initial context.
|
|
270
|
+
* @public
|
|
271
|
+
*/
|
|
272
|
+
export class RichJsonConverter extends JsonEditorConverter {
|
|
273
|
+
/**
|
|
274
|
+
* Constructs a new {@link RichJsonConverter | RichJsonConverter} with supplied or
|
|
275
|
+
* default options.
|
|
276
|
+
* @param options - Optional partial {@link RichJsonConverterOptions | configuration or context}
|
|
277
|
+
* for the converter.
|
|
278
|
+
*/
|
|
279
|
+
constructor(options) {
|
|
280
|
+
options = Object.assign(Object.assign({}, options), RichJsonConverter.richOptions);
|
|
281
|
+
const editor = converterOptionsToEditor(options).orThrow();
|
|
282
|
+
super(editor);
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Constructs a new {@link RichJsonConverter | RichJsonConverter} with supplied or
|
|
286
|
+
* default options
|
|
287
|
+
* @param options - Optional partial {@link RichJsonConverterOptions | configuration or context}
|
|
288
|
+
* for the converter.
|
|
289
|
+
*/
|
|
290
|
+
static create(options) {
|
|
291
|
+
return captureResult(() => new RichJsonConverter(options));
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Default {@link IJsonConverterOptions | JSON converter options}
|
|
296
|
+
* to enable rich conversion.
|
|
297
|
+
*/
|
|
298
|
+
RichJsonConverter.richOptions = Object.assign(Object.assign({}, ConditionalJsonConverter.conditionalOptions), { useReferences: true });
|
|
299
|
+
//# sourceMappingURL=jsonConverter.js.map
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
import { isJsonObject, isJsonArray, isJsonPrimitive } from '@fgv/ts-json-base';
|
|
23
|
+
import { succeed } from '@fgv/ts-utils';
|
|
24
|
+
import { deepEquals } from './utils';
|
|
25
|
+
/**
|
|
26
|
+
* Internal recursive diff function that builds the change list.
|
|
27
|
+
*/
|
|
28
|
+
function diffRecursive(obj1, obj2, path, options, changes) {
|
|
29
|
+
const pathPrefix = path ? `${path}${options.pathSeparator}` : '';
|
|
30
|
+
// Handle primitive values
|
|
31
|
+
if (isJsonPrimitive(obj1) || isJsonPrimitive(obj2)) {
|
|
32
|
+
if (!deepEquals(obj1, obj2)) {
|
|
33
|
+
changes.push({
|
|
34
|
+
path,
|
|
35
|
+
type: 'modified',
|
|
36
|
+
oldValue: obj1,
|
|
37
|
+
newValue: obj2
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
else if (options.includeUnchanged) {
|
|
41
|
+
changes.push({
|
|
42
|
+
path,
|
|
43
|
+
type: 'unchanged',
|
|
44
|
+
oldValue: obj1,
|
|
45
|
+
newValue: obj2
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
// Handle arrays
|
|
51
|
+
if (isJsonArray(obj1) && isJsonArray(obj2)) {
|
|
52
|
+
if (options.arrayOrderMatters) {
|
|
53
|
+
// Ordered array comparison
|
|
54
|
+
const maxLength = Math.max(obj1.length, obj2.length);
|
|
55
|
+
for (let i = 0; i < maxLength; i++) {
|
|
56
|
+
const itemPath = `${pathPrefix}${i}`;
|
|
57
|
+
if (i >= obj1.length) {
|
|
58
|
+
changes.push({
|
|
59
|
+
path: itemPath,
|
|
60
|
+
type: 'added',
|
|
61
|
+
newValue: obj2[i]
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
else if (i >= obj2.length) {
|
|
65
|
+
changes.push({
|
|
66
|
+
path: itemPath,
|
|
67
|
+
type: 'removed',
|
|
68
|
+
oldValue: obj1[i]
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
diffRecursive(obj1[i], obj2[i], itemPath, options, changes);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
// Unordered array comparison - simplified approach
|
|
78
|
+
// This is a basic implementation; a more sophisticated approach would use
|
|
79
|
+
// algorithms like longest common subsequence for better matching
|
|
80
|
+
const processed = new Set();
|
|
81
|
+
// Find matching elements
|
|
82
|
+
for (let i = 0; i < obj1.length; i++) {
|
|
83
|
+
let found = false;
|
|
84
|
+
for (let j = 0; j < obj2.length; j++) {
|
|
85
|
+
if (!processed.has(j) && deepEquals(obj1[i], obj2[j])) {
|
|
86
|
+
processed.add(j);
|
|
87
|
+
found = true;
|
|
88
|
+
if (options.includeUnchanged) {
|
|
89
|
+
changes.push({
|
|
90
|
+
path: `${pathPrefix}${i}`,
|
|
91
|
+
type: 'unchanged',
|
|
92
|
+
oldValue: obj1[i],
|
|
93
|
+
newValue: obj2[j]
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (!found) {
|
|
100
|
+
changes.push({
|
|
101
|
+
path: `${pathPrefix}${i}`,
|
|
102
|
+
type: 'removed',
|
|
103
|
+
oldValue: obj1[i]
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// Find added elements
|
|
108
|
+
for (let j = 0; j < obj2.length; j++) {
|
|
109
|
+
if (!processed.has(j)) {
|
|
110
|
+
changes.push({
|
|
111
|
+
path: `${pathPrefix}${j}`,
|
|
112
|
+
type: 'added',
|
|
113
|
+
newValue: obj2[j]
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
// Handle one array, one non-array
|
|
121
|
+
if (isJsonArray(obj1) || isJsonArray(obj2)) {
|
|
122
|
+
changes.push({
|
|
123
|
+
path,
|
|
124
|
+
type: 'modified',
|
|
125
|
+
oldValue: obj1,
|
|
126
|
+
newValue: obj2
|
|
127
|
+
});
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
// Handle objects
|
|
131
|
+
if (isJsonObject(obj1) && isJsonObject(obj2)) {
|
|
132
|
+
const keys1 = new Set(Object.keys(obj1));
|
|
133
|
+
const keys2 = new Set(Object.keys(obj2));
|
|
134
|
+
const allKeys = new Set([...keys1, ...keys2]);
|
|
135
|
+
for (const key of allKeys) {
|
|
136
|
+
const keyPath = path ? `${path}${options.pathSeparator}${key}` : key;
|
|
137
|
+
if (!keys1.has(key)) {
|
|
138
|
+
changes.push({
|
|
139
|
+
path: keyPath,
|
|
140
|
+
type: 'added',
|
|
141
|
+
newValue: obj2[key]
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
else if (!keys2.has(key)) {
|
|
145
|
+
changes.push({
|
|
146
|
+
path: keyPath,
|
|
147
|
+
type: 'removed',
|
|
148
|
+
oldValue: obj1[key]
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
diffRecursive(obj1[key], obj2[key], keyPath, options, changes);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
/* c8 ignore next 9 - defensive code path that should never be reached with valid JsonValue types */
|
|
158
|
+
// Handle mixed object types
|
|
159
|
+
changes.push({
|
|
160
|
+
path,
|
|
161
|
+
type: 'modified',
|
|
162
|
+
oldValue: obj1,
|
|
163
|
+
newValue: obj2
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Performs a deep diff comparison between two JSON values.
|
|
168
|
+
*
|
|
169
|
+
* This function provides detailed change tracking by analyzing every difference
|
|
170
|
+
* between two JSON structures. It returns a list of specific changes with paths,
|
|
171
|
+
* making it ideal for debugging, logging, change analysis, and understanding
|
|
172
|
+
* exactly what has changed between two data states.
|
|
173
|
+
*
|
|
174
|
+
* **Key Features:**
|
|
175
|
+
* - Deep recursive comparison of nested objects and arrays
|
|
176
|
+
* - Precise path tracking using dot notation (e.g., "user.profile.name")
|
|
177
|
+
* - Support for all JSON value types: objects, arrays, primitives, null
|
|
178
|
+
* - Configurable array comparison (ordered vs unordered)
|
|
179
|
+
* - Optional inclusion of unchanged values for complete comparisons
|
|
180
|
+
*
|
|
181
|
+
* **Use Cases:**
|
|
182
|
+
* - Debugging data changes in applications
|
|
183
|
+
* - Generating change logs or audit trails
|
|
184
|
+
* - Validating API responses against expected data
|
|
185
|
+
* - Creating detailed diff reports for data synchronization
|
|
186
|
+
*
|
|
187
|
+
* @param obj1 - The first JSON value to compare (often the "before" state)
|
|
188
|
+
* @param obj2 - The second JSON value to compare (often the "after" state)
|
|
189
|
+
* @param options - Optional configuration for customizing diff behavior
|
|
190
|
+
* @returns A Result containing the diff result with all detected changes
|
|
191
|
+
*
|
|
192
|
+
* @example Basic usage with objects
|
|
193
|
+
* ```typescript
|
|
194
|
+
* const before = { name: "John", age: 30, city: "NYC" };
|
|
195
|
+
* const after = { name: "Jane", age: 30, country: "USA" };
|
|
196
|
+
*
|
|
197
|
+
* const result = jsonDiff(before, after);
|
|
198
|
+
* if (result.success) {
|
|
199
|
+
* result.value.changes.forEach(change => {
|
|
200
|
+
* console.log(`${change.path}: ${change.type}`);
|
|
201
|
+
* // Output:
|
|
202
|
+
* // name: modified
|
|
203
|
+
* // city: removed
|
|
204
|
+
* // country: added
|
|
205
|
+
* });
|
|
206
|
+
* }
|
|
207
|
+
* ```
|
|
208
|
+
*
|
|
209
|
+
* @example With arrays and nested structures
|
|
210
|
+
* ```typescript
|
|
211
|
+
* const user1 = {
|
|
212
|
+
* profile: { name: "John", hobbies: ["reading"] },
|
|
213
|
+
* settings: { theme: "dark" }
|
|
214
|
+
* };
|
|
215
|
+
* const user2 = {
|
|
216
|
+
* profile: { name: "John", hobbies: ["reading", "gaming"] },
|
|
217
|
+
* settings: { theme: "light", notifications: true }
|
|
218
|
+
* };
|
|
219
|
+
*
|
|
220
|
+
* const result = jsonDiff(user1, user2);
|
|
221
|
+
* if (result.success) {
|
|
222
|
+
* console.log(result.value.changes);
|
|
223
|
+
* // [
|
|
224
|
+
* // { path: "profile.hobbies.1", type: "added", newValue: "gaming" },
|
|
225
|
+
* // { path: "settings.theme", type: "modified", oldValue: "dark", newValue: "light" },
|
|
226
|
+
* // { path: "settings.notifications", type: "added", newValue: true }
|
|
227
|
+
* // ]
|
|
228
|
+
* }
|
|
229
|
+
* ```
|
|
230
|
+
*
|
|
231
|
+
* @example Using options for custom behavior
|
|
232
|
+
* ```typescript
|
|
233
|
+
* const options: IJsonDiffOptions = {
|
|
234
|
+
* includeUnchanged: true, // Include unchanged properties
|
|
235
|
+
* pathSeparator: '/', // Use '/' instead of '.' in paths
|
|
236
|
+
* arrayOrderMatters: false // Treat arrays as unordered sets
|
|
237
|
+
* };
|
|
238
|
+
*
|
|
239
|
+
* const result = jsonDiff(obj1, obj2, options);
|
|
240
|
+
* ```
|
|
241
|
+
*
|
|
242
|
+
* @see {@link IDiffResult} for the structure of returned results
|
|
243
|
+
* @see {@link IDiffChange} for details about individual changes
|
|
244
|
+
* @see {@link IJsonDiffOptions} for available configuration options
|
|
245
|
+
* @see {@link jsonThreeWayDiff} for an alternative API focused on actionable results
|
|
246
|
+
* @see {@link jsonEquals} for simple equality checking without change details
|
|
247
|
+
*
|
|
248
|
+
* @public
|
|
249
|
+
*/
|
|
250
|
+
export function jsonDiff(obj1, obj2, options = {}) {
|
|
251
|
+
const opts = Object.assign({ includeUnchanged: false, pathSeparator: '.', arrayOrderMatters: true }, options);
|
|
252
|
+
const changes = [];
|
|
253
|
+
diffRecursive(obj1, obj2, '', opts, changes);
|
|
254
|
+
const result = {
|
|
255
|
+
changes,
|
|
256
|
+
identical: changes.length === 0 || changes.every((c) => c.type === 'unchanged')
|
|
257
|
+
};
|
|
258
|
+
return succeed(result);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* A simpler helper function that returns true if two JSON values are deeply equal.
|
|
262
|
+
*
|
|
263
|
+
* This function provides a fast boolean check for JSON equality without the overhead
|
|
264
|
+
* of tracking individual changes. It performs the same deep comparison logic as
|
|
265
|
+
* {@link Diff.jsonDiff} but returns only a true/false result, making it ideal for
|
|
266
|
+
* conditional logic and validation scenarios.
|
|
267
|
+
*
|
|
268
|
+
* **Key Features:**
|
|
269
|
+
* - Deep recursive comparison of all nested structures
|
|
270
|
+
* - Handles all JSON types: objects, arrays, primitives, null
|
|
271
|
+
* - Object property order independence
|
|
272
|
+
* - Array order significance (index positions matter)
|
|
273
|
+
* - Performance optimized for equality checking
|
|
274
|
+
*
|
|
275
|
+
* **Use Cases:**
|
|
276
|
+
* - Conditional logic based on data equality
|
|
277
|
+
* - Input validation and testing assertions
|
|
278
|
+
* - Caching and memoization keys
|
|
279
|
+
* - Quick checks before expensive diff operations
|
|
280
|
+
*
|
|
281
|
+
* @param obj1 - The first JSON value to compare
|
|
282
|
+
* @param obj2 - The second JSON value to compare
|
|
283
|
+
* @returns True if the values are deeply equal, false otherwise
|
|
284
|
+
*
|
|
285
|
+
* @example Basic equality checking
|
|
286
|
+
* ```typescript
|
|
287
|
+
* // Objects with same structure and values
|
|
288
|
+
* const user1 = { name: "John", hobbies: ["reading", "gaming"] };
|
|
289
|
+
* const user2 = { name: "John", hobbies: ["reading", "gaming"] };
|
|
290
|
+
* console.log(jsonEquals(user1, user2)); // true
|
|
291
|
+
*
|
|
292
|
+
* // Different property order (still equal)
|
|
293
|
+
* const obj1 = { a: 1, b: 2 };
|
|
294
|
+
* const obj2 = { b: 2, a: 1 };
|
|
295
|
+
* console.log(jsonEquals(obj1, obj2)); // true
|
|
296
|
+
*
|
|
297
|
+
* // Different values
|
|
298
|
+
* const before = { status: "pending" };
|
|
299
|
+
* const after = { status: "completed" };
|
|
300
|
+
* console.log(jsonEquals(before, after)); // false
|
|
301
|
+
* ```
|
|
302
|
+
*
|
|
303
|
+
* @example With nested structures
|
|
304
|
+
* ```typescript
|
|
305
|
+
* const config1 = {
|
|
306
|
+
* database: { host: "localhost", port: 5432 },
|
|
307
|
+
* features: ["auth", "cache"]
|
|
308
|
+
* };
|
|
309
|
+
* const config2 = {
|
|
310
|
+
* database: { host: "localhost", port: 5432 },
|
|
311
|
+
* features: ["auth", "cache"]
|
|
312
|
+
* };
|
|
313
|
+
*
|
|
314
|
+
* if (jsonEquals(config1, config2)) {
|
|
315
|
+
* console.log("Configurations are identical");
|
|
316
|
+
* }
|
|
317
|
+
* ```
|
|
318
|
+
*
|
|
319
|
+
* @example Array order sensitivity
|
|
320
|
+
* ```typescript
|
|
321
|
+
* const list1 = [1, 2, 3];
|
|
322
|
+
* const list2 = [3, 2, 1];
|
|
323
|
+
* console.log(jsonEquals(list1, list2)); // false - order matters
|
|
324
|
+
*
|
|
325
|
+
* const list3 = [1, 2, 3];
|
|
326
|
+
* const list4 = [1, 2, 3];
|
|
327
|
+
* console.log(jsonEquals(list3, list4)); // true - same order
|
|
328
|
+
* ```
|
|
329
|
+
*
|
|
330
|
+
* @see {@link Diff.jsonDiff} for detailed change analysis when equality fails
|
|
331
|
+
* @see {@link jsonThreeWayDiff} for actionable difference results
|
|
332
|
+
*
|
|
333
|
+
* @public
|
|
334
|
+
*/
|
|
335
|
+
export function jsonEquals(obj1, obj2) {
|
|
336
|
+
return deepEquals(obj1, obj2);
|
|
337
|
+
}
|
|
338
|
+
//# sourceMappingURL=detailedDiff.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
export * from './detailedDiff';
|
|
23
|
+
export * from './threeWayDiff';
|
|
24
|
+
//# sourceMappingURL=index.js.map
|