@arcanejs/diff 0.3.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 +43 -0
- package/dist/diff.d.mts +5 -5
- package/dist/diff.d.ts +5 -5
- package/package.json +6 -4
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# `@arcanejs/diff`
|
|
2
|
+
|
|
3
|
+
[](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
|
+
```
|
package/dist/diff.d.mts
CHANGED
|
@@ -6,22 +6,22 @@ type JSONArray = JSONValue[];
|
|
|
6
6
|
type JSONValue = JSONPrimitive | JSONObject | JSONArray;
|
|
7
7
|
type PossibleRecursiveDiff<V extends JSONValue | undefined> = V extends JSONValue ? NestedDiff<V> : never;
|
|
8
8
|
type NestedDiff<V extends JSONValue> = (V extends JSONPrimitive ? {
|
|
9
|
-
type:
|
|
9
|
+
type: 'value';
|
|
10
10
|
before: V;
|
|
11
11
|
after: V;
|
|
12
12
|
} : never) | (V extends JSONArray ? {
|
|
13
|
-
type:
|
|
13
|
+
type: 'splice';
|
|
14
14
|
start: number;
|
|
15
15
|
count: number;
|
|
16
16
|
items: V;
|
|
17
17
|
} : never) | (V extends JSONArray ? {
|
|
18
|
-
type:
|
|
18
|
+
type: 'modified-a';
|
|
19
19
|
changes: Array<{
|
|
20
20
|
i: number;
|
|
21
21
|
diff: PossibleRecursiveDiff<V[number]>;
|
|
22
22
|
}>;
|
|
23
23
|
} : never) | (V extends JSONObject ? {
|
|
24
|
-
type:
|
|
24
|
+
type: 'modified-o';
|
|
25
25
|
additions?: {
|
|
26
26
|
[K in keyof V]: V[K];
|
|
27
27
|
};
|
|
@@ -33,7 +33,7 @@ type NestedDiff<V extends JSONValue> = (V extends JSONPrimitive ? {
|
|
|
33
33
|
};
|
|
34
34
|
} : never);
|
|
35
35
|
type Diff<V extends JSONValue> = NestedDiff<V> | {
|
|
36
|
-
type:
|
|
36
|
+
type: 'match';
|
|
37
37
|
};
|
|
38
38
|
declare const diffJson: <V extends JSONValue>(a: V | undefined, b: V | undefined) => Diff<V>;
|
|
39
39
|
|
package/dist/diff.d.ts
CHANGED
|
@@ -6,22 +6,22 @@ type JSONArray = JSONValue[];
|
|
|
6
6
|
type JSONValue = JSONPrimitive | JSONObject | JSONArray;
|
|
7
7
|
type PossibleRecursiveDiff<V extends JSONValue | undefined> = V extends JSONValue ? NestedDiff<V> : never;
|
|
8
8
|
type NestedDiff<V extends JSONValue> = (V extends JSONPrimitive ? {
|
|
9
|
-
type:
|
|
9
|
+
type: 'value';
|
|
10
10
|
before: V;
|
|
11
11
|
after: V;
|
|
12
12
|
} : never) | (V extends JSONArray ? {
|
|
13
|
-
type:
|
|
13
|
+
type: 'splice';
|
|
14
14
|
start: number;
|
|
15
15
|
count: number;
|
|
16
16
|
items: V;
|
|
17
17
|
} : never) | (V extends JSONArray ? {
|
|
18
|
-
type:
|
|
18
|
+
type: 'modified-a';
|
|
19
19
|
changes: Array<{
|
|
20
20
|
i: number;
|
|
21
21
|
diff: PossibleRecursiveDiff<V[number]>;
|
|
22
22
|
}>;
|
|
23
23
|
} : never) | (V extends JSONObject ? {
|
|
24
|
-
type:
|
|
24
|
+
type: 'modified-o';
|
|
25
25
|
additions?: {
|
|
26
26
|
[K in keyof V]: V[K];
|
|
27
27
|
};
|
|
@@ -33,7 +33,7 @@ type NestedDiff<V extends JSONValue> = (V extends JSONPrimitive ? {
|
|
|
33
33
|
};
|
|
34
34
|
} : never);
|
|
35
35
|
type Diff<V extends JSONValue> = NestedDiff<V> | {
|
|
36
|
-
type:
|
|
36
|
+
type: 'match';
|
|
37
37
|
};
|
|
38
38
|
declare const diffJson: <V extends JSONValue>(a: V | undefined, b: V | undefined) => Diff<V>;
|
|
39
39
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcanejs/diff",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Create and apply JSON diffs of JSON objects",
|
|
6
6
|
"keywords": [
|
|
@@ -22,16 +22,19 @@
|
|
|
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"
|
|
28
29
|
},
|
|
29
30
|
"./diff": {
|
|
31
|
+
"@arcanejs/source": "./src/diff.ts",
|
|
30
32
|
"import": "./dist/diff.mjs",
|
|
31
33
|
"require": "./dist/diff.js",
|
|
32
34
|
"types": "./dist/diff.d.ts"
|
|
33
35
|
},
|
|
34
36
|
"./patch": {
|
|
37
|
+
"@arcanejs/source": "./src/patch.ts",
|
|
35
38
|
"import": "./dist/patch.mjs",
|
|
36
39
|
"require": "./dist/patch.js",
|
|
37
40
|
"types": "./dist/patch.d.ts"
|
|
@@ -46,8 +49,8 @@
|
|
|
46
49
|
"ts-jest": "^29.2.0",
|
|
47
50
|
"tsup": "^8.1.0",
|
|
48
51
|
"typescript": "^5.3.3",
|
|
49
|
-
"@arcanejs/eslint-config": "0.0.0",
|
|
50
|
-
"@arcanejs/typescript-config": "0.0.0"
|
|
52
|
+
"@arcanejs/eslint-config": "^0.0.0",
|
|
53
|
+
"@arcanejs/typescript-config": "^0.0.0"
|
|
51
54
|
},
|
|
52
55
|
"files": [
|
|
53
56
|
"dist"
|
|
@@ -58,7 +61,6 @@
|
|
|
58
61
|
"scripts": {
|
|
59
62
|
"lint": "eslint . --max-warnings 0",
|
|
60
63
|
"build": "tsup",
|
|
61
|
-
"dev": "tsup --watch",
|
|
62
64
|
"test": "jest",
|
|
63
65
|
"test:watch": "jest --watch"
|
|
64
66
|
}
|