@arcanejs/diff 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +28 -2
- package/dist/index.d.ts +28 -2
- package/dist/index.js +66 -2
- package/dist/index.mjs +65 -3
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
type JSONPrimitive = string | number | boolean | null;
|
|
2
|
+
type JSONObject = {
|
|
3
|
+
[member: string]: JSONValue | undefined;
|
|
4
|
+
};
|
|
5
|
+
type JSONArray = JSONValue[];
|
|
6
|
+
type JSONValue = JSONPrimitive | JSONObject | JSONArray;
|
|
7
|
+
type PossibleRecursiveDiff<V extends JSONValue | undefined> = V extends JSONValue ? NestedDiff<V> : never;
|
|
8
|
+
type NestedDiff<V extends JSONValue> = (V extends JSONPrimitive | JSONArray ? {
|
|
9
|
+
type: "value";
|
|
10
|
+
before: V;
|
|
11
|
+
after: V;
|
|
12
|
+
} : never) | (V extends JSONObject ? {
|
|
13
|
+
type: "modified";
|
|
14
|
+
additions?: {
|
|
15
|
+
[K in keyof V]: V[K];
|
|
16
|
+
};
|
|
17
|
+
deletions?: {
|
|
18
|
+
[K in keyof V]: V[K];
|
|
19
|
+
};
|
|
20
|
+
changes?: {
|
|
21
|
+
[K in keyof V]: PossibleRecursiveDiff<V[K]>;
|
|
22
|
+
};
|
|
23
|
+
} : never);
|
|
24
|
+
type Diff<V extends JSONValue> = NestedDiff<V> | {
|
|
25
|
+
type: "match";
|
|
26
|
+
};
|
|
27
|
+
declare const diffJson: <V extends JSONValue>(a: V | undefined, b: V | undefined) => Diff<V>;
|
|
2
28
|
|
|
3
|
-
export { diffJson };
|
|
29
|
+
export { type Diff, type JSONArray, type JSONObject, type JSONPrimitive, type JSONValue, type NestedDiff, diffJson };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
type JSONPrimitive = string | number | boolean | null;
|
|
2
|
+
type JSONObject = {
|
|
3
|
+
[member: string]: JSONValue | undefined;
|
|
4
|
+
};
|
|
5
|
+
type JSONArray = JSONValue[];
|
|
6
|
+
type JSONValue = JSONPrimitive | JSONObject | JSONArray;
|
|
7
|
+
type PossibleRecursiveDiff<V extends JSONValue | undefined> = V extends JSONValue ? NestedDiff<V> : never;
|
|
8
|
+
type NestedDiff<V extends JSONValue> = (V extends JSONPrimitive | JSONArray ? {
|
|
9
|
+
type: "value";
|
|
10
|
+
before: V;
|
|
11
|
+
after: V;
|
|
12
|
+
} : never) | (V extends JSONObject ? {
|
|
13
|
+
type: "modified";
|
|
14
|
+
additions?: {
|
|
15
|
+
[K in keyof V]: V[K];
|
|
16
|
+
};
|
|
17
|
+
deletions?: {
|
|
18
|
+
[K in keyof V]: V[K];
|
|
19
|
+
};
|
|
20
|
+
changes?: {
|
|
21
|
+
[K in keyof V]: PossibleRecursiveDiff<V[K]>;
|
|
22
|
+
};
|
|
23
|
+
} : never);
|
|
24
|
+
type Diff<V extends JSONValue> = NestedDiff<V> | {
|
|
25
|
+
type: "match";
|
|
26
|
+
};
|
|
27
|
+
declare const diffJson: <V extends JSONValue>(a: V | undefined, b: V | undefined) => Diff<V>;
|
|
2
28
|
|
|
3
|
-
export { diffJson };
|
|
29
|
+
export { type Diff, type JSONArray, type JSONObject, type JSONPrimitive, type JSONValue, type NestedDiff, diffJson };
|
package/dist/index.js
CHANGED
|
@@ -23,8 +23,72 @@ __export(src_exports, {
|
|
|
23
23
|
diffJson: () => diffJson
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
|
|
27
|
+
// src/diff.ts
|
|
28
|
+
var jsonTypeMatches = (a, b) => typeof a === typeof b && Array.isArray(a) === Array.isArray(b) && a === null === (b === null);
|
|
29
|
+
var diffJson = (a, b) => {
|
|
30
|
+
if (a === b) {
|
|
31
|
+
return { type: "match" };
|
|
32
|
+
}
|
|
33
|
+
if (!jsonTypeMatches(a, b)) {
|
|
34
|
+
throw new Error("Types don't match, unable to diff");
|
|
35
|
+
}
|
|
36
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
37
|
+
let matching = true;
|
|
38
|
+
if (a.length === b.length) {
|
|
39
|
+
for (let i = 0; i < a.length; i++) {
|
|
40
|
+
const d = diffJson(a[i], b[i]);
|
|
41
|
+
if (d.type !== "match") {
|
|
42
|
+
matching = false;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
matching = false;
|
|
48
|
+
}
|
|
49
|
+
if (matching) {
|
|
50
|
+
return { type: "match" };
|
|
51
|
+
}
|
|
52
|
+
return { type: "value", before: a, after: b };
|
|
53
|
+
}
|
|
54
|
+
if (a === null || b === null) {
|
|
55
|
+
return { type: "value", before: a, after: b };
|
|
56
|
+
}
|
|
57
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
58
|
+
return { type: "value", before: a, after: b };
|
|
59
|
+
}
|
|
60
|
+
if (typeof a === "object" && typeof b === "object") {
|
|
61
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
62
|
+
const changes = {};
|
|
63
|
+
const additions = {};
|
|
64
|
+
const deletions = {};
|
|
65
|
+
for (const key of keys) {
|
|
66
|
+
const valueA = a[key];
|
|
67
|
+
const valueB = b[key];
|
|
68
|
+
if (valueA === void 0 && valueB !== void 0) {
|
|
69
|
+
additions[key] = valueB;
|
|
70
|
+
} else if (valueB === void 0 && valueA !== void 0) {
|
|
71
|
+
deletions[key] = valueA;
|
|
72
|
+
} else if (valueA !== void 0 && valueB !== void 0) {
|
|
73
|
+
const d = diffJson(valueA, valueB);
|
|
74
|
+
if (d.type !== "match") {
|
|
75
|
+
changes[key] = d;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const result = {
|
|
80
|
+
type: "modified",
|
|
81
|
+
...Object.keys(changes).length === 0 ? {} : { changes },
|
|
82
|
+
...Object.keys(additions).length === 0 ? {} : { additions },
|
|
83
|
+
...Object.keys(deletions).length === 0 ? {} : { deletions }
|
|
84
|
+
};
|
|
85
|
+
if (result.changes || result.additions || result.deletions) {
|
|
86
|
+
return result;
|
|
87
|
+
} else {
|
|
88
|
+
return { type: "match" };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return { type: "value", before: a, after: b };
|
|
28
92
|
};
|
|
29
93
|
// Annotate the CommonJS export names for ESM import in node:
|
|
30
94
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
var
|
|
3
|
-
|
|
1
|
+
// src/diff.ts
|
|
2
|
+
var jsonTypeMatches = (a, b) => typeof a === typeof b && Array.isArray(a) === Array.isArray(b) && a === null === (b === null);
|
|
3
|
+
var diffJson = (a, b) => {
|
|
4
|
+
if (a === b) {
|
|
5
|
+
return { type: "match" };
|
|
6
|
+
}
|
|
7
|
+
if (!jsonTypeMatches(a, b)) {
|
|
8
|
+
throw new Error("Types don't match, unable to diff");
|
|
9
|
+
}
|
|
10
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
11
|
+
let matching = true;
|
|
12
|
+
if (a.length === b.length) {
|
|
13
|
+
for (let i = 0; i < a.length; i++) {
|
|
14
|
+
const d = diffJson(a[i], b[i]);
|
|
15
|
+
if (d.type !== "match") {
|
|
16
|
+
matching = false;
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
matching = false;
|
|
22
|
+
}
|
|
23
|
+
if (matching) {
|
|
24
|
+
return { type: "match" };
|
|
25
|
+
}
|
|
26
|
+
return { type: "value", before: a, after: b };
|
|
27
|
+
}
|
|
28
|
+
if (a === null || b === null) {
|
|
29
|
+
return { type: "value", before: a, after: b };
|
|
30
|
+
}
|
|
31
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
32
|
+
return { type: "value", before: a, after: b };
|
|
33
|
+
}
|
|
34
|
+
if (typeof a === "object" && typeof b === "object") {
|
|
35
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
36
|
+
const changes = {};
|
|
37
|
+
const additions = {};
|
|
38
|
+
const deletions = {};
|
|
39
|
+
for (const key of keys) {
|
|
40
|
+
const valueA = a[key];
|
|
41
|
+
const valueB = b[key];
|
|
42
|
+
if (valueA === void 0 && valueB !== void 0) {
|
|
43
|
+
additions[key] = valueB;
|
|
44
|
+
} else if (valueB === void 0 && valueA !== void 0) {
|
|
45
|
+
deletions[key] = valueA;
|
|
46
|
+
} else if (valueA !== void 0 && valueB !== void 0) {
|
|
47
|
+
const d = diffJson(valueA, valueB);
|
|
48
|
+
if (d.type !== "match") {
|
|
49
|
+
changes[key] = d;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const result = {
|
|
54
|
+
type: "modified",
|
|
55
|
+
...Object.keys(changes).length === 0 ? {} : { changes },
|
|
56
|
+
...Object.keys(additions).length === 0 ? {} : { additions },
|
|
57
|
+
...Object.keys(deletions).length === 0 ? {} : { deletions }
|
|
58
|
+
};
|
|
59
|
+
if (result.changes || result.additions || result.deletions) {
|
|
60
|
+
return result;
|
|
61
|
+
} else {
|
|
62
|
+
return { type: "match" };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return { type: "value", before: a, after: b };
|
|
4
66
|
};
|
|
5
67
|
export {
|
|
6
68
|
diffJson
|