@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,258 @@
|
|
|
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 { succeed } from '@fgv/ts-utils';
|
|
23
|
+
import { isJsonObject } from '@fgv/ts-json-base';
|
|
24
|
+
import { deepEquals } from './utils';
|
|
25
|
+
/**
|
|
26
|
+
* Internal function to build three-way diff for objects.
|
|
27
|
+
*/
|
|
28
|
+
function buildThreeWayDiffForObjects(obj1, obj2, metadata) {
|
|
29
|
+
if (!isJsonObject(obj1) || !isJsonObject(obj2)) {
|
|
30
|
+
// Handle non-object cases
|
|
31
|
+
if (deepEquals(obj1, obj2)) {
|
|
32
|
+
metadata.unchanged++;
|
|
33
|
+
return {
|
|
34
|
+
onlyInA: null,
|
|
35
|
+
unchanged: obj1,
|
|
36
|
+
onlyInB: null
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
metadata.modified++;
|
|
41
|
+
return {
|
|
42
|
+
onlyInA: obj1,
|
|
43
|
+
unchanged: null,
|
|
44
|
+
onlyInB: obj2
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const keys1 = new Set(Object.keys(obj1));
|
|
49
|
+
const keys2 = new Set(Object.keys(obj2));
|
|
50
|
+
const allKeys = new Set([...keys1, ...keys2]);
|
|
51
|
+
const onlyInA = {};
|
|
52
|
+
const unchanged = {};
|
|
53
|
+
const onlyInB = {};
|
|
54
|
+
let hasOnlyInA = false;
|
|
55
|
+
let hasUnchanged = false;
|
|
56
|
+
let hasOnlyInB = false;
|
|
57
|
+
for (const key of allKeys) {
|
|
58
|
+
if (!keys1.has(key)) {
|
|
59
|
+
// Property only exists in obj2
|
|
60
|
+
metadata.added++;
|
|
61
|
+
onlyInB[key] = obj2[key];
|
|
62
|
+
hasOnlyInB = true;
|
|
63
|
+
}
|
|
64
|
+
else if (!keys2.has(key)) {
|
|
65
|
+
// Property only exists in obj1
|
|
66
|
+
metadata.removed++;
|
|
67
|
+
onlyInA[key] = obj1[key];
|
|
68
|
+
hasOnlyInA = true;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
// Property exists in both
|
|
72
|
+
const val1 = obj1[key];
|
|
73
|
+
const val2 = obj2[key];
|
|
74
|
+
if (deepEquals(val1, val2)) {
|
|
75
|
+
// Values are identical
|
|
76
|
+
if (isJsonObject(val1) && isJsonObject(val2)) {
|
|
77
|
+
// For objects, we need to recurse to get proper metadata counts
|
|
78
|
+
const childDiff = buildThreeWayDiffForObjects(val1, val2, metadata);
|
|
79
|
+
if (childDiff.unchanged !== null) {
|
|
80
|
+
unchanged[key] = childDiff.unchanged;
|
|
81
|
+
hasUnchanged = true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
metadata.unchanged++;
|
|
86
|
+
unchanged[key] = val1;
|
|
87
|
+
hasUnchanged = true;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
// Values are different
|
|
92
|
+
if (isJsonObject(val1) && isJsonObject(val2)) {
|
|
93
|
+
// For nested objects, recurse
|
|
94
|
+
const childDiff = buildThreeWayDiffForObjects(val1, val2, metadata);
|
|
95
|
+
if (childDiff.onlyInA !== null) {
|
|
96
|
+
onlyInA[key] = childDiff.onlyInA;
|
|
97
|
+
hasOnlyInA = true;
|
|
98
|
+
}
|
|
99
|
+
if (childDiff.unchanged !== null) {
|
|
100
|
+
unchanged[key] = childDiff.unchanged;
|
|
101
|
+
hasUnchanged = true;
|
|
102
|
+
}
|
|
103
|
+
if (childDiff.onlyInB !== null) {
|
|
104
|
+
onlyInB[key] = childDiff.onlyInB;
|
|
105
|
+
hasOnlyInB = true;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
// For primitives or arrays, treat as complete replacement
|
|
110
|
+
metadata.modified++;
|
|
111
|
+
onlyInA[key] = val1;
|
|
112
|
+
onlyInB[key] = val2;
|
|
113
|
+
hasOnlyInA = true;
|
|
114
|
+
hasOnlyInB = true;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
onlyInA: hasOnlyInA ? onlyInA : null,
|
|
121
|
+
unchanged: hasUnchanged ? unchanged : null,
|
|
122
|
+
onlyInB: hasOnlyInB ? onlyInB : null
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Performs a three-way diff comparison between two JSON values, returning separate
|
|
127
|
+
* objects containing the differences and similarities.
|
|
128
|
+
*
|
|
129
|
+
* This function provides an alternative to {@link Diff.jsonDiff} that focuses on actionable
|
|
130
|
+
* results rather than detailed change analysis. Instead of a list of individual changes,
|
|
131
|
+
* it returns three objects that can be directly used for merging, UI display, or
|
|
132
|
+
* programmatic manipulation.
|
|
133
|
+
*
|
|
134
|
+
* **Key Features:**
|
|
135
|
+
* - **Actionable Results**: Returns objects ready for immediate use in merging operations
|
|
136
|
+
* - **Simplified Array Handling**: Arrays are treated as atomic values for cleaner results
|
|
137
|
+
* - **Structural Preservation**: Maintains original JSON structure rather than flattened paths
|
|
138
|
+
* - **UI-Optimized**: Perfect for side-by-side diff displays and change visualization
|
|
139
|
+
* - **Merge-Friendly**: Designed specifically for three-way merge scenarios
|
|
140
|
+
*
|
|
141
|
+
* **Array Handling:**
|
|
142
|
+
* Unlike {@link Diff.jsonDiff}, this function treats arrays as complete units. If arrays differ,
|
|
143
|
+
* the entire array appears in the appropriate result object rather than computing
|
|
144
|
+
* element-by-element deltas. This approach is simpler and more predictable for most
|
|
145
|
+
* use cases involving data updates and synchronization.
|
|
146
|
+
*
|
|
147
|
+
* **Use Cases:**
|
|
148
|
+
* - Applying configuration updates while preserving unchanged settings
|
|
149
|
+
* - Creating side-by-side diff displays in user interfaces
|
|
150
|
+
* - Building three-way merge tools for data synchronization
|
|
151
|
+
* - Implementing undo/redo functionality with granular control
|
|
152
|
+
* - Generating patch objects for API updates
|
|
153
|
+
*
|
|
154
|
+
* @param obj1 - The first JSON value to compare (often the "before" or "source" state)
|
|
155
|
+
* @param obj2 - The second JSON value to compare (often the "after" or "target" state)
|
|
156
|
+
* @returns A Result containing the three-way diff with separate objects and metadata
|
|
157
|
+
*
|
|
158
|
+
* @example Basic usage for applying changes
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const original = { name: "John", age: 30, city: "NYC", active: true };
|
|
161
|
+
* const updated = { name: "Jane", age: 30, country: "USA", active: true };
|
|
162
|
+
*
|
|
163
|
+
* const result = jsonThreeWayDiff(original, updated);
|
|
164
|
+
* if (result.success) {
|
|
165
|
+
* const { onlyInA, unchanged, onlyInB } = result.value;
|
|
166
|
+
*
|
|
167
|
+
* // Apply changes: merge unchanged + onlyInB
|
|
168
|
+
* const applied = { ...unchanged, ...onlyInB };
|
|
169
|
+
* console.log(applied); // { age: 30, active: true, name: "Jane", country: "USA" }
|
|
170
|
+
*
|
|
171
|
+
* // Revert changes: merge unchanged + onlyInA
|
|
172
|
+
* const reverted = { ...unchanged, ...onlyInA };
|
|
173
|
+
* console.log(reverted); // { age: 30, active: true, name: "John", city: "NYC" }
|
|
174
|
+
* }
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* @example UI-friendly diff display
|
|
178
|
+
* ```typescript
|
|
179
|
+
* const result = jsonThreeWayDiff(userBefore, userAfter);
|
|
180
|
+
* if (result.success) {
|
|
181
|
+
* const { onlyInA, unchanged, onlyInB, metadata } = result.value;
|
|
182
|
+
*
|
|
183
|
+
* // Display summary
|
|
184
|
+
* console.log(`Changes: ${metadata.added} added, ${metadata.removed} removed, ${metadata.modified} modified`);
|
|
185
|
+
*
|
|
186
|
+
* // Show removed/old values in red
|
|
187
|
+
* if (onlyInA) displayInColor(onlyInA, 'red');
|
|
188
|
+
*
|
|
189
|
+
* // Show unchanged values in gray
|
|
190
|
+
* if (unchanged) displayInColor(unchanged, 'gray');
|
|
191
|
+
*
|
|
192
|
+
* // Show added/new values in green
|
|
193
|
+
* if (onlyInB) displayInColor(onlyInB, 'green');
|
|
194
|
+
* }
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @example Nested objects and array handling
|
|
198
|
+
* ```typescript
|
|
199
|
+
* const config1 = {
|
|
200
|
+
* database: { host: "localhost", port: 5432 },
|
|
201
|
+
* features: ["auth", "logging"],
|
|
202
|
+
* version: "1.0"
|
|
203
|
+
* };
|
|
204
|
+
* const config2 = {
|
|
205
|
+
* database: { host: "production.db", port: 5432 },
|
|
206
|
+
* features: ["auth", "logging", "metrics"], // Array treated as complete unit
|
|
207
|
+
* version: "1.1"
|
|
208
|
+
* };
|
|
209
|
+
*
|
|
210
|
+
* const result = jsonThreeWayDiff(config1, config2);
|
|
211
|
+
* if (result.success) {
|
|
212
|
+
* // result.value.onlyInA = { database: { host: "localhost" }, features: ["auth", "logging"], version: "1.0" }
|
|
213
|
+
* // result.value.unchanged = { database: { port: 5432 } }
|
|
214
|
+
* // result.value.onlyInB = { database: { host: "production.db" }, features: ["auth", "logging", "metrics"], version: "1.1" }
|
|
215
|
+
* }
|
|
216
|
+
* ```
|
|
217
|
+
*
|
|
218
|
+
* @example Conditional updates based on changes
|
|
219
|
+
* ```typescript
|
|
220
|
+
* const result = jsonThreeWayDiff(currentState, newState);
|
|
221
|
+
* if (result.success && !result.value.identical) {
|
|
222
|
+
* const { metadata } = result.value;
|
|
223
|
+
*
|
|
224
|
+
* if (metadata.modified > 0) {
|
|
225
|
+
* console.log("Critical settings changed - requires restart");
|
|
226
|
+
* } else if (metadata.added > 0) {
|
|
227
|
+
* console.log("New features enabled");
|
|
228
|
+
* } else if (metadata.removed > 0) {
|
|
229
|
+
* console.log("Features disabled");
|
|
230
|
+
* }
|
|
231
|
+
* }
|
|
232
|
+
* ```
|
|
233
|
+
*
|
|
234
|
+
* @see {@link IThreeWayDiff} for the structure of returned results
|
|
235
|
+
* @see {@link IThreeWayDiffMetadata} for metadata details
|
|
236
|
+
* @see {@link Diff.jsonDiff} for detailed change-by-change analysis
|
|
237
|
+
* @see {@link jsonEquals} for simple equality checking
|
|
238
|
+
*
|
|
239
|
+
* @public
|
|
240
|
+
*/
|
|
241
|
+
export function jsonThreeWayDiff(obj1, obj2) {
|
|
242
|
+
const metadata = {
|
|
243
|
+
removed: 0,
|
|
244
|
+
added: 0,
|
|
245
|
+
modified: 0,
|
|
246
|
+
unchanged: 0
|
|
247
|
+
};
|
|
248
|
+
const diff = buildThreeWayDiffForObjects(obj1, obj2, metadata);
|
|
249
|
+
const result = {
|
|
250
|
+
onlyInA: diff.onlyInA,
|
|
251
|
+
unchanged: diff.unchanged,
|
|
252
|
+
onlyInB: diff.onlyInB,
|
|
253
|
+
metadata,
|
|
254
|
+
identical: metadata.removed === 0 && metadata.added === 0 && metadata.modified === 0
|
|
255
|
+
};
|
|
256
|
+
return succeed(result);
|
|
257
|
+
}
|
|
258
|
+
//# sourceMappingURL=threeWayDiff.js.map
|
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
/**
|
|
24
|
+
* Deep comparison function for JSON values that handles all JSON types.
|
|
25
|
+
*/
|
|
26
|
+
export function deepEquals(a, b) {
|
|
27
|
+
if (a === b) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
if (isJsonPrimitive(a) || isJsonPrimitive(b)) {
|
|
31
|
+
return a === b;
|
|
32
|
+
}
|
|
33
|
+
if (isJsonArray(a) && isJsonArray(b)) {
|
|
34
|
+
if (a.length !== b.length) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
for (let i = 0; i < a.length; i++) {
|
|
38
|
+
if (!deepEquals(a[i], b[i])) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
if (isJsonObject(a) && isJsonObject(b)) {
|
|
45
|
+
const keysA = Object.keys(a);
|
|
46
|
+
const keysB = Object.keys(b);
|
|
47
|
+
if (keysA.length !== keysB.length) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
for (const key of keysA) {
|
|
51
|
+
if (!(key in b) || !deepEquals(a[key], b[key])) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023 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 * as EditorRules from './rules';
|
|
23
|
+
export * from './common';
|
|
24
|
+
export { JsonEditor } from './jsonEditor';
|
|
25
|
+
export { JsonEditorRuleBase } from './jsonEditorRule';
|
|
26
|
+
export { JsonEditorState } from './jsonEditorState';
|
|
27
|
+
export { PrefixedJsonMap, ReferenceMapKeyPolicy, SimpleJsonMap } from './jsonReferenceMap';
|
|
28
|
+
export { EditorRules };
|
|
29
|
+
//# sourceMappingURL=index.js.map
|