@sinclair/typebox 0.33.2 → 0.33.3
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.
|
@@ -24,13 +24,9 @@ export declare const Delete: TObject<{
|
|
|
24
24
|
}>;
|
|
25
25
|
export type Edit = Static<typeof Edit>;
|
|
26
26
|
export declare const Edit: TUnion<[typeof Insert, typeof Update, typeof Delete]>;
|
|
27
|
-
export declare class
|
|
27
|
+
export declare class ValueDiffError extends TypeBoxError {
|
|
28
28
|
readonly value: unknown;
|
|
29
29
|
constructor(value: unknown, message: string);
|
|
30
30
|
}
|
|
31
|
-
export declare class ValueDeltaSymbolError extends ValueDeltaError {
|
|
32
|
-
readonly value: unknown;
|
|
33
|
-
constructor(value: unknown);
|
|
34
|
-
}
|
|
35
31
|
export declare function Diff(current: unknown, next: unknown): Edit[];
|
|
36
32
|
export declare function Patch<T = any>(current: unknown, edits: Edit[]): T;
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.
|
|
4
|
+
exports.ValueDiffError = exports.Edit = exports.Delete = exports.Update = exports.Insert = void 0;
|
|
5
5
|
exports.Diff = Diff;
|
|
6
6
|
exports.Patch = Patch;
|
|
7
7
|
const index_1 = require("../guard/index");
|
|
8
8
|
const index_2 = require("../pointer/index");
|
|
9
9
|
const index_3 = require("../clone/index");
|
|
10
|
+
const equal_1 = require("../equal/equal");
|
|
10
11
|
const index_4 = require("../../type/error/index");
|
|
11
12
|
const index_5 = require("../../type/literal/index");
|
|
12
13
|
const index_6 = require("../../type/object/index");
|
|
@@ -31,20 +32,13 @@ exports.Edit = (0, index_9.Union)([exports.Insert, exports.Update, exports.Delet
|
|
|
31
32
|
// ------------------------------------------------------------------
|
|
32
33
|
// Errors
|
|
33
34
|
// ------------------------------------------------------------------
|
|
34
|
-
class
|
|
35
|
+
class ValueDiffError extends index_4.TypeBoxError {
|
|
35
36
|
constructor(value, message) {
|
|
36
37
|
super(message);
|
|
37
38
|
this.value = value;
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
|
-
exports.
|
|
41
|
-
class ValueDeltaSymbolError extends ValueDeltaError {
|
|
42
|
-
constructor(value) {
|
|
43
|
-
super(value, 'Cannot diff objects with symbol keys');
|
|
44
|
-
this.value = value;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
exports.ValueDeltaSymbolError = ValueDeltaSymbolError;
|
|
41
|
+
exports.ValueDiffError = ValueDiffError;
|
|
48
42
|
// ------------------------------------------------------------------
|
|
49
43
|
// Command Factory
|
|
50
44
|
// ------------------------------------------------------------------
|
|
@@ -58,37 +52,47 @@ function CreateDelete(path) {
|
|
|
58
52
|
return { type: 'delete', path };
|
|
59
53
|
}
|
|
60
54
|
// ------------------------------------------------------------------
|
|
55
|
+
// AssertDiffable
|
|
56
|
+
// ------------------------------------------------------------------
|
|
57
|
+
function AssertDiffable(value) {
|
|
58
|
+
if (globalThis.Object.getOwnPropertySymbols(value).length > 0)
|
|
59
|
+
throw new ValueDiffError(value, 'Cannot diff objects with symbols');
|
|
60
|
+
}
|
|
61
|
+
// ------------------------------------------------------------------
|
|
61
62
|
// Diffing Generators
|
|
62
63
|
// ------------------------------------------------------------------
|
|
63
64
|
function* ObjectType(path, current, next) {
|
|
65
|
+
AssertDiffable(current);
|
|
66
|
+
AssertDiffable(next);
|
|
64
67
|
if (!(0, index_1.IsStandardObject)(next))
|
|
65
68
|
return yield CreateUpdate(path, next);
|
|
66
|
-
const currentKeys =
|
|
67
|
-
const nextKeys =
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if ((0, index_1.IsUndefined)(next[key]) && nextKeys.includes(key))
|
|
72
|
-
yield CreateUpdate(`${path}/${globalThis.String(key)}`, undefined);
|
|
73
|
-
}
|
|
69
|
+
const currentKeys = globalThis.Object.getOwnPropertyNames(current);
|
|
70
|
+
const nextKeys = globalThis.Object.getOwnPropertyNames(next);
|
|
71
|
+
// ----------------------------------------------------------------
|
|
72
|
+
// inserts
|
|
73
|
+
// ----------------------------------------------------------------
|
|
74
74
|
for (const key of nextKeys) {
|
|
75
|
-
if ((0, index_1.
|
|
75
|
+
if ((0, index_1.HasPropertyKey)(current, key))
|
|
76
76
|
continue;
|
|
77
|
-
|
|
78
|
-
throw new ValueDeltaSymbolError(key);
|
|
79
|
-
yield* Visit(`${path}/${globalThis.String(key)}`, current[key], next[key]);
|
|
77
|
+
yield CreateInsert(`${path}/${key}`, next[key]);
|
|
80
78
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
79
|
+
// ----------------------------------------------------------------
|
|
80
|
+
// updates
|
|
81
|
+
// ----------------------------------------------------------------
|
|
82
|
+
for (const key of currentKeys) {
|
|
83
|
+
if (!(0, index_1.HasPropertyKey)(next, key))
|
|
84
|
+
continue;
|
|
85
|
+
if ((0, equal_1.Equal)(current, next))
|
|
86
|
+
continue;
|
|
87
|
+
yield* Visit(`${path}/${key}`, current[key], next[key]);
|
|
86
88
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
89
|
+
// ----------------------------------------------------------------
|
|
90
|
+
// deletes
|
|
91
|
+
// ----------------------------------------------------------------
|
|
92
|
+
for (const key of currentKeys) {
|
|
93
|
+
if ((0, index_1.HasPropertyKey)(next, key))
|
|
94
|
+
continue;
|
|
95
|
+
yield CreateDelete(`${path}/${key}`);
|
|
92
96
|
}
|
|
93
97
|
}
|
|
94
98
|
function* ArrayType(path, current, next) {
|
|
@@ -129,7 +133,7 @@ function* Visit(path, current, next) {
|
|
|
129
133
|
return yield* TypedArrayType(path, current, next);
|
|
130
134
|
if ((0, index_1.IsValueType)(current))
|
|
131
135
|
return yield* ValueType(path, current, next);
|
|
132
|
-
throw new
|
|
136
|
+
throw new ValueDiffError(current, 'Unable to diff value');
|
|
133
137
|
}
|
|
134
138
|
// ------------------------------------------------------------------
|
|
135
139
|
// Diff
|
|
@@ -24,13 +24,9 @@ export declare const Delete: TObject<{
|
|
|
24
24
|
}>;
|
|
25
25
|
export type Edit = Static<typeof Edit>;
|
|
26
26
|
export declare const Edit: TUnion<[typeof Insert, typeof Update, typeof Delete]>;
|
|
27
|
-
export declare class
|
|
27
|
+
export declare class ValueDiffError extends TypeBoxError {
|
|
28
28
|
readonly value: unknown;
|
|
29
29
|
constructor(value: unknown, message: string);
|
|
30
30
|
}
|
|
31
|
-
export declare class ValueDeltaSymbolError extends ValueDeltaError {
|
|
32
|
-
readonly value: unknown;
|
|
33
|
-
constructor(value: unknown);
|
|
34
|
-
}
|
|
35
31
|
export declare function Diff(current: unknown, next: unknown): Edit[];
|
|
36
32
|
export declare function Patch<T = any>(current: unknown, edits: Edit[]): T;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { IsStandardObject, IsArray, IsTypedArray, IsValueType
|
|
1
|
+
import { HasPropertyKey, IsStandardObject, IsArray, IsTypedArray, IsValueType } from '../guard/index.mjs';
|
|
2
2
|
import { ValuePointer } from '../pointer/index.mjs';
|
|
3
3
|
import { Clone } from '../clone/index.mjs';
|
|
4
|
+
import { Equal } from '../equal/equal.mjs';
|
|
4
5
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
5
6
|
import { Literal } from '../../type/literal/index.mjs';
|
|
6
7
|
import { Object } from '../../type/object/index.mjs';
|
|
@@ -25,18 +26,12 @@ export const Edit = Union([Insert, Update, Delete]);
|
|
|
25
26
|
// ------------------------------------------------------------------
|
|
26
27
|
// Errors
|
|
27
28
|
// ------------------------------------------------------------------
|
|
28
|
-
export class
|
|
29
|
+
export class ValueDiffError extends TypeBoxError {
|
|
29
30
|
constructor(value, message) {
|
|
30
31
|
super(message);
|
|
31
32
|
this.value = value;
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
|
-
export class ValueDeltaSymbolError extends ValueDeltaError {
|
|
35
|
-
constructor(value) {
|
|
36
|
-
super(value, 'Cannot diff objects with symbol keys');
|
|
37
|
-
this.value = value;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
35
|
// ------------------------------------------------------------------
|
|
41
36
|
// Command Factory
|
|
42
37
|
// ------------------------------------------------------------------
|
|
@@ -50,37 +45,47 @@ function CreateDelete(path) {
|
|
|
50
45
|
return { type: 'delete', path };
|
|
51
46
|
}
|
|
52
47
|
// ------------------------------------------------------------------
|
|
48
|
+
// AssertDiffable
|
|
49
|
+
// ------------------------------------------------------------------
|
|
50
|
+
function AssertDiffable(value) {
|
|
51
|
+
if (globalThis.Object.getOwnPropertySymbols(value).length > 0)
|
|
52
|
+
throw new ValueDiffError(value, 'Cannot diff objects with symbols');
|
|
53
|
+
}
|
|
54
|
+
// ------------------------------------------------------------------
|
|
53
55
|
// Diffing Generators
|
|
54
56
|
// ------------------------------------------------------------------
|
|
55
57
|
function* ObjectType(path, current, next) {
|
|
58
|
+
AssertDiffable(current);
|
|
59
|
+
AssertDiffable(next);
|
|
56
60
|
if (!IsStandardObject(next))
|
|
57
61
|
return yield CreateUpdate(path, next);
|
|
58
|
-
const currentKeys =
|
|
59
|
-
const nextKeys =
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (IsUndefined(next[key]) && nextKeys.includes(key))
|
|
64
|
-
yield CreateUpdate(`${path}/${globalThis.String(key)}`, undefined);
|
|
65
|
-
}
|
|
62
|
+
const currentKeys = globalThis.Object.getOwnPropertyNames(current);
|
|
63
|
+
const nextKeys = globalThis.Object.getOwnPropertyNames(next);
|
|
64
|
+
// ----------------------------------------------------------------
|
|
65
|
+
// inserts
|
|
66
|
+
// ----------------------------------------------------------------
|
|
66
67
|
for (const key of nextKeys) {
|
|
67
|
-
if (
|
|
68
|
+
if (HasPropertyKey(current, key))
|
|
68
69
|
continue;
|
|
69
|
-
|
|
70
|
-
throw new ValueDeltaSymbolError(key);
|
|
71
|
-
yield* Visit(`${path}/${globalThis.String(key)}`, current[key], next[key]);
|
|
70
|
+
yield CreateInsert(`${path}/${key}`, next[key]);
|
|
72
71
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
72
|
+
// ----------------------------------------------------------------
|
|
73
|
+
// updates
|
|
74
|
+
// ----------------------------------------------------------------
|
|
75
|
+
for (const key of currentKeys) {
|
|
76
|
+
if (!HasPropertyKey(next, key))
|
|
77
|
+
continue;
|
|
78
|
+
if (Equal(current, next))
|
|
79
|
+
continue;
|
|
80
|
+
yield* Visit(`${path}/${key}`, current[key], next[key]);
|
|
78
81
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
// ----------------------------------------------------------------
|
|
83
|
+
// deletes
|
|
84
|
+
// ----------------------------------------------------------------
|
|
85
|
+
for (const key of currentKeys) {
|
|
86
|
+
if (HasPropertyKey(next, key))
|
|
87
|
+
continue;
|
|
88
|
+
yield CreateDelete(`${path}/${key}`);
|
|
84
89
|
}
|
|
85
90
|
}
|
|
86
91
|
function* ArrayType(path, current, next) {
|
|
@@ -121,7 +126,7 @@ function* Visit(path, current, next) {
|
|
|
121
126
|
return yield* TypedArrayType(path, current, next);
|
|
122
127
|
if (IsValueType(current))
|
|
123
128
|
return yield* ValueType(path, current, next);
|
|
124
|
-
throw new
|
|
129
|
+
throw new ValueDiffError(current, 'Unable to diff value');
|
|
125
130
|
}
|
|
126
131
|
// ------------------------------------------------------------------
|
|
127
132
|
// Diff
|