@sinclair/typebox 0.25.2 → 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/cast.js +63 -62
- package/value/create.js +0 -13
- 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/cast.js
CHANGED
|
@@ -32,50 +32,9 @@ const Types = require("../typebox");
|
|
|
32
32
|
const create_1 = require("./create");
|
|
33
33
|
const check_1 = require("./check");
|
|
34
34
|
const clone_1 = require("./clone");
|
|
35
|
-
|
|
36
|
-
(function (UnionValueCast) {
|
|
37
|
-
// ----------------------------------------------------------------------------------------------
|
|
38
|
-
// The following will score a schema against a value. For objects, the score is the tally of
|
|
39
|
-
// points awarded for each property of the value. Property points are (1.0 / propertyCount)
|
|
40
|
-
// to prevent large property counts biasing results. Properties that match literal values are
|
|
41
|
-
// maximally awarded as literals are typically used as union discriminator fields.
|
|
42
|
-
// ----------------------------------------------------------------------------------------------
|
|
43
|
-
function Score(schema, references, value) {
|
|
44
|
-
if (schema[Types.Kind] === 'Object' && typeof value === 'object' && value !== null) {
|
|
45
|
-
const object = schema;
|
|
46
|
-
const keys = Object.keys(value);
|
|
47
|
-
const entries = globalThis.Object.entries(object.properties);
|
|
48
|
-
const [point, max] = [1 / entries.length, entries.length];
|
|
49
|
-
return entries.reduce((acc, [key, schema]) => {
|
|
50
|
-
const literal = schema[Types.Kind] === 'Literal' && schema.const === value[key] ? max : 0;
|
|
51
|
-
const checks = check_1.ValueCheck.Check(schema, references, value[key]) ? point : 0;
|
|
52
|
-
const exists = keys.includes(key) ? point : 0;
|
|
53
|
-
return acc + (literal + checks + exists);
|
|
54
|
-
}, 0);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
return check_1.ValueCheck.Check(schema, references, value) ? 1 : 0;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
function Select(union, references, value) {
|
|
61
|
-
let [select, best] = [union.anyOf[0], 0];
|
|
62
|
-
for (const schema of union.anyOf) {
|
|
63
|
-
const score = Score(schema, references, value);
|
|
64
|
-
if (score > best) {
|
|
65
|
-
select = schema;
|
|
66
|
-
best = score;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return select;
|
|
70
|
-
}
|
|
71
|
-
function Create(union, references, value) {
|
|
72
|
-
return check_1.ValueCheck.Check(union, references, value) ? clone_1.ValueClone.Clone(value) : ValueCast.Cast(Select(union, references, value), references, value);
|
|
73
|
-
}
|
|
74
|
-
UnionValueCast.Create = Create;
|
|
75
|
-
})(UnionValueCast || (UnionValueCast = {}));
|
|
76
|
-
// -----------------------------------------------------------
|
|
35
|
+
// ----------------------------------------------------------------------------------------------
|
|
77
36
|
// Errors
|
|
78
|
-
//
|
|
37
|
+
// ----------------------------------------------------------------------------------------------
|
|
79
38
|
class ValueCastReferenceTypeError extends Error {
|
|
80
39
|
constructor(schema) {
|
|
81
40
|
super(`ValueCast: Cannot locate referenced schema with $id '${schema.$ref}'`);
|
|
@@ -112,11 +71,58 @@ class ValueCastUnknownTypeError extends Error {
|
|
|
112
71
|
}
|
|
113
72
|
}
|
|
114
73
|
exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
|
|
74
|
+
// ----------------------------------------------------------------------------------------------
|
|
75
|
+
// The following will score a schema against a value. For objects, the score is the tally of
|
|
76
|
+
// points awarded for each property of the value. Property points are (1.0 / propertyCount)
|
|
77
|
+
// to prevent large property counts biasing results. Properties that match literal values are
|
|
78
|
+
// maximally awarded as literals are typically used as union discriminator fields.
|
|
79
|
+
// ----------------------------------------------------------------------------------------------
|
|
80
|
+
var UnionCastCreate;
|
|
81
|
+
(function (UnionCastCreate) {
|
|
82
|
+
function Score(schema, references, value) {
|
|
83
|
+
if (schema[Types.Kind] === 'Object' && typeof value === 'object' && value !== null) {
|
|
84
|
+
const object = schema;
|
|
85
|
+
const keys = Object.keys(value);
|
|
86
|
+
const entries = globalThis.Object.entries(object.properties);
|
|
87
|
+
const [point, max] = [1 / entries.length, entries.length];
|
|
88
|
+
return entries.reduce((acc, [key, schema]) => {
|
|
89
|
+
const literal = schema[Types.Kind] === 'Literal' && schema.const === value[key] ? max : 0;
|
|
90
|
+
const checks = check_1.ValueCheck.Check(schema, references, value[key]) ? point : 0;
|
|
91
|
+
const exists = keys.includes(key) ? point : 0;
|
|
92
|
+
return acc + (literal + checks + exists);
|
|
93
|
+
}, 0);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
return check_1.ValueCheck.Check(schema, references, value) ? 1 : 0;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function Select(union, references, value) {
|
|
100
|
+
let [select, best] = [union.anyOf[0], 0];
|
|
101
|
+
for (const schema of union.anyOf) {
|
|
102
|
+
const score = Score(schema, references, value);
|
|
103
|
+
if (score > best) {
|
|
104
|
+
select = schema;
|
|
105
|
+
best = score;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return select;
|
|
109
|
+
}
|
|
110
|
+
function Create(union, references, value) {
|
|
111
|
+
if (union.default !== undefined) {
|
|
112
|
+
return union.default;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
const schema = Select(union, references, value);
|
|
116
|
+
return ValueCast.Cast(schema, references, value);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
UnionCastCreate.Create = Create;
|
|
120
|
+
})(UnionCastCreate || (UnionCastCreate = {}));
|
|
115
121
|
var ValueCast;
|
|
116
122
|
(function (ValueCast) {
|
|
117
|
-
//
|
|
123
|
+
// ----------------------------------------------------------------------------------------------
|
|
118
124
|
// Guards
|
|
119
|
-
//
|
|
125
|
+
// ----------------------------------------------------------------------------------------------
|
|
120
126
|
function IsArray(value) {
|
|
121
127
|
return typeof value === 'object' && globalThis.Array.isArray(value);
|
|
122
128
|
}
|
|
@@ -144,9 +150,9 @@ var ValueCast;
|
|
|
144
150
|
function IsValueFalse(value) {
|
|
145
151
|
return value === false || (IsNumber(value) && value === 0) || (IsBigInt(value) && value === 0n) || (IsString(value) && (value.toLowerCase() === 'false' || value === '0'));
|
|
146
152
|
}
|
|
147
|
-
//
|
|
153
|
+
// ----------------------------------------------------------------------------------------------
|
|
148
154
|
// Convert
|
|
149
|
-
//
|
|
155
|
+
// ----------------------------------------------------------------------------------------------
|
|
150
156
|
function TryConvertString(value) {
|
|
151
157
|
return IsValueToString(value) ? value.toString() : value;
|
|
152
158
|
}
|
|
@@ -159,11 +165,11 @@ var ValueCast;
|
|
|
159
165
|
function TryConvertBoolean(value) {
|
|
160
166
|
return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value;
|
|
161
167
|
}
|
|
162
|
-
//
|
|
168
|
+
// ----------------------------------------------------------------------------------------------
|
|
163
169
|
// Cast
|
|
164
|
-
//
|
|
170
|
+
// ----------------------------------------------------------------------------------------------
|
|
165
171
|
function Any(schema, references, value) {
|
|
166
|
-
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
172
|
+
return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
|
|
167
173
|
}
|
|
168
174
|
function Array(schema, references, value) {
|
|
169
175
|
if (check_1.ValueCheck.Check(schema, references, value))
|
|
@@ -198,9 +204,6 @@ var ValueCast;
|
|
|
198
204
|
function Date(schema, references, value) {
|
|
199
205
|
return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
|
|
200
206
|
}
|
|
201
|
-
function Enum(schema, references, value) {
|
|
202
|
-
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
203
|
-
}
|
|
204
207
|
function Function(schema, references, value) {
|
|
205
208
|
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
206
209
|
}
|
|
@@ -209,13 +212,13 @@ var ValueCast;
|
|
|
209
212
|
return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references);
|
|
210
213
|
}
|
|
211
214
|
function Literal(schema, references, value) {
|
|
212
|
-
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
215
|
+
return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
|
|
213
216
|
}
|
|
214
217
|
function Never(schema, references, value) {
|
|
215
218
|
throw new ValueCastNeverTypeError(schema);
|
|
216
219
|
}
|
|
217
220
|
function Null(schema, references, value) {
|
|
218
|
-
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
221
|
+
return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
|
|
219
222
|
}
|
|
220
223
|
function Number(schema, references, value) {
|
|
221
224
|
const conversion = TryConvertNumber(value);
|
|
@@ -289,19 +292,19 @@ var ValueCast;
|
|
|
289
292
|
return schema.items.map((schema, index) => Visit(schema, references, value[index]));
|
|
290
293
|
}
|
|
291
294
|
function Undefined(schema, references, value) {
|
|
292
|
-
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
295
|
+
return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
|
|
293
296
|
}
|
|
294
297
|
function Union(schema, references, value) {
|
|
295
|
-
return
|
|
298
|
+
return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : UnionCastCreate.Create(schema, references, value);
|
|
296
299
|
}
|
|
297
300
|
function Uint8Array(schema, references, value) {
|
|
298
|
-
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
301
|
+
return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
|
|
299
302
|
}
|
|
300
303
|
function Unknown(schema, references, value) {
|
|
301
|
-
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
304
|
+
return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
|
|
302
305
|
}
|
|
303
306
|
function Void(schema, references, value) {
|
|
304
|
-
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
307
|
+
return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
|
|
305
308
|
}
|
|
306
309
|
function Visit(schema, references, value) {
|
|
307
310
|
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
@@ -317,8 +320,6 @@ var ValueCast;
|
|
|
317
320
|
return Constructor(anySchema, anyReferences, value);
|
|
318
321
|
case 'Date':
|
|
319
322
|
return Date(anySchema, anyReferences, value);
|
|
320
|
-
case 'Enum':
|
|
321
|
-
return Enum(anySchema, anyReferences, value);
|
|
322
323
|
case 'Function':
|
|
323
324
|
return Function(anySchema, anyReferences, value);
|
|
324
325
|
case 'Integer':
|
package/value/create.js
CHANGED
|
@@ -110,17 +110,6 @@ var ValueCreate;
|
|
|
110
110
|
return new globalThis.Date(0);
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
|
-
function Enum(schema, references) {
|
|
114
|
-
if (schema.default !== undefined) {
|
|
115
|
-
return schema.default;
|
|
116
|
-
}
|
|
117
|
-
else if (schema.anyOf.length === 0) {
|
|
118
|
-
throw new Error('ValueCreate.Enum: Cannot create default enum value as this enum has no items');
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
return schema.anyOf[0].const;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
113
|
function Function(schema, references) {
|
|
125
114
|
if (schema.default !== undefined) {
|
|
126
115
|
return schema.default;
|
|
@@ -318,8 +307,6 @@ var ValueCreate;
|
|
|
318
307
|
return Constructor(anySchema, anyReferences);
|
|
319
308
|
case 'Date':
|
|
320
309
|
return Date(anySchema, anyReferences);
|
|
321
|
-
case 'Enum':
|
|
322
|
-
return Enum(anySchema, anyReferences);
|
|
323
310
|
case 'Function':
|
|
324
311
|
return Function(anySchema, anyReferences);
|
|
325
312
|
case 'Integer':
|
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
|
}
|