@diffson/core 1.0.0 → 1.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.
Files changed (56) hide show
  1. package/README.md +264 -0
  2. package/package.json +5 -1
  3. package/.turbo/turbo-build.log +0 -1
  4. package/.turbo/turbo-test.log +0 -96
  5. package/.turbo/turbo-typecheck.log +0 -2
  6. package/src/contract/constant/DiffConstants.ts +0 -10
  7. package/src/contract/constant/PresetName.ts +0 -6
  8. package/src/contract/constant/index.ts +0 -2
  9. package/src/contract/index.ts +0 -2
  10. package/src/contract/type/ArrayComparator.ts +0 -15
  11. package/src/contract/type/ComparatorOrchestrator.ts +0 -16
  12. package/src/contract/type/DiffService.ts +0 -31
  13. package/src/contract/type/JsonTypes.ts +0 -3
  14. package/src/contract/type/NullComparator.ts +0 -9
  15. package/src/contract/type/ObjectComparator.ts +0 -15
  16. package/src/contract/type/OtherComparator.ts +0 -14
  17. package/src/contract/type/PrimitiveComparator.ts +0 -13
  18. package/src/contract/type/Result.ts +0 -7
  19. package/src/contract/type/SingleNodeDifference.ts +0 -13
  20. package/src/contract/type/index.ts +0 -10
  21. package/src/index.ts +0 -17
  22. package/src/service/comparator/ComparatorOrchestrator.ts +0 -50
  23. package/src/service/comparator/array/AbstractArray.ts +0 -34
  24. package/src/service/comparator/array/SequentialArrayComparator.test.ts +0 -46
  25. package/src/service/comparator/array/SequentialArrayComparator.ts +0 -48
  26. package/src/service/comparator/array/SimilarArrayComparator.test.ts +0 -37
  27. package/src/service/comparator/array/SimilarArrayComparator.ts +0 -211
  28. package/src/service/comparator/array/index.ts +0 -3
  29. package/src/service/comparator/index.ts +0 -6
  30. package/src/service/comparator/nulls/DefaultNullComparator.test.ts +0 -38
  31. package/src/service/comparator/nulls/DefaultNullComparator.ts +0 -9
  32. package/src/service/comparator/nulls/index.ts +0 -1
  33. package/src/service/comparator/object/AbstractObject.ts +0 -102
  34. package/src/service/comparator/object/LeftJoinObjectComparator.test.ts +0 -71
  35. package/src/service/comparator/object/LeftJoinObjectComparator.ts +0 -18
  36. package/src/service/comparator/object/UnionKeyObjectComparator.test.ts +0 -20
  37. package/src/service/comparator/object/UnionKeyObjectComparator.ts +0 -19
  38. package/src/service/comparator/object/index.ts +0 -3
  39. package/src/service/comparator/other/DefaultOtherComparator.test.ts +0 -48
  40. package/src/service/comparator/other/DefaultOtherComparator.ts +0 -23
  41. package/src/service/comparator/other/index.ts +0 -1
  42. package/src/service/comparator/primitive/DefaultPrimitiveComparator.test.ts +0 -50
  43. package/src/service/comparator/primitive/DefaultPrimitiveComparator.ts +0 -27
  44. package/src/service/comparator/primitive/index.ts +0 -1
  45. package/src/service/diff/Diff.test.ts +0 -113
  46. package/src/service/diff/DiffContext.ts +0 -37
  47. package/src/service/diff/DiffService.test.ts +0 -292
  48. package/src/service/diff/DiffService.ts +0 -245
  49. package/src/service/diff/PathTracker.ts +0 -71
  50. package/src/service/diff/index.ts +0 -1
  51. package/src/service/index.ts +0 -2
  52. package/src/util/TypeGuards.test.ts +0 -90
  53. package/src/util/TypeGuards.ts +0 -33
  54. package/src/util/index.ts +0 -1
  55. package/tsconfig.json +0 -11
  56. package/tsconfig.tsbuildinfo +0 -1
package/README.md ADDED
@@ -0,0 +1,264 @@
1
+ # @diffson/core
2
+
3
+ A powerful TypeScript library for comparing JSON objects with multiple comparison strategies.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @diffson/core
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { DiffService, PresetName } from '@diffson/core';
15
+
16
+ // Create a diff service
17
+ const diffService = new DiffService();
18
+
19
+ // Compare two JSON objects
20
+ const left = { name: 'John', age: 30 };
21
+ const right = { name: 'John', age: 31 };
22
+
23
+ const results = diffService.diffElement(left, right);
24
+
25
+ console.log(results);
26
+ // [
27
+ // {
28
+ // leftPath: 'age',
29
+ // rightPath: 'age',
30
+ // left: '30',
31
+ // right: '31',
32
+ // diffType: 'MODIFY'
33
+ // }
34
+ // ]
35
+ ```
36
+
37
+ ## API Reference
38
+
39
+ ### DiffService
40
+
41
+ The main class for comparing JSON objects.
42
+
43
+ #### Constructor
44
+
45
+ ```typescript
46
+ new DiffService(preset?: PresetName)
47
+ ```
48
+
49
+ **Parameters:**
50
+ - `preset` (optional): Comparison strategy preset. Default: `PresetName.FullSmart`
51
+
52
+ **Presets:**
53
+ - `PresetName.FullSmart` - Smart comparison of all fields (default)
54
+ - `PresetName.FullOrdered` - Order-sensitive comparison
55
+ - `PresetName.LeftSmart` - Compare only fields in left object, smart mode
56
+ - `PresetName.LeftOrdered` - Compare only fields in left object, order-sensitive
57
+
58
+ #### Methods
59
+
60
+ ##### `diffElement(left, right, options?)`
61
+
62
+ Compare two JSON values directly.
63
+
64
+ ```typescript
65
+ diffElement(
66
+ left: JsonValue,
67
+ right: JsonValue,
68
+ options?: {
69
+ noisePath?: string[];
70
+ specialPath?: string[];
71
+ parseNestedJson?: boolean;
72
+ }
73
+ ): Result[]
74
+ ```
75
+
76
+ **Parameters:**
77
+ - `left`: First JSON value
78
+ - `right`: Second JSON value
79
+ - `options` (optional):
80
+ - `noisePath`: Array of paths to ignore during comparison
81
+ - `specialPath`: Array of paths to mark as special even if values match
82
+ - `parseNestedJson`: Parse nested JSON strings recursively
83
+
84
+ **Returns:** Array of `Result` objects representing differences.
85
+
86
+ ##### `diffJson(leftJson, rightJson, options?)`
87
+
88
+ Compare two JSON strings.
89
+
90
+ ```typescript
91
+ diffJson(
92
+ leftJson: string,
93
+ rightJson: string,
94
+ options?: {
95
+ noisePath?: string[];
96
+ specialPath?: string[];
97
+ parseNestedJson?: boolean;
98
+ }
99
+ ): Result[]
100
+ ```
101
+
102
+ **Parameters:**
103
+ - `leftJson`: First JSON string
104
+ - `rightJson`: Second JSON string
105
+ - `options`: Same as `diffElement`
106
+
107
+ **Returns:** Array of `Result` objects representing differences.
108
+
109
+ ### Result Object
110
+
111
+ Each difference is represented by a `Result` object:
112
+
113
+ ```typescript
114
+ {
115
+ leftPath: string | null; // Path in left object
116
+ rightPath: string | null; // Path in right object
117
+ left: unknown; // Value in left object
118
+ right: unknown; // Value in right object
119
+ diffType: string; // 'ADD' | 'DELETE' | 'MODIFY'
120
+ }
121
+ ```
122
+
123
+ **Diff Types:**
124
+ - `ADD`: Field exists in right but not in left
125
+ - `DELETE`: Field exists in left but not in right
126
+ - `MODIFY`: Field exists in both but with different values
127
+
128
+ ## Usage Examples
129
+
130
+ ### Basic Comparison
131
+
132
+ ```typescript
133
+ import { DiffService } from '@diffson/core';
134
+
135
+ const diffService = new DiffService();
136
+
137
+ const left = { a: 1, b: 2 };
138
+ const right = { a: 1, b: 3, c: 4 };
139
+
140
+ const results = diffService.diffElement(left, right);
141
+ // Results: [
142
+ // { leftPath: 'b', rightPath: 'b', left: '2', right: '3', diffType: 'MODIFY' },
143
+ // { leftPath: null, rightPath: 'c', left: null, right: '4', diffType: 'ADD' }
144
+ // ]
145
+ ```
146
+
147
+ ### Using Presets
148
+
149
+ ```typescript
150
+ import { DiffService, PresetName } from '@diffson/core';
151
+
152
+ // Order-sensitive comparison
153
+ const orderedDiff = new DiffService(PresetName.FullOrdered);
154
+ const results1 = orderedDiff.diffElement([1, 2, 3], [1, 3, 2]);
155
+ // Detects order changes
156
+
157
+ // Left-only comparison (ignore fields only in right)
158
+ const leftDiff = new DiffService(PresetName.LeftSmart);
159
+ const left = { a: 1, b: 2 };
160
+ const right = { a: 1, b: 2, c: 3 };
161
+ const results2 = leftDiff.diffElement(left, right);
162
+ // Results: [] (ignores 'c' since it's not in left)
163
+ ```
164
+
165
+ ### Ignoring Paths (Noise Paths)
166
+
167
+ ```typescript
168
+ const diffService = new DiffService();
169
+
170
+ const left = { name: 'John', timestamp: 1000 };
171
+ const right = { name: 'John', timestamp: 2000 };
172
+
173
+ // Ignore timestamp field
174
+ const results = diffService.diffElement(left, right, {
175
+ noisePath: ['timestamp']
176
+ });
177
+ // Results: [] (timestamp is ignored)
178
+ ```
179
+
180
+ ### Nested Objects
181
+
182
+ ```typescript
183
+ const left = {
184
+ user: { name: 'Alice', age: 30 },
185
+ items: [{ id: 1, name: 'Item 1' }]
186
+ };
187
+
188
+ const right = {
189
+ user: { name: 'Bob', age: 30 },
190
+ items: [{ id: 1, name: 'Item 1' }]
191
+ };
192
+
193
+ const results = diffService.diffElement(left, right);
194
+ // Results: [
195
+ // { leftPath: 'user.name', rightPath: 'user.name', left: 'Alice', right: 'Bob', diffType: 'MODIFY' }
196
+ // ]
197
+ ```
198
+
199
+ ### Parsing Nested JSON Strings
200
+
201
+ ```typescript
202
+ const left = { data: '{"nested":"value1"}' };
203
+ const right = { data: '{"nested":"value2"}' };
204
+
205
+ // Without parseNestedJson
206
+ const results1 = diffService.diffElement(left, right);
207
+ // Compares strings: '{"nested":"value1"}' vs '{"nested":"value2"}'
208
+
209
+ // With parseNestedJson
210
+ const results2 = diffService.diffElement(left, right, {
211
+ parseNestedJson: true
212
+ });
213
+ // Parses and compares: { nested: 'value1' } vs { nested: 'value2' }
214
+ // Results: [{ leftPath: 'data.nested', rightPath: 'data.nested', ... }]
215
+ ```
216
+
217
+ ### Comparing JSON Strings
218
+
219
+ ```typescript
220
+ const leftJson = '{"name":"John","age":30}';
221
+ const rightJson = '{"name":"John","age":31}';
222
+
223
+ const results = diffService.diffJson(leftJson, rightJson);
224
+ // Same as diffElement but accepts JSON strings
225
+ ```
226
+
227
+ ### Fluent API (Advanced)
228
+
229
+ ```typescript
230
+ import { DiffService, SequentialArrayComparator, LeftJoinObjectComparator } from '@diffson/core';
231
+
232
+ const diffService = new DiffService()
233
+ .withArrayComparator(SequentialArrayComparator)
234
+ .withObjectComparator(LeftJoinObjectComparator);
235
+
236
+ const results = diffService.diffElement(left, right);
237
+ // Uses custom comparators
238
+ ```
239
+
240
+ ## Path Format
241
+
242
+ Paths use dot notation to reference nested fields:
243
+
244
+ - `field` - Object field
245
+ - `array.[0]` - Array element at index 0
246
+ - `parent.child` - Nested object field
247
+ - `items.[0].name` - Field in array element
248
+
249
+ When using `noisePath`, array indices are auto-filtered:
250
+ - `items.name` matches `items.[0].name`, `items.[1].name`, etc.
251
+
252
+ ## TypeScript Support
253
+
254
+ This library is written in TypeScript and includes full type definitions.
255
+
256
+ ```typescript
257
+ import type { JsonValue, Result, PresetName } from '@diffson/core';
258
+ ```
259
+
260
+ ## License
261
+
262
+ MIT
263
+
264
+
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "@diffson/core",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
+ "files": [
8
+ "dist",
9
+ "README.md"
10
+ ],
7
11
  "imports": {
8
12
  "#contract": "./src/contract/index.ts",
9
13
  "#service": "./src/service/index.ts"
@@ -1 +0,0 @@
1
- $ tsc
@@ -1,96 +0,0 @@
1
-
2
- $ bun test
3
- bun test v1.3.5 (1e86cebd)
4
- 
5
- src/util/TypeGuards.test.ts:
6
- ✓ TypeGuards > isJsonObject > should return true for objects [0.92ms]
7
- ✓ TypeGuards > isJsonObject > should return false for non-objects [0.05ms]
8
- ✓ TypeGuards > isJsonArray > should return true for arrays [0.02ms]
9
- ✓ TypeGuards > isJsonArray > should return false for non-arrays [0.02ms]
10
- ✓ TypeGuards > isJsonPrimitive > should return true for primitives [0.01ms]
11
- ✓ TypeGuards > isJsonPrimitive > should return false for non-primitives [0.02ms]
12
- ✓ TypeGuards > isJsonNull > should return true for null [0.02ms]
13
- ✓ TypeGuards > isJsonNull > should return false for non-null [0.01ms]
14
- ✓ TypeGuards > jsonElement2Str > should convert primitives to strings [0.03ms]
15
- ✓ TypeGuards > jsonElement2Str > should return null for undefined
16
- ✓ TypeGuards > jsonElement2Str > should return 'null' for null
17
- ✓ TypeGuards > jsonElement2Str > should return placeholder for objects
18
- ✓ TypeGuards > jsonElement2Str > should return placeholder for arrays
19
- 
20
- src/service/diff/Diff.test.ts:
21
- ✓ DiffService > diffElement > should return empty array for identical objects [1.15ms]
22
- ✓ DiffService > diffElement > should detect modified values [0.12ms]
23
- ✓ DiffService > diffElement > should detect added fields [0.06ms]
24
- ✓ DiffService > diffElement > should detect deleted fields [0.13ms]
25
- ✓ DiffService > diffJson > should parse JSON strings and diff them [0.10ms]
26
- ✓ DiffService > diffJson > should return empty array for identical JSON strings [0.09ms]
27
- ✓ DiffService > diffJson > should throw error for invalid left JSON string [0.31ms]
28
- ✓ DiffService > diffJson > should throw error for invalid right JSON string [0.02ms]
29
- ✓ DiffService > nested objects > should handle nested objects [0.09ms]
30
- 
31
- src/service/diff/DiffService.test.ts:
32
- ✓ DiffService > basic comparison > should return empty array for identical objects [0.10ms]
33
- ✓ DiffService > basic comparison > should detect modified values [0.06ms]
34
- [
35
- Result {
36
- leftPath: "items.[1]",
37
- rightPath: "items.[1]",
38
- left: "2",
39
- right: "4",
40
- diffType: "MODIFY",
41
- }
42
- ]
43
- ✓ DiffService > withArrayComparator > should use sequential array comparator when configured [0.85ms]
44
- ✓ DiffService > withArrayComparator > should reuse injector when configuration changes [0.40ms]
45
- ✓ DiffService > preset > should use FullOrdered preset [0.04ms]
46
- ✓ DiffService > preset > should use LeftSmart preset [0.03ms]
47
- ✓ DiffService > preset > should use LeftOrdered preset [0.05ms]
48
- ✓ DiffService > compareWithOptions > should ignore noise paths [0.06ms]
49
- ✓ DiffService > compareWithOptions > should handle special paths [0.13ms]
50
- ✓ DiffService > fluent API > should support method chaining [0.34ms]
51
- ✓ DiffService - parseNestedJson > should parse nested JSON strings when parseNestedJson option is true [0.61ms]
52
- ✓ DiffService - parseNestedJson > should not parse nested JSON strings when parseNestedJson option is false or undefined [0.07ms]
53
- ✓ DiffService - parseNestedJson > should handle deeply nested JSON strings [0.09ms]
54
- ✓ DiffService - parseNestedJson > should handle arrays containing JSON strings [0.11ms]
55
- ✓ DiffService - parseNestedJson > should handle mixed content with both JSON strings and regular strings [0.07ms]
56
- ✓ DiffService - parseNestedJson > should not fail on invalid JSON strings [0.07ms]
57
- 
58
- src/service/comparator/array/SequentialArrayComparator.test.ts:
59
- ✓ SequentialArrayComparator > should compare arrays by index [0.13ms]
60
- ✓ SequentialArrayComparator > should detect added elements [0.03ms]
61
- ✓ SequentialArrayComparator > should detect deleted elements [0.04ms]
62
- 
63
- src/service/comparator/array/SimilarArrayComparator.test.ts:
64
- ✓ SimilarArrayComparator > should match similar elements [0.04ms]
65
- ✓ SimilarArrayComparator > should detect modifications in matched elements [0.10ms]
66
- ✓ SimilarArrayComparator > should detect added elements [0.05ms]
67
- 
68
- src/service/comparator/primitive/DefaultPrimitiveComparator.test.ts:
69
- ✓ DefaultPrimitiveComparator > should detect string changes
70
- ✓ DefaultPrimitiveComparator > should detect number changes [0.12ms]
71
- ✓ DefaultPrimitiveComparator > should detect boolean changes [0.02ms]
72
- ✓ DefaultPrimitiveComparator > should not report identical primitives [0.03ms]
73
- 
74
- src/service/comparator/other/DefaultOtherComparator.test.ts:
75
- ✓ DefaultOtherComparator > should detect type changes from object to array
76
- ✓ DefaultOtherComparator > should detect type changes from array to primitive [0.22ms]
77
- ✓ DefaultOtherComparator > should detect undefined to value [0.02ms]
78
- ✓ DefaultOtherComparator > should detect value to undefined [0.04ms]
79
- 
80
- src/service/comparator/nulls/DefaultNullComparator.test.ts:
81
- ✓ DefaultNullComparator > should not report differences for null vs null [0.23ms]
82
- ✓ DefaultNullComparator > should detect null to value changes [0.16ms]
83
- ✓ DefaultNullComparator > should detect value to null changes [0.03ms]
84
- 
85
- src/service/comparator/object/UnionKeyObjectComparator.test.ts:
86
- ✓ UnionKeyObjectComparator > should compare all keys from both objects [0.12ms]
87
- 
88
- src/service/comparator/object/LeftJoinObjectComparator.test.ts:
89
- ✓ LeftJoinObjectComparator > should only compare keys from left object [0.15ms]
90
- ✓ LeftJoinObjectComparator > should detect changes in left keys [0.04ms]
91
- ✓ LeftJoinObjectComparator > should detect missing keys in right object [0.03ms]
92
-
93
-  59 pass
94
-  0 fail
95
- 154 expect() calls
96
- Ran 59 tests across 10 files. [27.00ms]
@@ -1,2 +0,0 @@
1
-
2
- $ tsc --noEmit
@@ -1,10 +0,0 @@
1
- export const DIFFERENT = false;
2
- export const SAME = true;
3
- export const SPLIT_PATH = "\\.";
4
- export const MERGE_PATH = ".";
5
-
6
- export const OBJECT_NULL = null;
7
- export const TYPE_MODIFY = "MODIFY";
8
- export const TYPE_ADD = "ADD";
9
- export const TYPE_DELETE = "DELETE";
10
-
@@ -1,6 +0,0 @@
1
- export enum PresetName {
2
- FullSmart = "fullSmart",
3
- FullOrdered = "fullOrdered",
4
- LeftSmart = "leftSmart",
5
- LeftOrdered = "leftOrdered",
6
- }
@@ -1,2 +0,0 @@
1
- export * from "./DiffConstants";
2
- export * from "./PresetName";
@@ -1,2 +0,0 @@
1
- export * from "./type";
2
- export * from "./constant";
@@ -1,15 +0,0 @@
1
- import { createIdentifier } from "@wendellhu/redi";
2
- import type { JsonArray, JsonValue } from "./JsonTypes";
3
- import type { DiffContext } from "../../service/diff/DiffContext";
4
- import type { PathTracker } from "../../service/diff/PathTracker";
5
-
6
- export interface IArrayComparator {
7
- diffArray(a: JsonArray, b: JsonArray, pathTracker: PathTracker): DiffContext;
8
- diffElement(
9
- a: JsonValue | undefined,
10
- b: JsonValue | undefined,
11
- pathTracker: PathTracker
12
- ): DiffContext;
13
- }
14
-
15
- export const IArrayComparator = createIdentifier<IArrayComparator>("IArrayComparator");
@@ -1,16 +0,0 @@
1
- import { createIdentifier } from "@wendellhu/redi";
2
- import type { JsonValue } from "./JsonTypes";
3
- import type { DiffContext } from "../../service/diff/DiffContext";
4
- import type { PathTracker } from "../../service/diff/PathTracker";
5
- import type { IArrayComparator } from "./ArrayComparator";
6
-
7
- export interface IComparatorOrchestrator {
8
- diffElement(
9
- a: JsonValue | undefined,
10
- b: JsonValue | undefined,
11
- pathTracker: PathTracker
12
- ): DiffContext;
13
- getArrayComparator(): IArrayComparator;
14
- }
15
-
16
- export const IComparatorOrchestrator = createIdentifier<IComparatorOrchestrator>("IComparatorOrchestrator");
@@ -1,31 +0,0 @@
1
- import { createIdentifier } from "@wendellhu/redi";
2
- import type { JsonValue } from "./JsonTypes";
3
- import type { Result } from "./Result";
4
-
5
- export interface IDiffService {
6
- /**
7
- * Diff two JSON strings
8
- * @param leftJson - Left JSON string
9
- * @param rightJson - Right JSON string
10
- * @returns Array of differences
11
- */
12
- diffJson(leftJson: string, rightJson: string, options?: {
13
- noisePath?: string[];
14
- specialPath?: string[];
15
- parseNestedJson?: boolean;
16
- }): Result[];
17
-
18
- /**
19
- * Diff two JSON objects directly
20
- * @param left - Left JSON value
21
- * @param right - Right JSON value
22
- * @returns Array of differences
23
- */
24
- diffElement(left: JsonValue, right: JsonValue, options?: {
25
- noisePath?: string[];
26
- specialPath?: string[];
27
- parseNestedJson?: boolean;
28
- }): Result[];
29
- }
30
-
31
- export const IDiffService = createIdentifier<IDiffService>("IDiffService");
@@ -1,3 +0,0 @@
1
- export type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
2
- export type JsonObject = { [key: string]: JsonValue };
3
- export type JsonArray = JsonValue[];
@@ -1,9 +0,0 @@
1
- import { createIdentifier } from "@wendellhu/redi";
2
- import type { DiffContext } from "../../service/diff/DiffContext";
3
- import type { PathTracker } from "../../service/diff/PathTracker";
4
-
5
- export interface INullComparator {
6
- diff(a: null, b: null, pathTracker: PathTracker): DiffContext;
7
- }
8
-
9
- export const INullComparator = createIdentifier<INullComparator>("INullComparator");
@@ -1,15 +0,0 @@
1
- import { createIdentifier } from "@wendellhu/redi";
2
- import type { JsonObject, JsonValue } from "./JsonTypes";
3
- import type { DiffContext } from "../../service/diff/DiffContext";
4
- import type { PathTracker } from "../../service/diff/PathTracker";
5
-
6
- export interface IObjectComparator {
7
- diff(a: JsonObject, b: JsonObject, pathTracker: PathTracker): DiffContext;
8
- diffElement(
9
- a: JsonValue | undefined,
10
- b: JsonValue | undefined,
11
- pathTracker: PathTracker
12
- ): DiffContext;
13
- }
14
-
15
- export const IObjectComparator = createIdentifier<IObjectComparator>("IObjectComparator");
@@ -1,14 +0,0 @@
1
- import { createIdentifier } from "@wendellhu/redi";
2
- import type { JsonValue } from "./JsonTypes";
3
- import type { DiffContext } from "../../service/diff/DiffContext";
4
- import type { PathTracker } from "../../service/diff/PathTracker";
5
-
6
- export interface IOtherComparator {
7
- diff(
8
- a: JsonValue | undefined,
9
- b: JsonValue | undefined,
10
- pathTracker: PathTracker
11
- ): DiffContext;
12
- }
13
-
14
- export const IOtherComparator = createIdentifier<IOtherComparator>("IOtherComparator");
@@ -1,13 +0,0 @@
1
- import { createIdentifier } from "@wendellhu/redi";
2
- import type { DiffContext } from "../../service/diff/DiffContext";
3
- import type { PathTracker } from "../../service/diff/PathTracker";
4
-
5
- export interface IPrimitiveComparator {
6
- diff(
7
- a: string | number | boolean,
8
- b: string | number | boolean,
9
- pathTracker: PathTracker
10
- ): DiffContext;
11
- }
12
-
13
- export const IPrimitiveComparator = createIdentifier<IPrimitiveComparator>("IPrimitiveComparator");
@@ -1,7 +0,0 @@
1
- export class Result {
2
- leftPath: string | null = null;
3
- rightPath: string | null = null;
4
- left: unknown = null;
5
- right: unknown = null;
6
- diffType: string = "";
7
- }
@@ -1,13 +0,0 @@
1
- export class SingleNodeDifference {
2
- leftPath: string;
3
- rightPath: string;
4
- left: unknown;
5
- right: unknown;
6
-
7
- constructor(leftPath: string, rightPath: string, left: unknown, right: unknown) {
8
- this.leftPath = leftPath;
9
- this.rightPath = rightPath;
10
- this.left = left;
11
- this.right = right;
12
- }
13
- }
@@ -1,10 +0,0 @@
1
- export * from "./JsonTypes";
2
- export * from "./Result";
3
- export * from "./SingleNodeDifference";
4
- export * from "./ComparatorOrchestrator";
5
- export * from "./ObjectComparator";
6
- export * from "./ArrayComparator";
7
- export * from "./PrimitiveComparator";
8
- export * from "./NullComparator";
9
- export * from "./OtherComparator";
10
- export * from "./DiffService";
package/src/index.ts DELETED
@@ -1,17 +0,0 @@
1
- // Public API - Types
2
- export type { JsonValue, JsonObject, JsonArray } from "./contract/type/JsonTypes";
3
- export { Result } from "./contract/type/Result";
4
- export * from "./contract/type/DiffService";
5
-
6
- // Public API - Constants
7
- export {
8
- TYPE_ADD,
9
- TYPE_DELETE,
10
- TYPE_MODIFY,
11
- } from "./contract/constant";
12
-
13
- // Public API - Enums
14
- export { PresetName } from "./contract/constant/PresetName";
15
-
16
- // Public API - Service
17
- export { DiffService } from "./service";
@@ -1,50 +0,0 @@
1
- import { Inject } from "@wendellhu/redi";
2
- import {
3
- type IComparatorOrchestrator,
4
- type IObjectComparator,
5
- IObjectComparator as IObjectComparatorToken,
6
- type IArrayComparator,
7
- IArrayComparator as IArrayComparatorToken,
8
- type IPrimitiveComparator,
9
- IPrimitiveComparator as IPrimitiveComparatorToken,
10
- type INullComparator,
11
- INullComparator as INullComparatorToken,
12
- type IOtherComparator,
13
- IOtherComparator as IOtherComparatorToken,
14
- type JsonValue,
15
- } from "../../contract/type";
16
- import { DiffContext } from "../diff/DiffContext";
17
- import type { PathTracker } from "../diff/PathTracker";
18
- import { isJsonObject, isJsonArray, isJsonPrimitive, isJsonNull } from "../../util";
19
-
20
- export class ComparatorOrchestrator implements IComparatorOrchestrator {
21
- constructor(
22
- @Inject(IObjectComparatorToken) protected objectComparator: IObjectComparator,
23
- @Inject(IArrayComparatorToken) protected arrayComparator: IArrayComparator,
24
- @Inject(IPrimitiveComparatorToken) protected primitiveComparator: IPrimitiveComparator,
25
- @Inject(INullComparatorToken) protected nullComparator: INullComparator,
26
- @Inject(IOtherComparatorToken) protected otherComparator: IOtherComparator
27
- ) {}
28
-
29
- diffElement(
30
- a: JsonValue | undefined,
31
- b: JsonValue | undefined,
32
- pathTracker: PathTracker
33
- ): DiffContext {
34
- if (isJsonObject(a) && isJsonObject(b)) {
35
- return this.objectComparator.diff(a, b, pathTracker);
36
- } else if (isJsonArray(a) && isJsonArray(b)) {
37
- return this.arrayComparator.diffArray(a, b, pathTracker);
38
- } else if (isJsonPrimitive(a) && isJsonPrimitive(b)) {
39
- return this.primitiveComparator.diff(a, b, pathTracker);
40
- } else if (isJsonNull(a) && isJsonNull(b)) {
41
- return this.nullComparator.diff(a, b, pathTracker);
42
- } else {
43
- return this.otherComparator.diff(a, b, pathTracker);
44
- }
45
- }
46
-
47
- getArrayComparator(): IArrayComparator {
48
- return this.arrayComparator;
49
- }
50
- }