@fgv/ts-json 5.0.0-2 → 5.0.0-21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/.vscode/launch.json +16 -0
  2. package/.vscode/settings.json +32 -0
  3. package/config/api-extractor.json +343 -0
  4. package/config/rig.json +16 -0
  5. package/dist/ts-json.d.ts +719 -28
  6. package/dist/tsdoc-metadata.json +1 -1
  7. package/lib/index.d.ts +2 -0
  8. package/lib/index.js +25 -0
  9. package/lib/packlets/diff/detailedDiff.d.ts +375 -0
  10. package/lib/packlets/diff/detailedDiff.js +342 -0
  11. package/lib/packlets/diff/index.d.ts +3 -0
  12. package/lib/packlets/diff/index.js +40 -0
  13. package/lib/packlets/diff/threeWayDiff.d.ts +263 -0
  14. package/lib/packlets/diff/threeWayDiff.js +261 -0
  15. package/lib/packlets/diff/utils.d.ts +6 -0
  16. package/lib/packlets/diff/utils.js +62 -0
  17. package/lib/packlets/editor/common.d.ts +6 -0
  18. package/lib/packlets/editor/jsonEditor.d.ts +57 -28
  19. package/lib/packlets/editor/jsonEditor.js +101 -32
  20. package/lib/test/legacy/jsonConverter.conditional.test.d.ts +2 -0
  21. package/lib/test/legacy/jsonEditor/rules/conditional.test.d.ts +2 -0
  22. package/lib/test/legacy/jsonEditor/rules/multivalue.test.d.ts +2 -0
  23. package/lib/test/legacy/jsonEditor/rules/references.test.d.ts +2 -0
  24. package/lib/test/legacy/jsonEditor/rules/templates.test.d.ts +2 -0
  25. package/lib/test/unit/contextHelpers.test.d.ts +2 -0
  26. package/lib/test/unit/converters.test.d.ts +2 -0
  27. package/lib/test/unit/diff/jsonDiff.test.d.ts +2 -0
  28. package/lib/test/unit/jsonConverter.test.d.ts +2 -0
  29. package/lib/test/unit/jsonEditor/jsonEditor.test.d.ts +2 -0
  30. package/lib/test/unit/jsonEditor/templateContext.test.d.ts +2 -0
  31. package/lib/test/unit/jsonReferenceMap.test.d.ts +2 -0
  32. package/package.json +11 -11
  33. package/src/index.ts +29 -0
  34. package/src/packlets/context/compositeJsonMap.ts +120 -0
  35. package/src/packlets/context/contextHelpers.ts +221 -0
  36. package/src/packlets/context/index.ts +33 -0
  37. package/src/packlets/context/jsonContext.ts +133 -0
  38. package/src/packlets/converters/converters.ts +117 -0
  39. package/src/packlets/converters/index.ts +26 -0
  40. package/src/packlets/converters/jsonConverter.ts +476 -0
  41. package/src/packlets/diff/detailedDiff.ts +585 -0
  42. package/src/packlets/diff/index.ts +24 -0
  43. package/src/packlets/diff/threeWayDiff.ts +420 -0
  44. package/src/packlets/diff/utils.ts +66 -0
  45. package/src/packlets/editor/common.ts +125 -0
  46. package/src/packlets/editor/index.ts +36 -0
  47. package/src/packlets/editor/jsonEditor.ts +523 -0
  48. package/src/packlets/editor/jsonEditorRule.ts +117 -0
  49. package/src/packlets/editor/jsonEditorState.ts +225 -0
  50. package/src/packlets/editor/jsonReferenceMap.ts +516 -0
  51. package/src/packlets/editor/rules/conditional.ts +222 -0
  52. package/src/packlets/editor/rules/index.ts +25 -0
  53. package/src/packlets/editor/rules/multivalue.ts +206 -0
  54. package/src/packlets/editor/rules/references.ts +177 -0
  55. package/src/packlets/editor/rules/templates.ts +159 -0
  56. package/CHANGELOG.md +0 -115
  57. package/lib/index.d.ts.map +0 -1
  58. package/lib/index.js.map +0 -1
  59. package/lib/packlets/context/compositeJsonMap.d.ts.map +0 -1
  60. package/lib/packlets/context/compositeJsonMap.js.map +0 -1
  61. package/lib/packlets/context/contextHelpers.d.ts.map +0 -1
  62. package/lib/packlets/context/contextHelpers.js.map +0 -1
  63. package/lib/packlets/context/index.d.ts.map +0 -1
  64. package/lib/packlets/context/index.js.map +0 -1
  65. package/lib/packlets/context/jsonContext.d.ts.map +0 -1
  66. package/lib/packlets/context/jsonContext.js.map +0 -1
  67. package/lib/packlets/converters/converters.d.ts.map +0 -1
  68. package/lib/packlets/converters/converters.js.map +0 -1
  69. package/lib/packlets/converters/index.d.ts.map +0 -1
  70. package/lib/packlets/converters/index.js.map +0 -1
  71. package/lib/packlets/converters/jsonConverter.d.ts.map +0 -1
  72. package/lib/packlets/converters/jsonConverter.js.map +0 -1
  73. package/lib/packlets/editor/common.d.ts.map +0 -1
  74. package/lib/packlets/editor/common.js.map +0 -1
  75. package/lib/packlets/editor/index.d.ts.map +0 -1
  76. package/lib/packlets/editor/index.js.map +0 -1
  77. package/lib/packlets/editor/jsonEditor.d.ts.map +0 -1
  78. package/lib/packlets/editor/jsonEditor.js.map +0 -1
  79. package/lib/packlets/editor/jsonEditorRule.d.ts.map +0 -1
  80. package/lib/packlets/editor/jsonEditorRule.js.map +0 -1
  81. package/lib/packlets/editor/jsonEditorState.d.ts.map +0 -1
  82. package/lib/packlets/editor/jsonEditorState.js.map +0 -1
  83. package/lib/packlets/editor/jsonReferenceMap.d.ts.map +0 -1
  84. package/lib/packlets/editor/jsonReferenceMap.js.map +0 -1
  85. package/lib/packlets/editor/rules/conditional.d.ts.map +0 -1
  86. package/lib/packlets/editor/rules/conditional.js.map +0 -1
  87. package/lib/packlets/editor/rules/index.d.ts.map +0 -1
  88. package/lib/packlets/editor/rules/index.js.map +0 -1
  89. package/lib/packlets/editor/rules/multivalue.d.ts.map +0 -1
  90. package/lib/packlets/editor/rules/multivalue.js.map +0 -1
  91. package/lib/packlets/editor/rules/references.d.ts.map +0 -1
  92. package/lib/packlets/editor/rules/references.js.map +0 -1
  93. package/lib/packlets/editor/rules/templates.d.ts.map +0 -1
  94. package/lib/packlets/editor/rules/templates.js.map +0 -1
@@ -0,0 +1,420 @@
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
+
23
+ import { Result, succeed } from '@fgv/ts-utils';
24
+ import { JsonValue, isJsonObject } from '@fgv/ts-json-base';
25
+ import { deepEquals } from './utils';
26
+
27
+ /**
28
+ * Metadata about the differences found in a three-way diff.
29
+ *
30
+ * Provides summary statistics about the types and quantities of changes
31
+ * detected between two JSON values, making it easy to understand the
32
+ * overall scope of differences at a glance.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const metadata: IThreeWayDiffMetadata = {
37
+ * removed: 2, // 2 properties only in first object
38
+ * added: 1, // 1 property only in second object
39
+ * modified: 3, // 3 properties changed between objects
40
+ * unchanged: 5 // 5 properties identical in both objects
41
+ * };
42
+ *
43
+ * console.log(`Total changes: ${metadata.added + metadata.removed + metadata.modified}`);
44
+ * console.log(`Stability: ${metadata.unchanged / (metadata.unchanged + metadata.modified) * 100}%`);
45
+ * ```
46
+ *
47
+ * @public
48
+ */
49
+ export interface IThreeWayDiffMetadata {
50
+ /**
51
+ * Number of properties that exist only in the first object.
52
+ *
53
+ * These represent data that was removed when transitioning from
54
+ * the first object to the second object.
55
+ */
56
+ removed: number;
57
+
58
+ /**
59
+ * Number of properties that exist only in the second object.
60
+ *
61
+ * These represent new data that was added when transitioning from
62
+ * the first object to the second object.
63
+ */
64
+ added: number;
65
+
66
+ /**
67
+ * Number of properties that exist in both objects but have different values.
68
+ *
69
+ * These represent data that was modified during the transition between objects.
70
+ * For arrays, this counts entire array replacements as single modifications.
71
+ */
72
+ modified: number;
73
+
74
+ /**
75
+ * Number of properties that exist in both objects with identical values.
76
+ *
77
+ * These represent stable data that remained consistent between the two objects.
78
+ */
79
+ unchanged: number;
80
+ }
81
+
82
+ /**
83
+ * Result of a three-way JSON diff operation.
84
+ *
85
+ * This interface provides an actionable representation of differences between
86
+ * two JSON values by separating them into three distinct objects. This approach
87
+ * makes it easy to apply changes, display side-by-side comparisons, perform
88
+ * merges, or programmatically work with the differences.
89
+ *
90
+ * **Key Benefits:**
91
+ * - **Actionable Results**: Objects can be directly used for merging or applying changes
92
+ * - **UI-Friendly**: Perfect for side-by-side diff displays with clear visual separation
93
+ * - **Merge-Ready**: Simplified three-way merge operations
94
+ * - **Structured Data**: Maintains original JSON structure rather than flattened paths
95
+ *
96
+ * @example Basic usage
97
+ * ```typescript
98
+ * const result: IThreeWayDiff = {
99
+ * onlyInA: { name: "John", city: "NYC" }, // Original or removed data
100
+ * unchanged: { age: 30 }, // Stable data
101
+ * onlyInB: { name: "Jane", country: "USA" }, // New or modified data
102
+ * metadata: { added: 1, removed: 1, modified: 1, unchanged: 1 },
103
+ * identical: false
104
+ * };
105
+ *
106
+ * // Apply changes: merge unchanged + onlyInB
107
+ * const updated = { ...result.unchanged, ...result.onlyInB };
108
+ * // Result: { age: 30, name: "Jane", country: "USA" }
109
+ *
110
+ * // Revert changes: merge unchanged + onlyInA
111
+ * const reverted = { ...result.unchanged, ...result.onlyInA };
112
+ * // Result: { age: 30, name: "John", city: "NYC" }
113
+ * ```
114
+ *
115
+ * @see {@link IThreeWayDiffMetadata} for metadata structure details
116
+ * @see {@link jsonThreeWayDiff} for the function that produces this result
117
+ * @see {@link Diff.jsonDiff} for an alternative detailed change-list approach
118
+ *
119
+ * @public
120
+ */
121
+ export interface IThreeWayDiff {
122
+ /**
123
+ * Contains properties that exist only in the first object, plus the first object's
124
+ * version of any properties that exist in both but have different values.
125
+ *
126
+ * This object represents the "old" or "source" state and can be used for:
127
+ * - Reverting changes by merging with `unchanged`
128
+ * - Displaying what was removed or changed from the original
129
+ * - Understanding the baseline state before modifications
130
+ *
131
+ * Will be `null` if no properties are unique to the first object.
132
+ */
133
+ onlyInA: JsonValue;
134
+
135
+ /**
136
+ * Contains properties that exist in both objects with identical values.
137
+ *
138
+ * This object represents the stable, consistent data between both inputs
139
+ * and can be used for:
140
+ * - The foundation for merging operations
141
+ * - Identifying what remained constant during changes
142
+ * - Building complete objects by combining with other parts
143
+ *
144
+ * Will be `null` if no properties are shared between the objects.
145
+ */
146
+ unchanged: JsonValue;
147
+
148
+ /**
149
+ * Contains properties that exist only in the second object, plus the second object's
150
+ * version of any properties that exist in both but have different values.
151
+ *
152
+ * This object represents the "new" or "target" state and can be used for:
153
+ * - Applying changes by merging with `unchanged`
154
+ * - Displaying what was added or changed in the update
155
+ * - Understanding the desired end state after modifications
156
+ *
157
+ * Will be `null` if no properties are unique to the second object.
158
+ */
159
+ onlyInB: JsonValue;
160
+
161
+ /**
162
+ * Summary metadata about the differences found.
163
+ *
164
+ * Provides counts of added, removed, modified, and unchanged properties
165
+ * for quick assessment of the scope and nature of changes.
166
+ */
167
+ metadata: IThreeWayDiffMetadata;
168
+
169
+ /**
170
+ * True if the objects are identical, false otherwise.
171
+ *
172
+ * When `true`, both `onlyInA` and `onlyInB` will be `null`, and `unchanged`
173
+ * will contain the complete shared structure. The metadata will show zero
174
+ * added, removed, and modified properties.
175
+ */
176
+ identical: boolean;
177
+ }
178
+
179
+ /**
180
+ * Internal function to build three-way diff for objects.
181
+ */
182
+ function buildThreeWayDiffForObjects(
183
+ obj1: JsonValue,
184
+ obj2: JsonValue,
185
+ metadata: IThreeWayDiffMetadata
186
+ ): { onlyInA: JsonValue; unchanged: JsonValue; onlyInB: JsonValue } {
187
+ if (!isJsonObject(obj1) || !isJsonObject(obj2)) {
188
+ // Handle non-object cases
189
+ if (deepEquals(obj1, obj2)) {
190
+ metadata.unchanged++;
191
+ return {
192
+ onlyInA: null,
193
+ unchanged: obj1,
194
+ onlyInB: null
195
+ };
196
+ } else {
197
+ metadata.modified++;
198
+ return {
199
+ onlyInA: obj1,
200
+ unchanged: null,
201
+ onlyInB: obj2
202
+ };
203
+ }
204
+ }
205
+
206
+ const keys1 = new Set(Object.keys(obj1));
207
+ const keys2 = new Set(Object.keys(obj2));
208
+ const allKeys = new Set([...keys1, ...keys2]);
209
+
210
+ const onlyInA: Record<string, JsonValue> = {};
211
+ const unchanged: Record<string, JsonValue> = {};
212
+ const onlyInB: Record<string, JsonValue> = {};
213
+
214
+ let hasOnlyInA = false;
215
+ let hasUnchanged = false;
216
+ let hasOnlyInB = false;
217
+
218
+ for (const key of allKeys) {
219
+ if (!keys1.has(key)) {
220
+ // Property only exists in obj2
221
+ metadata.added++;
222
+ onlyInB[key] = obj2[key];
223
+ hasOnlyInB = true;
224
+ } else if (!keys2.has(key)) {
225
+ // Property only exists in obj1
226
+ metadata.removed++;
227
+ onlyInA[key] = obj1[key];
228
+ hasOnlyInA = true;
229
+ } else {
230
+ // Property exists in both
231
+ const val1 = obj1[key];
232
+ const val2 = obj2[key];
233
+
234
+ if (deepEquals(val1, val2)) {
235
+ // Values are identical
236
+ if (isJsonObject(val1) && isJsonObject(val2)) {
237
+ // For objects, we need to recurse to get proper metadata counts
238
+ const childDiff = buildThreeWayDiffForObjects(val1, val2, metadata);
239
+ if (childDiff.unchanged !== null) {
240
+ unchanged[key] = childDiff.unchanged;
241
+ hasUnchanged = true;
242
+ }
243
+ } else {
244
+ metadata.unchanged++;
245
+ unchanged[key] = val1;
246
+ hasUnchanged = true;
247
+ }
248
+ } else {
249
+ // Values are different
250
+ if (isJsonObject(val1) && isJsonObject(val2)) {
251
+ // For nested objects, recurse
252
+ const childDiff = buildThreeWayDiffForObjects(val1, val2, metadata);
253
+
254
+ if (childDiff.onlyInA !== null) {
255
+ onlyInA[key] = childDiff.onlyInA;
256
+ hasOnlyInA = true;
257
+ }
258
+ if (childDiff.unchanged !== null) {
259
+ unchanged[key] = childDiff.unchanged;
260
+ hasUnchanged = true;
261
+ }
262
+ if (childDiff.onlyInB !== null) {
263
+ onlyInB[key] = childDiff.onlyInB;
264
+ hasOnlyInB = true;
265
+ }
266
+ } else {
267
+ // For primitives or arrays, treat as complete replacement
268
+ metadata.modified++;
269
+ onlyInA[key] = val1;
270
+ onlyInB[key] = val2;
271
+ hasOnlyInA = true;
272
+ hasOnlyInB = true;
273
+ }
274
+ }
275
+ }
276
+ }
277
+
278
+ return {
279
+ onlyInA: hasOnlyInA ? onlyInA : null,
280
+ unchanged: hasUnchanged ? unchanged : null,
281
+ onlyInB: hasOnlyInB ? onlyInB : null
282
+ };
283
+ }
284
+
285
+ /**
286
+ * Performs a three-way diff comparison between two JSON values, returning separate
287
+ * objects containing the differences and similarities.
288
+ *
289
+ * This function provides an alternative to {@link Diff.jsonDiff} that focuses on actionable
290
+ * results rather than detailed change analysis. Instead of a list of individual changes,
291
+ * it returns three objects that can be directly used for merging, UI display, or
292
+ * programmatic manipulation.
293
+ *
294
+ * **Key Features:**
295
+ * - **Actionable Results**: Returns objects ready for immediate use in merging operations
296
+ * - **Simplified Array Handling**: Arrays are treated as atomic values for cleaner results
297
+ * - **Structural Preservation**: Maintains original JSON structure rather than flattened paths
298
+ * - **UI-Optimized**: Perfect for side-by-side diff displays and change visualization
299
+ * - **Merge-Friendly**: Designed specifically for three-way merge scenarios
300
+ *
301
+ * **Array Handling:**
302
+ * Unlike {@link Diff.jsonDiff}, this function treats arrays as complete units. If arrays differ,
303
+ * the entire array appears in the appropriate result object rather than computing
304
+ * element-by-element deltas. This approach is simpler and more predictable for most
305
+ * use cases involving data updates and synchronization.
306
+ *
307
+ * **Use Cases:**
308
+ * - Applying configuration updates while preserving unchanged settings
309
+ * - Creating side-by-side diff displays in user interfaces
310
+ * - Building three-way merge tools for data synchronization
311
+ * - Implementing undo/redo functionality with granular control
312
+ * - Generating patch objects for API updates
313
+ *
314
+ * @param obj1 - The first JSON value to compare (often the "before" or "source" state)
315
+ * @param obj2 - The second JSON value to compare (often the "after" or "target" state)
316
+ * @returns A Result containing the three-way diff with separate objects and metadata
317
+ *
318
+ * @example Basic usage for applying changes
319
+ * ```typescript
320
+ * const original = { name: "John", age: 30, city: "NYC", active: true };
321
+ * const updated = { name: "Jane", age: 30, country: "USA", active: true };
322
+ *
323
+ * const result = jsonThreeWayDiff(original, updated);
324
+ * if (result.success) {
325
+ * const { onlyInA, unchanged, onlyInB } = result.value;
326
+ *
327
+ * // Apply changes: merge unchanged + onlyInB
328
+ * const applied = { ...unchanged, ...onlyInB };
329
+ * console.log(applied); // { age: 30, active: true, name: "Jane", country: "USA" }
330
+ *
331
+ * // Revert changes: merge unchanged + onlyInA
332
+ * const reverted = { ...unchanged, ...onlyInA };
333
+ * console.log(reverted); // { age: 30, active: true, name: "John", city: "NYC" }
334
+ * }
335
+ * ```
336
+ *
337
+ * @example UI-friendly diff display
338
+ * ```typescript
339
+ * const result = jsonThreeWayDiff(userBefore, userAfter);
340
+ * if (result.success) {
341
+ * const { onlyInA, unchanged, onlyInB, metadata } = result.value;
342
+ *
343
+ * // Display summary
344
+ * console.log(`Changes: ${metadata.added} added, ${metadata.removed} removed, ${metadata.modified} modified`);
345
+ *
346
+ * // Show removed/old values in red
347
+ * if (onlyInA) displayInColor(onlyInA, 'red');
348
+ *
349
+ * // Show unchanged values in gray
350
+ * if (unchanged) displayInColor(unchanged, 'gray');
351
+ *
352
+ * // Show added/new values in green
353
+ * if (onlyInB) displayInColor(onlyInB, 'green');
354
+ * }
355
+ * ```
356
+ *
357
+ * @example Nested objects and array handling
358
+ * ```typescript
359
+ * const config1 = {
360
+ * database: { host: "localhost", port: 5432 },
361
+ * features: ["auth", "logging"],
362
+ * version: "1.0"
363
+ * };
364
+ * const config2 = {
365
+ * database: { host: "production.db", port: 5432 },
366
+ * features: ["auth", "logging", "metrics"], // Array treated as complete unit
367
+ * version: "1.1"
368
+ * };
369
+ *
370
+ * const result = jsonThreeWayDiff(config1, config2);
371
+ * if (result.success) {
372
+ * // result.value.onlyInA = { database: { host: "localhost" }, features: ["auth", "logging"], version: "1.0" }
373
+ * // result.value.unchanged = { database: { port: 5432 } }
374
+ * // result.value.onlyInB = { database: { host: "production.db" }, features: ["auth", "logging", "metrics"], version: "1.1" }
375
+ * }
376
+ * ```
377
+ *
378
+ * @example Conditional updates based on changes
379
+ * ```typescript
380
+ * const result = jsonThreeWayDiff(currentState, newState);
381
+ * if (result.success && !result.value.identical) {
382
+ * const { metadata } = result.value;
383
+ *
384
+ * if (metadata.modified > 0) {
385
+ * console.log("Critical settings changed - requires restart");
386
+ * } else if (metadata.added > 0) {
387
+ * console.log("New features enabled");
388
+ * } else if (metadata.removed > 0) {
389
+ * console.log("Features disabled");
390
+ * }
391
+ * }
392
+ * ```
393
+ *
394
+ * @see {@link IThreeWayDiff} for the structure of returned results
395
+ * @see {@link IThreeWayDiffMetadata} for metadata details
396
+ * @see {@link Diff.jsonDiff} for detailed change-by-change analysis
397
+ * @see {@link jsonEquals} for simple equality checking
398
+ *
399
+ * @public
400
+ */
401
+ export function jsonThreeWayDiff(obj1: JsonValue, obj2: JsonValue): Result<IThreeWayDiff> {
402
+ const metadata: IThreeWayDiffMetadata = {
403
+ removed: 0,
404
+ added: 0,
405
+ modified: 0,
406
+ unchanged: 0
407
+ };
408
+
409
+ const diff = buildThreeWayDiffForObjects(obj1, obj2, metadata);
410
+
411
+ const result: IThreeWayDiff = {
412
+ onlyInA: diff.onlyInA,
413
+ unchanged: diff.unchanged,
414
+ onlyInB: diff.onlyInB,
415
+ metadata,
416
+ identical: metadata.removed === 0 && metadata.added === 0 && metadata.modified === 0
417
+ };
418
+
419
+ return succeed(result);
420
+ }
@@ -0,0 +1,66 @@
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
+
23
+ import { JsonValue, isJsonObject, isJsonArray, isJsonPrimitive } from '@fgv/ts-json-base';
24
+
25
+ /**
26
+ * Deep comparison function for JSON values that handles all JSON types.
27
+ */
28
+ export function deepEquals(a: JsonValue, b: JsonValue): boolean {
29
+ if (a === b) {
30
+ return true;
31
+ }
32
+
33
+ if (isJsonPrimitive(a) || isJsonPrimitive(b)) {
34
+ return a === b;
35
+ }
36
+
37
+ if (isJsonArray(a) && isJsonArray(b)) {
38
+ if (a.length !== b.length) {
39
+ return false;
40
+ }
41
+ for (let i = 0; i < a.length; i++) {
42
+ if (!deepEquals(a[i], b[i])) {
43
+ return false;
44
+ }
45
+ }
46
+ return true;
47
+ }
48
+
49
+ if (isJsonObject(a) && isJsonObject(b)) {
50
+ const keysA = Object.keys(a);
51
+ const keysB = Object.keys(b);
52
+
53
+ if (keysA.length !== keysB.length) {
54
+ return false;
55
+ }
56
+
57
+ for (const key of keysA) {
58
+ if (!(key in b) || !deepEquals(a[key], b[key])) {
59
+ return false;
60
+ }
61
+ }
62
+ return true;
63
+ }
64
+
65
+ return false;
66
+ }
@@ -0,0 +1,125 @@
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 { JsonValue } from '@fgv/ts-json-base';
23
+ import { DetailedResult } from '@fgv/ts-utils';
24
+ import { IJsonContext } from '../context';
25
+
26
+ /**
27
+ * Possible `DetailedResult` details for various editor operations.
28
+ * @public
29
+ */
30
+ export type JsonEditFailureReason = 'ignore' | 'inapplicable' | 'edited' | 'error';
31
+
32
+ /**
33
+ * Possible `DetailedResult` details for property edit operations.
34
+ * @public
35
+ */
36
+ export type JsonPropertyEditFailureReason = JsonEditFailureReason | 'deferred';
37
+
38
+ /**
39
+ * Possible validation rules for a {@link JsonEditor | JsonEditor}.
40
+ * @public
41
+ */
42
+ export type JsonEditorValidationRules =
43
+ | 'invalidPropertyName'
44
+ | 'invalidPropertyValue'
45
+ | 'undefinedPropertyValue';
46
+
47
+ /**
48
+ * Array merge behavior options for a {@link JsonEditor | JsonEditor}.
49
+ * @public
50
+ */
51
+ export type ArrayMergeBehavior = 'append' | 'replace';
52
+
53
+ /**
54
+ * Merge options for a {@link JsonEditor | JsonEditor}.
55
+ * @public
56
+ */
57
+ export interface IJsonEditorMergeOptions {
58
+ /**
59
+ * Controls how arrays are merged when combining JSON values.
60
+ * - `'append'` (default): Existing array elements are preserved and new elements are appended
61
+ * - `'replace'`: Existing array is completely replaced with the new array
62
+ */
63
+ arrayMergeBehavior: ArrayMergeBehavior;
64
+
65
+ /**
66
+ * Controls whether null values should be treated as property deletion during merge operations.
67
+ * - `false` (default): Null values are merged normally, setting the property to null
68
+ * - `true`: Null values delete the property from the target object during merge
69
+ */
70
+ nullAsDelete?: boolean;
71
+ }
72
+
73
+ /**
74
+ * Validation options for a {@link JsonEditor | JsonEditor}.
75
+ * @public
76
+ */
77
+ export interface IJsonEditorValidationOptions {
78
+ /**
79
+ * If `onInvalidPropertyName` is `'error'` (default) then any property name
80
+ * that is invalid after template rendering causes an error and stops
81
+ * conversion. If `onInvalidPropertyName` is `'ignore'`, then names which
82
+ * are invalid after template rendering are passed through unchanged.
83
+ */
84
+ onInvalidPropertyName: 'error' | 'ignore';
85
+
86
+ /**
87
+ * If `onInvalidPropertyValue` is `'error'` (default) then any illegal
88
+ * property value other than `undefined` causes an error and stops
89
+ * conversion. If `onInvalidPropertyValue` is `'ignore'` then any
90
+ * invalid property values are silently ignored.
91
+ */
92
+ onInvalidPropertyValue: 'error' | 'ignore';
93
+
94
+ /**
95
+ * If `onUndefinedPropertyValue` is `'error'`, then any property with
96
+ * value `undefined` will cause an error and stop conversion. If
97
+ * `onUndefinedPropertyValue` is `'ignore'` (default) then any
98
+ * property with value `undefined` is silently ignored.
99
+ */
100
+ onUndefinedPropertyValue: 'error' | 'ignore';
101
+ }
102
+
103
+ /**
104
+ * Initialization options for a {@link JsonEditor | JsonEditor}.
105
+ * @public
106
+ */
107
+ export interface IJsonEditorOptions {
108
+ context?: IJsonContext;
109
+ validation: IJsonEditorValidationOptions;
110
+ merge?: IJsonEditorMergeOptions;
111
+ }
112
+
113
+ /**
114
+ * A specialized JSON editor which does a deep clone of a supplied `JsonValue`.
115
+ * @public
116
+ */
117
+ export interface IJsonCloneEditor {
118
+ /**
119
+ * Returns a deep clone of a supplied `JsonValue`.
120
+ * @param src - The `JsonValue` to be cloned.
121
+ * @param context - An optional {@link IJsonContext | JSON context} used for clone
122
+ * conversion operations.
123
+ */
124
+ clone(src: JsonValue, context?: IJsonContext): DetailedResult<JsonValue, JsonEditFailureReason>;
125
+ }
@@ -0,0 +1,36 @@
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
+
23
+ import * as EditorRules from './rules';
24
+
25
+ export * from './common';
26
+ export { JsonEditor } from './jsonEditor';
27
+ export { IJsonEditorRule, JsonEditorRuleBase } from './jsonEditorRule';
28
+ export { JsonEditorState } from './jsonEditorState';
29
+ export {
30
+ IReferenceMapKeyPolicyValidateOptions,
31
+ ISimpleJsonMapOptions,
32
+ PrefixedJsonMap,
33
+ ReferenceMapKeyPolicy,
34
+ SimpleJsonMap
35
+ } from './jsonReferenceMap';
36
+ export { EditorRules };