@sinclair/typebox 0.25.3 → 0.25.4
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/package.json +1 -1
- package/readme.md +3 -3
- package/value/delta.d.ts +39 -18
- package/value/delta.js +42 -14
- package/value/value.d.ts +2 -2
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -832,7 +832,7 @@ const R = Value.Equal( // const R = true
|
|
|
832
832
|
Use the Diff function to produce a sequence of edits to transform one value into another.
|
|
833
833
|
|
|
834
834
|
```typescript
|
|
835
|
-
const E = Value.Diff
|
|
835
|
+
const E = Value.Diff( // const E = [
|
|
836
836
|
{ x: 1, y: 2, z: 3 }, // { type: 'update', path: '/y', value: 4 },
|
|
837
837
|
{ y: 4, z: 5, w: 6 } // { type: 'update', path: '/z', value: 5 },
|
|
838
838
|
) // { type: 'insert', path: '/w', value: 6 },
|
|
@@ -851,12 +851,12 @@ const A = { x: 1, y: 2 }
|
|
|
851
851
|
|
|
852
852
|
const B = { x: 3 }
|
|
853
853
|
|
|
854
|
-
const E = Value.Diff
|
|
854
|
+
const E = Value.Diff(A, B) // const E = [
|
|
855
855
|
// { type: 'update', path: '/x', value: 3 },
|
|
856
856
|
// { type: 'delete', path: '/y' }
|
|
857
857
|
// ]
|
|
858
858
|
|
|
859
|
-
const C = Value.Patch<
|
|
859
|
+
const C = Value.Patch<typeof B>(A, E) // const C = { x: 3 }
|
|
860
860
|
```
|
|
861
861
|
|
|
862
862
|
|
package/value/delta.d.ts
CHANGED
|
@@ -1,22 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
type:
|
|
5
|
-
path: string
|
|
6
|
-
value:
|
|
1
|
+
import { Static } from '../typebox';
|
|
2
|
+
export declare type Insert = Static<typeof Insert>;
|
|
3
|
+
export declare const Insert: import("../typebox").TObject<{
|
|
4
|
+
type: import("../typebox").TLiteral<"insert">;
|
|
5
|
+
path: import("../typebox").TString<string>;
|
|
6
|
+
value: import("../typebox").TUnknown;
|
|
7
|
+
}>;
|
|
8
|
+
export declare type Update = Static<typeof Update>;
|
|
9
|
+
export declare const Update: import("../typebox").TObject<{
|
|
10
|
+
type: import("../typebox").TLiteral<"update">;
|
|
11
|
+
path: import("../typebox").TString<string>;
|
|
12
|
+
value: import("../typebox").TUnknown;
|
|
13
|
+
}>;
|
|
14
|
+
export declare type Delete = Static<typeof Delete>;
|
|
15
|
+
export declare const Delete: import("../typebox").TObject<{
|
|
16
|
+
type: import("../typebox").TLiteral<"delete">;
|
|
17
|
+
path: import("../typebox").TString<string>;
|
|
18
|
+
}>;
|
|
19
|
+
export declare type Edit = Static<typeof Edit>;
|
|
20
|
+
export declare const Edit: import("../typebox").TUnion<[import("../typebox").TObject<{
|
|
21
|
+
type: import("../typebox").TLiteral<"insert">;
|
|
22
|
+
path: import("../typebox").TString<string>;
|
|
23
|
+
value: import("../typebox").TUnknown;
|
|
24
|
+
}>, import("../typebox").TObject<{
|
|
25
|
+
type: import("../typebox").TLiteral<"update">;
|
|
26
|
+
path: import("../typebox").TString<string>;
|
|
27
|
+
value: import("../typebox").TUnknown;
|
|
28
|
+
}>, import("../typebox").TObject<{
|
|
29
|
+
type: import("../typebox").TLiteral<"delete">;
|
|
30
|
+
path: import("../typebox").TString<string>;
|
|
31
|
+
}>]>;
|
|
32
|
+
export declare class ValueDeltaObjectWithSymbolKeyError extends Error {
|
|
33
|
+
readonly key: unknown;
|
|
34
|
+
constructor(key: unknown);
|
|
7
35
|
}
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
path: string;
|
|
12
|
-
value: any;
|
|
13
|
-
}
|
|
14
|
-
export interface Delete<T> {
|
|
15
|
-
brand: T;
|
|
16
|
-
type: 'delete';
|
|
17
|
-
path: string;
|
|
36
|
+
export declare class ValueDeltaUnableToDiffUnknownValue extends Error {
|
|
37
|
+
readonly value: unknown;
|
|
38
|
+
constructor(value: unknown);
|
|
18
39
|
}
|
|
19
40
|
export declare namespace ValueDelta {
|
|
20
|
-
function Diff
|
|
21
|
-
function Patch<T>(current:
|
|
41
|
+
function Diff(current: unknown, next: unknown): Edit[];
|
|
42
|
+
function Patch<T = any>(current: unknown, edits: Edit[]): T;
|
|
22
43
|
}
|
package/value/delta.js
CHANGED
|
@@ -27,10 +27,46 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueDelta = void 0;
|
|
30
|
+
exports.ValueDelta = exports.ValueDeltaUnableToDiffUnknownValue = exports.ValueDeltaObjectWithSymbolKeyError = exports.Edit = exports.Delete = exports.Update = exports.Insert = void 0;
|
|
31
|
+
const typebox_1 = require("../typebox");
|
|
31
32
|
const is_1 = require("./is");
|
|
32
33
|
const clone_1 = require("./clone");
|
|
33
34
|
const pointer_1 = require("./pointer");
|
|
35
|
+
exports.Insert = typebox_1.Type.Object({
|
|
36
|
+
type: typebox_1.Type.Literal('insert'),
|
|
37
|
+
path: typebox_1.Type.String(),
|
|
38
|
+
value: typebox_1.Type.Unknown(),
|
|
39
|
+
});
|
|
40
|
+
exports.Update = typebox_1.Type.Object({
|
|
41
|
+
type: typebox_1.Type.Literal('update'),
|
|
42
|
+
path: typebox_1.Type.String(),
|
|
43
|
+
value: typebox_1.Type.Unknown(),
|
|
44
|
+
});
|
|
45
|
+
exports.Delete = typebox_1.Type.Object({
|
|
46
|
+
type: typebox_1.Type.Literal('delete'),
|
|
47
|
+
path: typebox_1.Type.String(),
|
|
48
|
+
});
|
|
49
|
+
exports.Edit = typebox_1.Type.Union([exports.Insert, exports.Update, exports.Delete]);
|
|
50
|
+
// ---------------------------------------------------------------------
|
|
51
|
+
// Errors
|
|
52
|
+
// ---------------------------------------------------------------------
|
|
53
|
+
class ValueDeltaObjectWithSymbolKeyError extends Error {
|
|
54
|
+
constructor(key) {
|
|
55
|
+
super('ValueDelta: Cannot diff objects with symbol keys');
|
|
56
|
+
this.key = key;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.ValueDeltaObjectWithSymbolKeyError = ValueDeltaObjectWithSymbolKeyError;
|
|
60
|
+
class ValueDeltaUnableToDiffUnknownValue extends Error {
|
|
61
|
+
constructor(value) {
|
|
62
|
+
super('ValueDelta: Unable to create diff edits for unknown value');
|
|
63
|
+
this.value = value;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.ValueDeltaUnableToDiffUnknownValue = ValueDeltaUnableToDiffUnknownValue;
|
|
67
|
+
// ---------------------------------------------------------------------
|
|
68
|
+
// ValueDelta
|
|
69
|
+
// ---------------------------------------------------------------------
|
|
34
70
|
var ValueDelta;
|
|
35
71
|
(function (ValueDelta) {
|
|
36
72
|
// ---------------------------------------------------------------------
|
|
@@ -55,7 +91,7 @@ var ValueDelta;
|
|
|
55
91
|
const nextKeys = [...globalThis.Object.keys(next), ...globalThis.Object.getOwnPropertySymbols(next)];
|
|
56
92
|
for (const key of currentKeys) {
|
|
57
93
|
if (typeof key === 'symbol')
|
|
58
|
-
throw
|
|
94
|
+
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
59
95
|
if (next[key] === undefined && nextKeys.includes(key))
|
|
60
96
|
yield Update(`${path}/${String(key)}`, undefined);
|
|
61
97
|
}
|
|
@@ -63,27 +99,22 @@ var ValueDelta;
|
|
|
63
99
|
if (current[key] === undefined || next[key] === undefined)
|
|
64
100
|
continue;
|
|
65
101
|
if (typeof key === 'symbol')
|
|
66
|
-
throw
|
|
102
|
+
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
67
103
|
yield* Visit(`${path}/${String(key)}`, current[key], next[key]);
|
|
68
104
|
}
|
|
69
105
|
for (const key of nextKeys) {
|
|
70
106
|
if (typeof key === 'symbol')
|
|
71
|
-
throw
|
|
107
|
+
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
72
108
|
if (current[key] === undefined)
|
|
73
109
|
yield Insert(`${path}/${String(key)}`, next[key]);
|
|
74
110
|
}
|
|
75
111
|
for (const key of currentKeys.reverse()) {
|
|
76
112
|
if (typeof key === 'symbol')
|
|
77
|
-
throw
|
|
113
|
+
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
78
114
|
if (next[key] === undefined && !nextKeys.includes(key))
|
|
79
115
|
yield Delete(`${path}/${String(key)}`);
|
|
80
116
|
}
|
|
81
117
|
}
|
|
82
|
-
function* Date(path, current, next) {
|
|
83
|
-
if (is_1.Is.Date(next) && current.getTime() === next.getTime())
|
|
84
|
-
return;
|
|
85
|
-
yield Update(path, next);
|
|
86
|
-
}
|
|
87
118
|
function* Array(path, current, next) {
|
|
88
119
|
if (!is_1.Is.Array(next))
|
|
89
120
|
return yield Update(path, next);
|
|
@@ -117,9 +148,6 @@ var ValueDelta;
|
|
|
117
148
|
if (is_1.Is.Object(current)) {
|
|
118
149
|
return yield* Object(path, current, next);
|
|
119
150
|
}
|
|
120
|
-
else if (is_1.Is.Date(current)) {
|
|
121
|
-
return yield* Date(path, current, next);
|
|
122
|
-
}
|
|
123
151
|
else if (is_1.Is.Array(current)) {
|
|
124
152
|
return yield* Array(path, current, next);
|
|
125
153
|
}
|
|
@@ -130,7 +158,7 @@ var ValueDelta;
|
|
|
130
158
|
return yield* Value(path, current, next);
|
|
131
159
|
}
|
|
132
160
|
else {
|
|
133
|
-
throw new
|
|
161
|
+
throw new ValueDeltaUnableToDiffUnknownValue(current);
|
|
134
162
|
}
|
|
135
163
|
}
|
|
136
164
|
function Diff(current, next) {
|
package/value/value.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export declare namespace Value {
|
|
|
25
25
|
/** Returns a structural clone of the given value */
|
|
26
26
|
function Clone<T>(value: T): T;
|
|
27
27
|
/** Returns edits to transform the current value into the next value */
|
|
28
|
-
function Diff
|
|
28
|
+
function Diff(current: unknown, next: unknown): Edit[];
|
|
29
29
|
/** Returns a new value with edits applied to the given value */
|
|
30
|
-
function Patch<T>(current:
|
|
30
|
+
function Patch<T = any>(current: unknown, edits: Edit[]): T;
|
|
31
31
|
}
|