@arcanejs/diff 0.2.0 → 0.3.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/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # `@arcanejs/diff`
2
+
3
+ [![NPM Version](https://img.shields.io/npm/v/%40arcanejs%2Fdiff)](https://www.npmjs.com/package/@arcanejs/diff)
4
+
5
+ This package provides an easy way to:
6
+
7
+ - Create diffs by comparing objects
8
+ - Update objects by applying diffs
9
+
10
+ This library is written in TypeScript,
11
+ and produces diffs that are type-safe,
12
+ and can only be applied to objects that match the type
13
+ of the objects being compared.
14
+
15
+ This package is part of the
16
+ [`arcanejs` project](https://github.com/arcanejs/arcanejs#arcanejs),
17
+ and is used to maintain a copy of a JSON tree in downstream clients in real-time
18
+ via websockets.
19
+
20
+ ## Usage
21
+
22
+ ```ts
23
+ import { diffJson, Diff } from '@arcanejs/diff/diff';
24
+ import { patchJson } from '@arcanejs/diff/patch';
25
+
26
+ type E = {
27
+ foo: string;
28
+ bar?: number[];
29
+ };
30
+
31
+ const a: E = { foo: 'bar' };
32
+ const b: E = { foo: 'baz', bar: [1] };
33
+
34
+ const diffA: Diff<E> = diffJson(a, b);
35
+
36
+ const resultA = patchJson(a, diffA);
37
+
38
+ console.log(resultB); // { foo: 'baz', bar: [1] }
39
+
40
+ const c = { baz: 'foo' };
41
+
42
+ const resultB = patchJson(c, diffA); // TypeScript Type Error: Property 'baz' is missing in type '{ foo: string; bar?: number[] | undefined; }' but required in type '{ baz: string; }'
43
+ ```
@@ -0,0 +1,52 @@
1
+ // src/patch.ts
2
+ var patchJson = (old, diff) => {
3
+ if (diff.type === "match") {
4
+ return old;
5
+ }
6
+ if (diff.type === "value") {
7
+ return diff.after;
8
+ }
9
+ if (diff.type === "splice") {
10
+ if (!Array.isArray(old)) {
11
+ throw new Error("Cannot apply splice diff to non-array value");
12
+ }
13
+ const result = [...old];
14
+ result.splice(diff.start, diff.count, ...diff.items);
15
+ return result;
16
+ }
17
+ if (diff.type === "modified-a") {
18
+ if (!Array.isArray(old)) {
19
+ throw new Error("Cannot apply changes diff to non-array value");
20
+ }
21
+ const result = [...old];
22
+ for (const { i, diff: d } of diff.changes) {
23
+ result[i] = patchJson(old[i], d);
24
+ }
25
+ return result;
26
+ }
27
+ if (Array.isArray(old)) {
28
+ throw new Error("Unexpected array type");
29
+ }
30
+ if (diff.type === "modified-o") {
31
+ if (typeof old !== "object" || old === null) {
32
+ throw new Error("Cannot apply modified diff to non-object value");
33
+ }
34
+ const result = { ...old, ...diff.additions };
35
+ for (const key of Object.keys(diff.changes || {})) {
36
+ const changes = diff.changes?.[key];
37
+ if (changes) {
38
+ result[key] = patchJson(old[key], changes);
39
+ }
40
+ }
41
+ for (const keu of Object.keys(diff.deletions || {})) {
42
+ delete result[keu];
43
+ }
44
+ return result;
45
+ }
46
+ const _n = diff;
47
+ throw new Error(`Unknown diff type: ${_n}`);
48
+ };
49
+
50
+ export {
51
+ patchJson
52
+ };
@@ -0,0 +1,81 @@
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
+ if (a.length === b.length) {
12
+ const changes = [];
13
+ for (let i = 0; i < a.length; i++) {
14
+ const d = diffJson(a[i], b[i]);
15
+ if (d.type !== "match") {
16
+ changes.push({ i, diff: d });
17
+ }
18
+ }
19
+ if (changes.length === 0) {
20
+ return { type: "match" };
21
+ }
22
+ return { type: "modified-a", changes };
23
+ } else {
24
+ let start = 0;
25
+ while (start < a.length && start < b.length && diffJson(a[start], b[start]).type === "match") {
26
+ start++;
27
+ }
28
+ let endIndexA = a.length - 1;
29
+ let endIndexB = b.length - 1;
30
+ while (endIndexA >= start && endIndexB >= start && diffJson(a[endIndexA], b[endIndexB]).type === "match") {
31
+ endIndexA--;
32
+ endIndexB--;
33
+ }
34
+ const count = endIndexA - start + 1;
35
+ const items = b.slice(start, endIndexB + 1);
36
+ return { type: "splice", start, count, items };
37
+ }
38
+ }
39
+ if (a === null || b === null) {
40
+ return { type: "value", before: a, after: b };
41
+ }
42
+ if (Array.isArray(a) || Array.isArray(b)) {
43
+ return { type: "value", before: a, after: b };
44
+ }
45
+ if (typeof a === "object" && typeof b === "object") {
46
+ const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
47
+ const changes = {};
48
+ const additions = {};
49
+ const deletions = {};
50
+ for (const key of keys) {
51
+ const valueA = a[key];
52
+ const valueB = b[key];
53
+ if (valueA === void 0 && valueB !== void 0) {
54
+ additions[key] = valueB;
55
+ } else if (valueB === void 0 && valueA !== void 0) {
56
+ deletions[key] = valueA;
57
+ } else if (valueA !== void 0 && valueB !== void 0) {
58
+ const d = diffJson(valueA, valueB);
59
+ if (d.type !== "match") {
60
+ changes[key] = d;
61
+ }
62
+ }
63
+ }
64
+ const result = {
65
+ type: "modified-o",
66
+ ...Object.keys(changes).length === 0 ? {} : { changes },
67
+ ...Object.keys(additions).length === 0 ? {} : { additions },
68
+ ...Object.keys(deletions).length === 0 ? {} : { deletions }
69
+ };
70
+ if (result.changes || result.additions || result.deletions) {
71
+ return result;
72
+ } else {
73
+ return { type: "match" };
74
+ }
75
+ }
76
+ return { type: "value", before: a, after: b };
77
+ };
78
+
79
+ export {
80
+ diffJson
81
+ };
@@ -0,0 +1,40 @@
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 ? {
9
+ type: 'value';
10
+ before: V;
11
+ after: V;
12
+ } : never) | (V extends JSONArray ? {
13
+ type: 'splice';
14
+ start: number;
15
+ count: number;
16
+ items: V;
17
+ } : never) | (V extends JSONArray ? {
18
+ type: 'modified-a';
19
+ changes: Array<{
20
+ i: number;
21
+ diff: PossibleRecursiveDiff<V[number]>;
22
+ }>;
23
+ } : never) | (V extends JSONObject ? {
24
+ type: 'modified-o';
25
+ additions?: {
26
+ [K in keyof V]: V[K];
27
+ };
28
+ deletions?: {
29
+ [K in keyof V]: V[K];
30
+ };
31
+ changes?: {
32
+ [K in keyof V]: PossibleRecursiveDiff<V[K]>;
33
+ };
34
+ } : never);
35
+ type Diff<V extends JSONValue> = NestedDiff<V> | {
36
+ type: 'match';
37
+ };
38
+ declare const diffJson: <V extends JSONValue>(a: V | undefined, b: V | undefined) => Diff<V>;
39
+
40
+ export { type Diff, type JSONArray, type JSONObject, type JSONPrimitive, type JSONValue, type NestedDiff, diffJson };
package/dist/diff.d.ts ADDED
@@ -0,0 +1,40 @@
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 ? {
9
+ type: 'value';
10
+ before: V;
11
+ after: V;
12
+ } : never) | (V extends JSONArray ? {
13
+ type: 'splice';
14
+ start: number;
15
+ count: number;
16
+ items: V;
17
+ } : never) | (V extends JSONArray ? {
18
+ type: 'modified-a';
19
+ changes: Array<{
20
+ i: number;
21
+ diff: PossibleRecursiveDiff<V[number]>;
22
+ }>;
23
+ } : never) | (V extends JSONObject ? {
24
+ type: 'modified-o';
25
+ additions?: {
26
+ [K in keyof V]: V[K];
27
+ };
28
+ deletions?: {
29
+ [K in keyof V]: V[K];
30
+ };
31
+ changes?: {
32
+ [K in keyof V]: PossibleRecursiveDiff<V[K]>;
33
+ };
34
+ } : never);
35
+ type Diff<V extends JSONValue> = NestedDiff<V> | {
36
+ type: 'match';
37
+ };
38
+ declare const diffJson: <V extends JSONValue>(a: V | undefined, b: V | undefined) => Diff<V>;
39
+
40
+ export { type Diff, type JSONArray, type JSONObject, type JSONPrimitive, type JSONValue, type NestedDiff, diffJson };
package/dist/diff.js ADDED
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/diff.ts
21
+ var diff_exports = {};
22
+ __export(diff_exports, {
23
+ diffJson: () => diffJson
24
+ });
25
+ module.exports = __toCommonJS(diff_exports);
26
+ var jsonTypeMatches = (a, b) => typeof a === typeof b && Array.isArray(a) === Array.isArray(b) && a === null === (b === null);
27
+ var diffJson = (a, b) => {
28
+ if (a === b) {
29
+ return { type: "match" };
30
+ }
31
+ if (!jsonTypeMatches(a, b)) {
32
+ throw new Error("Types don't match, unable to diff");
33
+ }
34
+ if (Array.isArray(a) && Array.isArray(b)) {
35
+ if (a.length === b.length) {
36
+ const changes = [];
37
+ for (let i = 0; i < a.length; i++) {
38
+ const d = diffJson(a[i], b[i]);
39
+ if (d.type !== "match") {
40
+ changes.push({ i, diff: d });
41
+ }
42
+ }
43
+ if (changes.length === 0) {
44
+ return { type: "match" };
45
+ }
46
+ return { type: "modified-a", changes };
47
+ } else {
48
+ let start = 0;
49
+ while (start < a.length && start < b.length && diffJson(a[start], b[start]).type === "match") {
50
+ start++;
51
+ }
52
+ let endIndexA = a.length - 1;
53
+ let endIndexB = b.length - 1;
54
+ while (endIndexA >= start && endIndexB >= start && diffJson(a[endIndexA], b[endIndexB]).type === "match") {
55
+ endIndexA--;
56
+ endIndexB--;
57
+ }
58
+ const count = endIndexA - start + 1;
59
+ const items = b.slice(start, endIndexB + 1);
60
+ return { type: "splice", start, count, items };
61
+ }
62
+ }
63
+ if (a === null || b === null) {
64
+ return { type: "value", before: a, after: b };
65
+ }
66
+ if (Array.isArray(a) || Array.isArray(b)) {
67
+ return { type: "value", before: a, after: b };
68
+ }
69
+ if (typeof a === "object" && typeof b === "object") {
70
+ const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
71
+ const changes = {};
72
+ const additions = {};
73
+ const deletions = {};
74
+ for (const key of keys) {
75
+ const valueA = a[key];
76
+ const valueB = b[key];
77
+ if (valueA === void 0 && valueB !== void 0) {
78
+ additions[key] = valueB;
79
+ } else if (valueB === void 0 && valueA !== void 0) {
80
+ deletions[key] = valueA;
81
+ } else if (valueA !== void 0 && valueB !== void 0) {
82
+ const d = diffJson(valueA, valueB);
83
+ if (d.type !== "match") {
84
+ changes[key] = d;
85
+ }
86
+ }
87
+ }
88
+ const result = {
89
+ type: "modified-o",
90
+ ...Object.keys(changes).length === 0 ? {} : { changes },
91
+ ...Object.keys(additions).length === 0 ? {} : { additions },
92
+ ...Object.keys(deletions).length === 0 ? {} : { deletions }
93
+ };
94
+ if (result.changes || result.additions || result.deletions) {
95
+ return result;
96
+ } else {
97
+ return { type: "match" };
98
+ }
99
+ }
100
+ return { type: "value", before: a, after: b };
101
+ };
102
+ // Annotate the CommonJS export names for ESM import in node:
103
+ 0 && (module.exports = {
104
+ diffJson
105
+ });
package/dist/diff.mjs ADDED
@@ -0,0 +1,6 @@
1
+ import {
2
+ diffJson
3
+ } from "./chunk-SZTK7XV4.mjs";
4
+ export {
5
+ diffJson
6
+ };
package/dist/index.d.mts CHANGED
@@ -1,29 +1,2 @@
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>;
28
-
29
- export { type Diff, type JSONArray, type JSONObject, type JSONPrimitive, type JSONValue, type NestedDiff, diffJson };
1
+ export { Diff, JSONArray, JSONObject, JSONPrimitive, JSONValue, NestedDiff, diffJson } from './diff.mjs';
2
+ export { patchJson } from './patch.mjs';
package/dist/index.d.ts CHANGED
@@ -1,29 +1,2 @@
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>;
28
-
29
- export { type Diff, type JSONArray, type JSONObject, type JSONPrimitive, type JSONValue, type NestedDiff, diffJson };
1
+ export { Diff, JSONArray, JSONObject, JSONPrimitive, JSONValue, NestedDiff, diffJson } from './diff.js';
2
+ export { patchJson } from './patch.js';
package/dist/index.js CHANGED
@@ -20,7 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
- diffJson: () => diffJson
23
+ diffJson: () => diffJson,
24
+ patchJson: () => patchJson
24
25
  });
25
26
  module.exports = __toCommonJS(src_exports);
26
27
 
@@ -34,22 +35,33 @@ var diffJson = (a, b) => {
34
35
  throw new Error("Types don't match, unable to diff");
35
36
  }
36
37
  if (Array.isArray(a) && Array.isArray(b)) {
37
- let matching = true;
38
38
  if (a.length === b.length) {
39
+ const changes = [];
39
40
  for (let i = 0; i < a.length; i++) {
40
41
  const d = diffJson(a[i], b[i]);
41
42
  if (d.type !== "match") {
42
- matching = false;
43
- break;
43
+ changes.push({ i, diff: d });
44
44
  }
45
45
  }
46
+ if (changes.length === 0) {
47
+ return { type: "match" };
48
+ }
49
+ return { type: "modified-a", changes };
46
50
  } else {
47
- matching = false;
48
- }
49
- if (matching) {
50
- return { type: "match" };
51
+ let start = 0;
52
+ while (start < a.length && start < b.length && diffJson(a[start], b[start]).type === "match") {
53
+ start++;
54
+ }
55
+ let endIndexA = a.length - 1;
56
+ let endIndexB = b.length - 1;
57
+ while (endIndexA >= start && endIndexB >= start && diffJson(a[endIndexA], b[endIndexB]).type === "match") {
58
+ endIndexA--;
59
+ endIndexB--;
60
+ }
61
+ const count = endIndexA - start + 1;
62
+ const items = b.slice(start, endIndexB + 1);
63
+ return { type: "splice", start, count, items };
51
64
  }
52
- return { type: "value", before: a, after: b };
53
65
  }
54
66
  if (a === null || b === null) {
55
67
  return { type: "value", before: a, after: b };
@@ -77,7 +89,7 @@ var diffJson = (a, b) => {
77
89
  }
78
90
  }
79
91
  const result = {
80
- type: "modified",
92
+ type: "modified-o",
81
93
  ...Object.keys(changes).length === 0 ? {} : { changes },
82
94
  ...Object.keys(additions).length === 0 ? {} : { additions },
83
95
  ...Object.keys(deletions).length === 0 ? {} : { deletions }
@@ -90,7 +102,57 @@ var diffJson = (a, b) => {
90
102
  }
91
103
  return { type: "value", before: a, after: b };
92
104
  };
105
+
106
+ // src/patch.ts
107
+ var patchJson = (old, diff) => {
108
+ if (diff.type === "match") {
109
+ return old;
110
+ }
111
+ if (diff.type === "value") {
112
+ return diff.after;
113
+ }
114
+ if (diff.type === "splice") {
115
+ if (!Array.isArray(old)) {
116
+ throw new Error("Cannot apply splice diff to non-array value");
117
+ }
118
+ const result = [...old];
119
+ result.splice(diff.start, diff.count, ...diff.items);
120
+ return result;
121
+ }
122
+ if (diff.type === "modified-a") {
123
+ if (!Array.isArray(old)) {
124
+ throw new Error("Cannot apply changes diff to non-array value");
125
+ }
126
+ const result = [...old];
127
+ for (const { i, diff: d } of diff.changes) {
128
+ result[i] = patchJson(old[i], d);
129
+ }
130
+ return result;
131
+ }
132
+ if (Array.isArray(old)) {
133
+ throw new Error("Unexpected array type");
134
+ }
135
+ if (diff.type === "modified-o") {
136
+ if (typeof old !== "object" || old === null) {
137
+ throw new Error("Cannot apply modified diff to non-object value");
138
+ }
139
+ const result = { ...old, ...diff.additions };
140
+ for (const key of Object.keys(diff.changes || {})) {
141
+ const changes = diff.changes?.[key];
142
+ if (changes) {
143
+ result[key] = patchJson(old[key], changes);
144
+ }
145
+ }
146
+ for (const keu of Object.keys(diff.deletions || {})) {
147
+ delete result[keu];
148
+ }
149
+ return result;
150
+ }
151
+ const _n = diff;
152
+ throw new Error(`Unknown diff type: ${_n}`);
153
+ };
93
154
  // Annotate the CommonJS export names for ESM import in node:
94
155
  0 && (module.exports = {
95
- diffJson
156
+ diffJson,
157
+ patchJson
96
158
  });
package/dist/index.mjs CHANGED
@@ -1,69 +1,10 @@
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 };
66
- };
67
- export {
1
+ import {
68
2
  diffJson
3
+ } from "./chunk-SZTK7XV4.mjs";
4
+ import {
5
+ patchJson
6
+ } from "./chunk-KAHL2YAL.mjs";
7
+ export {
8
+ diffJson,
9
+ patchJson
69
10
  };
@@ -0,0 +1,5 @@
1
+ import { JSONValue, Diff } from './diff.mjs';
2
+
3
+ declare const patchJson: <V extends JSONValue>(old: V | undefined, diff: Diff<V>) => V | undefined;
4
+
5
+ export { patchJson };
@@ -0,0 +1,5 @@
1
+ import { JSONValue, Diff } from './diff.js';
2
+
3
+ declare const patchJson: <V extends JSONValue>(old: V | undefined, diff: Diff<V>) => V | undefined;
4
+
5
+ export { patchJson };
package/dist/patch.js ADDED
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/patch.ts
21
+ var patch_exports = {};
22
+ __export(patch_exports, {
23
+ patchJson: () => patchJson
24
+ });
25
+ module.exports = __toCommonJS(patch_exports);
26
+ var patchJson = (old, diff) => {
27
+ if (diff.type === "match") {
28
+ return old;
29
+ }
30
+ if (diff.type === "value") {
31
+ return diff.after;
32
+ }
33
+ if (diff.type === "splice") {
34
+ if (!Array.isArray(old)) {
35
+ throw new Error("Cannot apply splice diff to non-array value");
36
+ }
37
+ const result = [...old];
38
+ result.splice(diff.start, diff.count, ...diff.items);
39
+ return result;
40
+ }
41
+ if (diff.type === "modified-a") {
42
+ if (!Array.isArray(old)) {
43
+ throw new Error("Cannot apply changes diff to non-array value");
44
+ }
45
+ const result = [...old];
46
+ for (const { i, diff: d } of diff.changes) {
47
+ result[i] = patchJson(old[i], d);
48
+ }
49
+ return result;
50
+ }
51
+ if (Array.isArray(old)) {
52
+ throw new Error("Unexpected array type");
53
+ }
54
+ if (diff.type === "modified-o") {
55
+ if (typeof old !== "object" || old === null) {
56
+ throw new Error("Cannot apply modified diff to non-object value");
57
+ }
58
+ const result = { ...old, ...diff.additions };
59
+ for (const key of Object.keys(diff.changes || {})) {
60
+ const changes = diff.changes?.[key];
61
+ if (changes) {
62
+ result[key] = patchJson(old[key], changes);
63
+ }
64
+ }
65
+ for (const keu of Object.keys(diff.deletions || {})) {
66
+ delete result[keu];
67
+ }
68
+ return result;
69
+ }
70
+ const _n = diff;
71
+ throw new Error(`Unknown diff type: ${_n}`);
72
+ };
73
+ // Annotate the CommonJS export names for ESM import in node:
74
+ 0 && (module.exports = {
75
+ patchJson
76
+ });
package/dist/patch.mjs ADDED
@@ -0,0 +1,6 @@
1
+ import {
2
+ patchJson
3
+ } from "./chunk-KAHL2YAL.mjs";
4
+ export {
5
+ patchJson
6
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcanejs/diff",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "private": false,
5
5
  "description": "Create and apply JSON diffs of JSON objects",
6
6
  "keywords": [
@@ -22,19 +22,35 @@
22
22
  },
23
23
  "exports": {
24
24
  ".": {
25
+ "@arcanejs/source": "./src/index.ts",
25
26
  "import": "./dist/index.mjs",
26
27
  "require": "./dist/index.js",
27
28
  "types": "./dist/index.d.ts"
29
+ },
30
+ "./diff": {
31
+ "@arcanejs/source": "./src/diff.ts",
32
+ "import": "./dist/diff.mjs",
33
+ "require": "./dist/diff.js",
34
+ "types": "./dist/diff.d.ts"
35
+ },
36
+ "./patch": {
37
+ "@arcanejs/source": "./src/patch.ts",
38
+ "import": "./dist/patch.mjs",
39
+ "require": "./dist/patch.js",
40
+ "types": "./dist/patch.d.ts"
28
41
  }
29
42
  },
30
43
  "devDependencies": {
31
44
  "@types/eslint": "^8.56.5",
45
+ "@types/jest": "^29.5.12",
32
46
  "@types/node": "^20.11.24",
33
47
  "eslint": "^8.57.0",
48
+ "jest": "^29.7.0",
49
+ "ts-jest": "^29.2.0",
34
50
  "tsup": "^8.1.0",
35
51
  "typescript": "^5.3.3",
36
- "@arcanejs/eslint-config": "0.0.0",
37
- "@arcanejs/typescript-config": "0.0.0"
52
+ "@arcanejs/eslint-config": "^0.0.0",
53
+ "@arcanejs/typescript-config": "^0.0.0"
38
54
  },
39
55
  "files": [
40
56
  "dist"
@@ -45,6 +61,7 @@
45
61
  "scripts": {
46
62
  "lint": "eslint . --max-warnings 0",
47
63
  "build": "tsup",
48
- "dev": "tsup --watch"
64
+ "test": "jest",
65
+ "test:watch": "jest --watch"
49
66
  }
50
67
  }