@sinclair/typebox 0.25.24 → 0.26.0-dev.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/compiler/compiler.d.ts +10 -5
- package/compiler/compiler.js +161 -123
- package/errors/errors.d.ts +56 -46
- package/errors/errors.js +234 -153
- package/package.json +1 -6
- package/readme.md +294 -207
- package/system/system.d.ts +9 -6
- package/system/system.js +17 -17
- package/typebox.d.ts +388 -162
- package/typebox.js +1716 -229
- package/value/cast.d.ts +2 -2
- package/value/cast.js +121 -188
- package/value/check.d.ts +1 -1
- package/value/check.js +156 -111
- package/value/convert.d.ts +13 -0
- package/value/convert.js +345 -0
- package/value/create.d.ts +6 -2
- package/value/create.js +149 -97
- package/{hash → value}/hash.js +39 -14
- package/value/index.d.ts +1 -0
- package/value/index.js +3 -1
- package/value/value.d.ts +2 -8
- package/value/value.js +20 -14
- package/conditional/conditional.d.ts +0 -17
- package/conditional/conditional.js +0 -91
- package/conditional/index.d.ts +0 -2
- package/conditional/index.js +0 -45
- package/conditional/structural.d.ts +0 -11
- package/conditional/structural.js +0 -685
- package/custom/custom.d.ts +0 -12
- package/custom/custom.js +0 -55
- package/custom/index.d.ts +0 -1
- package/custom/index.js +0 -44
- package/format/format.d.ts +0 -12
- package/format/format.js +0 -55
- package/format/index.d.ts +0 -1
- package/format/index.js +0 -44
- package/guard/extends.d.ts +0 -10
- package/guard/extends.js +0 -50
- package/guard/guard.d.ts +0 -60
- package/guard/guard.js +0 -440
- package/guard/index.d.ts +0 -2
- package/guard/index.js +0 -45
- package/hash/index.d.ts +0 -1
- package/hash/index.js +0 -44
- /package/{hash → value}/hash.d.ts +0 -0
package/value/create.js
CHANGED
|
@@ -27,9 +27,12 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueCreate = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
|
|
30
|
+
exports.ValueCreate = exports.ValueCreateIntersectTypeError = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
|
-
const
|
|
32
|
+
const check_1 = require("./check");
|
|
33
|
+
// --------------------------------------------------------------------------
|
|
34
|
+
// Errors
|
|
35
|
+
// --------------------------------------------------------------------------
|
|
33
36
|
class ValueCreateUnknownTypeError extends Error {
|
|
34
37
|
constructor(schema) {
|
|
35
38
|
super('ValueCreate: Unknown type');
|
|
@@ -44,46 +47,64 @@ class ValueCreateNeverTypeError extends Error {
|
|
|
44
47
|
}
|
|
45
48
|
}
|
|
46
49
|
exports.ValueCreateNeverTypeError = ValueCreateNeverTypeError;
|
|
50
|
+
class ValueCreateIntersectTypeError extends Error {
|
|
51
|
+
constructor(schema) {
|
|
52
|
+
super('ValueCreate: Can only create values for intersected objects and non-varying primitive types. Consider using a default value.');
|
|
53
|
+
this.schema = schema;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.ValueCreateIntersectTypeError = ValueCreateIntersectTypeError;
|
|
57
|
+
// --------------------------------------------------------------------------
|
|
58
|
+
// ValueCreate
|
|
59
|
+
// --------------------------------------------------------------------------
|
|
47
60
|
var ValueCreate;
|
|
48
61
|
(function (ValueCreate) {
|
|
49
|
-
function Any(schema
|
|
50
|
-
if (
|
|
62
|
+
function Any(schema) {
|
|
63
|
+
if ('default' in schema) {
|
|
51
64
|
return schema.default;
|
|
52
65
|
}
|
|
53
66
|
else {
|
|
54
67
|
return {};
|
|
55
68
|
}
|
|
56
69
|
}
|
|
57
|
-
function Array(schema
|
|
70
|
+
function Array(schema) {
|
|
58
71
|
if (schema.uniqueItems === true && schema.default === undefined) {
|
|
59
72
|
throw new Error('ValueCreate.Array: Arrays with uniqueItems require a default value');
|
|
60
73
|
}
|
|
61
|
-
else if (
|
|
74
|
+
else if ('default' in schema) {
|
|
62
75
|
return schema.default;
|
|
63
76
|
}
|
|
64
77
|
else if (schema.minItems !== undefined) {
|
|
65
78
|
return globalThis.Array.from({ length: schema.minItems }).map((item) => {
|
|
66
|
-
return ValueCreate.Create(schema.items
|
|
79
|
+
return ValueCreate.Create(schema.items);
|
|
67
80
|
});
|
|
68
81
|
}
|
|
69
82
|
else {
|
|
70
83
|
return [];
|
|
71
84
|
}
|
|
72
85
|
}
|
|
73
|
-
function
|
|
74
|
-
if (
|
|
86
|
+
function BigInt(schema) {
|
|
87
|
+
if ('default' in schema) {
|
|
88
|
+
return schema.default;
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
return globalThis.BigInt(0);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function Boolean(schema) {
|
|
95
|
+
if ('default' in schema) {
|
|
75
96
|
return schema.default;
|
|
76
97
|
}
|
|
77
98
|
else {
|
|
78
99
|
return false;
|
|
79
100
|
}
|
|
80
101
|
}
|
|
81
|
-
function Constructor(schema
|
|
82
|
-
if (
|
|
102
|
+
function Constructor(schema) {
|
|
103
|
+
if ('default' in schema) {
|
|
83
104
|
return schema.default;
|
|
84
105
|
}
|
|
85
106
|
else {
|
|
86
|
-
const value = ValueCreate.Create(schema.returns
|
|
107
|
+
const value = ValueCreate.Create(schema.returns);
|
|
87
108
|
if (typeof value === 'object' && !globalThis.Array.isArray(value)) {
|
|
88
109
|
return class {
|
|
89
110
|
constructor() {
|
|
@@ -100,8 +121,8 @@ var ValueCreate;
|
|
|
100
121
|
}
|
|
101
122
|
}
|
|
102
123
|
}
|
|
103
|
-
function Date(schema
|
|
104
|
-
if (
|
|
124
|
+
function Date(schema) {
|
|
125
|
+
if ('default' in schema) {
|
|
105
126
|
return schema.default;
|
|
106
127
|
}
|
|
107
128
|
else if (schema.minimumTimestamp !== undefined) {
|
|
@@ -111,16 +132,16 @@ var ValueCreate;
|
|
|
111
132
|
return new globalThis.Date(0);
|
|
112
133
|
}
|
|
113
134
|
}
|
|
114
|
-
function Function(schema
|
|
115
|
-
if (
|
|
135
|
+
function Function(schema) {
|
|
136
|
+
if ('default' in schema) {
|
|
116
137
|
return schema.default;
|
|
117
138
|
}
|
|
118
139
|
else {
|
|
119
|
-
return () => ValueCreate.Create(schema.returns
|
|
140
|
+
return () => ValueCreate.Create(schema.returns);
|
|
120
141
|
}
|
|
121
142
|
}
|
|
122
|
-
function Integer(schema
|
|
123
|
-
if (
|
|
143
|
+
function Integer(schema) {
|
|
144
|
+
if ('default' in schema) {
|
|
124
145
|
return schema.default;
|
|
125
146
|
}
|
|
126
147
|
else if (schema.minimum !== undefined) {
|
|
@@ -130,27 +151,46 @@ var ValueCreate;
|
|
|
130
151
|
return 0;
|
|
131
152
|
}
|
|
132
153
|
}
|
|
133
|
-
function
|
|
134
|
-
if (
|
|
154
|
+
function Intersect(schema) {
|
|
155
|
+
if ('default' in schema) {
|
|
156
|
+
return schema.default;
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
const value = schema.type === 'object' ? schema.allOf.reduce((acc, schema) => ({ ...acc, ...Visit(schema) }), {}) : schema.allOf.reduce((_, schema) => Visit(schema), undefined);
|
|
160
|
+
if (!check_1.ValueCheck.Check(schema, value))
|
|
161
|
+
throw new ValueCreateIntersectTypeError(schema);
|
|
162
|
+
return value;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function Literal(schema) {
|
|
166
|
+
if ('default' in schema) {
|
|
135
167
|
return schema.default;
|
|
136
168
|
}
|
|
137
169
|
else {
|
|
138
170
|
return schema.const;
|
|
139
171
|
}
|
|
140
172
|
}
|
|
141
|
-
function Never(schema
|
|
173
|
+
function Never(schema) {
|
|
142
174
|
throw new ValueCreateNeverTypeError(schema);
|
|
143
175
|
}
|
|
144
|
-
function
|
|
145
|
-
if (
|
|
176
|
+
function Not(schema) {
|
|
177
|
+
if ('default' in schema) {
|
|
178
|
+
return schema.default;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
return Visit(schema.allOf[1]);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function Null(schema) {
|
|
185
|
+
if ('default' in schema) {
|
|
146
186
|
return schema.default;
|
|
147
187
|
}
|
|
148
188
|
else {
|
|
149
189
|
return null;
|
|
150
190
|
}
|
|
151
191
|
}
|
|
152
|
-
function Number(schema
|
|
153
|
-
if (
|
|
192
|
+
function Number(schema) {
|
|
193
|
+
if ('default' in schema) {
|
|
154
194
|
return schema.default;
|
|
155
195
|
}
|
|
156
196
|
else if (schema.minimum !== undefined) {
|
|
@@ -160,66 +200,60 @@ var ValueCreate;
|
|
|
160
200
|
return 0;
|
|
161
201
|
}
|
|
162
202
|
}
|
|
163
|
-
function Object(schema
|
|
164
|
-
if (
|
|
203
|
+
function Object(schema) {
|
|
204
|
+
if ('default' in schema) {
|
|
165
205
|
return schema.default;
|
|
166
206
|
}
|
|
167
207
|
else {
|
|
168
208
|
const required = new Set(schema.required);
|
|
169
209
|
return (schema.default ||
|
|
170
210
|
globalThis.Object.entries(schema.properties).reduce((acc, [key, schema]) => {
|
|
171
|
-
return required.has(key) ? { ...acc, [key]: ValueCreate.Create(schema
|
|
211
|
+
return required.has(key) ? { ...acc, [key]: ValueCreate.Create(schema) } : { ...acc };
|
|
172
212
|
}, {}));
|
|
173
213
|
}
|
|
174
214
|
}
|
|
175
|
-
function Promise(schema
|
|
176
|
-
if (
|
|
215
|
+
function Promise(schema) {
|
|
216
|
+
if ('default' in schema) {
|
|
177
217
|
return schema.default;
|
|
178
218
|
}
|
|
179
219
|
else {
|
|
180
|
-
return globalThis.Promise.resolve(ValueCreate.Create(schema.item
|
|
220
|
+
return globalThis.Promise.resolve(ValueCreate.Create(schema.item));
|
|
181
221
|
}
|
|
182
222
|
}
|
|
183
|
-
function Record(schema
|
|
223
|
+
function Record(schema) {
|
|
184
224
|
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
185
|
-
if (
|
|
225
|
+
if ('default' in schema) {
|
|
186
226
|
return schema.default;
|
|
187
227
|
}
|
|
188
228
|
else if (!(keyPattern === '^.*$' || keyPattern === '^(0|[1-9][0-9]*)$')) {
|
|
189
229
|
const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
|
|
190
230
|
return propertyKeys.reduce((acc, key) => {
|
|
191
|
-
return { ...acc, [key]: Create(valueSchema
|
|
231
|
+
return { ...acc, [key]: Create(valueSchema) };
|
|
192
232
|
}, {});
|
|
193
233
|
}
|
|
194
234
|
else {
|
|
195
235
|
return {};
|
|
196
236
|
}
|
|
197
237
|
}
|
|
198
|
-
function Ref(schema
|
|
199
|
-
if (
|
|
238
|
+
function Ref(schema) {
|
|
239
|
+
if ('default' in schema) {
|
|
200
240
|
return schema.default;
|
|
201
241
|
}
|
|
202
242
|
else {
|
|
203
|
-
|
|
204
|
-
if (reference === undefined)
|
|
205
|
-
throw new Error(`ValueCreate.Ref: Cannot find schema with $id '${schema.$ref}'.`);
|
|
206
|
-
return Visit(reference, references);
|
|
243
|
+
return Visit(Types.ReferenceRegistry.DerefOne(schema));
|
|
207
244
|
}
|
|
208
245
|
}
|
|
209
|
-
function Self(schema
|
|
210
|
-
if (
|
|
246
|
+
function Self(schema) {
|
|
247
|
+
if ('default' in schema) {
|
|
211
248
|
return schema.default;
|
|
212
249
|
}
|
|
213
250
|
else {
|
|
214
|
-
|
|
215
|
-
if (reference === undefined)
|
|
216
|
-
throw new Error(`ValueCreate.Self: Cannot locate schema with $id '${schema.$ref}'`);
|
|
217
|
-
return Visit(reference, references);
|
|
251
|
+
return Visit(Types.ReferenceRegistry.DerefOne(schema));
|
|
218
252
|
}
|
|
219
253
|
}
|
|
220
|
-
function String(schema
|
|
254
|
+
function String(schema) {
|
|
221
255
|
if (schema.pattern !== undefined) {
|
|
222
|
-
if (
|
|
256
|
+
if (!('default' in schema)) {
|
|
223
257
|
throw new Error('ValueCreate.String: String types with patterns must specify a default value');
|
|
224
258
|
}
|
|
225
259
|
else {
|
|
@@ -227,7 +261,7 @@ var ValueCreate;
|
|
|
227
261
|
}
|
|
228
262
|
}
|
|
229
263
|
else if (schema.format !== undefined) {
|
|
230
|
-
if (
|
|
264
|
+
if (!('default' in schema)) {
|
|
231
265
|
throw new Error('ValueCreate.String: String types with formats must specify a default value');
|
|
232
266
|
}
|
|
233
267
|
else {
|
|
@@ -235,7 +269,7 @@ var ValueCreate;
|
|
|
235
269
|
}
|
|
236
270
|
}
|
|
237
271
|
else {
|
|
238
|
-
if (
|
|
272
|
+
if ('default' in schema) {
|
|
239
273
|
return schema.default;
|
|
240
274
|
}
|
|
241
275
|
else if (schema.minLength !== undefined) {
|
|
@@ -248,38 +282,49 @@ var ValueCreate;
|
|
|
248
282
|
}
|
|
249
283
|
}
|
|
250
284
|
}
|
|
251
|
-
function
|
|
252
|
-
if (
|
|
285
|
+
function Symbol(schema) {
|
|
286
|
+
if ('default' in schema) {
|
|
287
|
+
return schema.default;
|
|
288
|
+
}
|
|
289
|
+
else if ('value' in schema) {
|
|
290
|
+
return globalThis.Symbol.for(schema.value);
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
return globalThis.Symbol();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
function Tuple(schema) {
|
|
297
|
+
if ('default' in schema) {
|
|
253
298
|
return schema.default;
|
|
254
299
|
}
|
|
255
300
|
if (schema.items === undefined) {
|
|
256
301
|
return [];
|
|
257
302
|
}
|
|
258
303
|
else {
|
|
259
|
-
return globalThis.Array.from({ length: schema.minItems }).map((_, index) => ValueCreate.Create(schema.items[index]
|
|
304
|
+
return globalThis.Array.from({ length: schema.minItems }).map((_, index) => ValueCreate.Create(schema.items[index]));
|
|
260
305
|
}
|
|
261
306
|
}
|
|
262
|
-
function Undefined(schema
|
|
263
|
-
if (
|
|
307
|
+
function Undefined(schema) {
|
|
308
|
+
if ('default' in schema) {
|
|
264
309
|
return schema.default;
|
|
265
310
|
}
|
|
266
311
|
else {
|
|
267
312
|
return undefined;
|
|
268
313
|
}
|
|
269
314
|
}
|
|
270
|
-
function Union(schema
|
|
271
|
-
if (
|
|
315
|
+
function Union(schema) {
|
|
316
|
+
if ('default' in schema) {
|
|
272
317
|
return schema.default;
|
|
273
318
|
}
|
|
274
319
|
else if (schema.anyOf.length === 0) {
|
|
275
320
|
throw new Error('ValueCreate.Union: Cannot create Union with zero variants');
|
|
276
321
|
}
|
|
277
322
|
else {
|
|
278
|
-
return ValueCreate.Create(schema.anyOf[0]
|
|
323
|
+
return ValueCreate.Create(schema.anyOf[0]);
|
|
279
324
|
}
|
|
280
325
|
}
|
|
281
|
-
function Uint8Array(schema
|
|
282
|
-
if (
|
|
326
|
+
function Uint8Array(schema) {
|
|
327
|
+
if ('default' in schema) {
|
|
283
328
|
return schema.default;
|
|
284
329
|
}
|
|
285
330
|
else if (schema.minByteLength !== undefined) {
|
|
@@ -289,24 +334,24 @@ var ValueCreate;
|
|
|
289
334
|
return new globalThis.Uint8Array(0);
|
|
290
335
|
}
|
|
291
336
|
}
|
|
292
|
-
function Unknown(schema
|
|
293
|
-
if (
|
|
337
|
+
function Unknown(schema) {
|
|
338
|
+
if ('default' in schema) {
|
|
294
339
|
return schema.default;
|
|
295
340
|
}
|
|
296
341
|
else {
|
|
297
342
|
return {};
|
|
298
343
|
}
|
|
299
344
|
}
|
|
300
|
-
function Void(schema
|
|
301
|
-
if (
|
|
345
|
+
function Void(schema) {
|
|
346
|
+
if ('default' in schema) {
|
|
302
347
|
return schema.default;
|
|
303
348
|
}
|
|
304
349
|
else {
|
|
305
|
-
return
|
|
350
|
+
return void 0;
|
|
306
351
|
}
|
|
307
352
|
}
|
|
308
|
-
function UserDefined(schema
|
|
309
|
-
if (
|
|
353
|
+
function UserDefined(schema) {
|
|
354
|
+
if ('default' in schema) {
|
|
310
355
|
return schema.default;
|
|
311
356
|
}
|
|
312
357
|
else {
|
|
@@ -314,65 +359,72 @@ var ValueCreate;
|
|
|
314
359
|
}
|
|
315
360
|
}
|
|
316
361
|
/** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
|
|
317
|
-
function Visit(schema
|
|
318
|
-
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
362
|
+
function Visit(schema) {
|
|
319
363
|
const anySchema = schema;
|
|
320
364
|
switch (anySchema[Types.Kind]) {
|
|
321
365
|
case 'Any':
|
|
322
|
-
return Any(anySchema
|
|
366
|
+
return Any(anySchema);
|
|
323
367
|
case 'Array':
|
|
324
|
-
return Array(anySchema
|
|
368
|
+
return Array(anySchema);
|
|
369
|
+
case 'BigInt':
|
|
370
|
+
return BigInt(anySchema);
|
|
325
371
|
case 'Boolean':
|
|
326
|
-
return Boolean(anySchema
|
|
372
|
+
return Boolean(anySchema);
|
|
327
373
|
case 'Constructor':
|
|
328
|
-
return Constructor(anySchema
|
|
374
|
+
return Constructor(anySchema);
|
|
329
375
|
case 'Date':
|
|
330
|
-
return Date(anySchema
|
|
376
|
+
return Date(anySchema);
|
|
331
377
|
case 'Function':
|
|
332
|
-
return Function(anySchema
|
|
378
|
+
return Function(anySchema);
|
|
333
379
|
case 'Integer':
|
|
334
|
-
return Integer(anySchema
|
|
380
|
+
return Integer(anySchema);
|
|
381
|
+
case 'Intersect':
|
|
382
|
+
return Intersect(anySchema);
|
|
335
383
|
case 'Literal':
|
|
336
|
-
return Literal(anySchema
|
|
384
|
+
return Literal(anySchema);
|
|
337
385
|
case 'Never':
|
|
338
|
-
return Never(anySchema
|
|
386
|
+
return Never(anySchema);
|
|
387
|
+
case 'Not':
|
|
388
|
+
return Not(anySchema);
|
|
339
389
|
case 'Null':
|
|
340
|
-
return Null(anySchema
|
|
390
|
+
return Null(anySchema);
|
|
341
391
|
case 'Number':
|
|
342
|
-
return Number(anySchema
|
|
392
|
+
return Number(anySchema);
|
|
343
393
|
case 'Object':
|
|
344
|
-
return Object(anySchema
|
|
394
|
+
return Object(anySchema);
|
|
345
395
|
case 'Promise':
|
|
346
|
-
return Promise(anySchema
|
|
396
|
+
return Promise(anySchema);
|
|
347
397
|
case 'Record':
|
|
348
|
-
return Record(anySchema
|
|
398
|
+
return Record(anySchema);
|
|
349
399
|
case 'Ref':
|
|
350
|
-
return Ref(anySchema
|
|
400
|
+
return Ref(anySchema);
|
|
351
401
|
case 'Self':
|
|
352
|
-
return Self(anySchema
|
|
402
|
+
return Self(anySchema);
|
|
353
403
|
case 'String':
|
|
354
|
-
return String(anySchema
|
|
404
|
+
return String(anySchema);
|
|
405
|
+
case 'Symbol':
|
|
406
|
+
return Symbol(anySchema);
|
|
355
407
|
case 'Tuple':
|
|
356
|
-
return Tuple(anySchema
|
|
408
|
+
return Tuple(anySchema);
|
|
357
409
|
case 'Undefined':
|
|
358
|
-
return Undefined(anySchema
|
|
410
|
+
return Undefined(anySchema);
|
|
359
411
|
case 'Union':
|
|
360
|
-
return Union(anySchema
|
|
412
|
+
return Union(anySchema);
|
|
361
413
|
case 'Uint8Array':
|
|
362
|
-
return Uint8Array(anySchema
|
|
414
|
+
return Uint8Array(anySchema);
|
|
363
415
|
case 'Unknown':
|
|
364
|
-
return Unknown(anySchema
|
|
416
|
+
return Unknown(anySchema);
|
|
365
417
|
case 'Void':
|
|
366
|
-
return Void(anySchema
|
|
418
|
+
return Void(anySchema);
|
|
367
419
|
default:
|
|
368
|
-
if (!
|
|
420
|
+
if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
|
|
369
421
|
throw new ValueCreateUnknownTypeError(anySchema);
|
|
370
|
-
return UserDefined(anySchema
|
|
422
|
+
return UserDefined(anySchema);
|
|
371
423
|
}
|
|
372
424
|
}
|
|
373
425
|
ValueCreate.Visit = Visit;
|
|
374
|
-
function Create(schema
|
|
375
|
-
return Visit(schema
|
|
426
|
+
function Create(schema) {
|
|
427
|
+
return Visit(schema);
|
|
376
428
|
}
|
|
377
429
|
ValueCreate.Create = Create;
|
|
378
430
|
})(ValueCreate = exports.ValueCreate || (exports.ValueCreate = {}));
|
package/{hash → value}/hash.js
RENAMED
|
@@ -48,6 +48,8 @@ var ValueHash;
|
|
|
48
48
|
ByteMarker[ByteMarker["Array"] = 6] = "Array";
|
|
49
49
|
ByteMarker[ByteMarker["Date"] = 7] = "Date";
|
|
50
50
|
ByteMarker[ByteMarker["Uint8Array"] = 8] = "Uint8Array";
|
|
51
|
+
ByteMarker[ByteMarker["Symbol"] = 9] = "Symbol";
|
|
52
|
+
ByteMarker[ByteMarker["BigInt"] = 10] = "BigInt";
|
|
51
53
|
})(ByteMarker || (ByteMarker = {}));
|
|
52
54
|
// ----------------------------------------------------
|
|
53
55
|
// State
|
|
@@ -79,6 +81,12 @@ var ValueHash;
|
|
|
79
81
|
function IsNumber(value) {
|
|
80
82
|
return typeof value === 'number';
|
|
81
83
|
}
|
|
84
|
+
function IsSymbol(value) {
|
|
85
|
+
return typeof value === 'symbol';
|
|
86
|
+
}
|
|
87
|
+
function IsBigInt(value) {
|
|
88
|
+
return typeof value === 'bigint';
|
|
89
|
+
}
|
|
82
90
|
function IsObject(value) {
|
|
83
91
|
return typeof value === 'object' && value !== null && !IsArray(value) && !IsDate(value) && !IsUint8Array(value);
|
|
84
92
|
}
|
|
@@ -92,50 +100,61 @@ var ValueHash;
|
|
|
92
100
|
// Encoding
|
|
93
101
|
// ----------------------------------------------------
|
|
94
102
|
function Array(value) {
|
|
95
|
-
|
|
103
|
+
FNV1A64(ByteMarker.Array);
|
|
96
104
|
for (const item of value) {
|
|
97
105
|
Visit(item);
|
|
98
106
|
}
|
|
99
107
|
}
|
|
100
108
|
function Boolean(value) {
|
|
101
|
-
|
|
102
|
-
|
|
109
|
+
FNV1A64(ByteMarker.Boolean);
|
|
110
|
+
FNV1A64(value ? 1 : 0);
|
|
111
|
+
}
|
|
112
|
+
function BigInt(value) {
|
|
113
|
+
FNV1A64(ByteMarker.BigInt);
|
|
114
|
+
F64In.setBigInt64(0, value);
|
|
115
|
+
for (const byte of F64Out) {
|
|
116
|
+
FNV1A64(byte);
|
|
117
|
+
}
|
|
103
118
|
}
|
|
104
119
|
function Date(value) {
|
|
105
|
-
|
|
120
|
+
FNV1A64(ByteMarker.Date);
|
|
106
121
|
Visit(value.getTime());
|
|
107
122
|
}
|
|
108
123
|
function Null(value) {
|
|
109
|
-
|
|
124
|
+
FNV1A64(ByteMarker.Null);
|
|
110
125
|
}
|
|
111
126
|
function Number(value) {
|
|
112
|
-
|
|
127
|
+
FNV1A64(ByteMarker.Number);
|
|
113
128
|
F64In.setFloat64(0, value);
|
|
114
129
|
for (const byte of F64Out) {
|
|
115
|
-
|
|
130
|
+
FNV1A64(byte);
|
|
116
131
|
}
|
|
117
132
|
}
|
|
118
133
|
function Object(value) {
|
|
119
|
-
|
|
134
|
+
FNV1A64(ByteMarker.Object);
|
|
120
135
|
for (const key of globalThis.Object.keys(value).sort()) {
|
|
121
136
|
Visit(key);
|
|
122
137
|
Visit(value[key]);
|
|
123
138
|
}
|
|
124
139
|
}
|
|
125
140
|
function String(value) {
|
|
126
|
-
|
|
141
|
+
FNV1A64(ByteMarker.String);
|
|
127
142
|
for (let i = 0; i < value.length; i++) {
|
|
128
|
-
|
|
143
|
+
FNV1A64(value.charCodeAt(i));
|
|
129
144
|
}
|
|
130
145
|
}
|
|
146
|
+
function Symbol(value) {
|
|
147
|
+
FNV1A64(ByteMarker.Symbol);
|
|
148
|
+
Visit(value.description);
|
|
149
|
+
}
|
|
131
150
|
function Uint8Array(value) {
|
|
132
|
-
|
|
151
|
+
FNV1A64(ByteMarker.Uint8Array);
|
|
133
152
|
for (let i = 0; i < value.length; i++) {
|
|
134
|
-
|
|
153
|
+
FNV1A64(value[i]);
|
|
135
154
|
}
|
|
136
155
|
}
|
|
137
156
|
function Undefined(value) {
|
|
138
|
-
return
|
|
157
|
+
return FNV1A64(ByteMarker.Undefined);
|
|
139
158
|
}
|
|
140
159
|
function Visit(value) {
|
|
141
160
|
if (IsArray(value)) {
|
|
@@ -144,6 +163,9 @@ var ValueHash;
|
|
|
144
163
|
else if (IsBoolean(value)) {
|
|
145
164
|
Boolean(value);
|
|
146
165
|
}
|
|
166
|
+
else if (IsBigInt(value)) {
|
|
167
|
+
BigInt(value);
|
|
168
|
+
}
|
|
147
169
|
else if (IsDate(value)) {
|
|
148
170
|
Date(value);
|
|
149
171
|
}
|
|
@@ -159,6 +181,9 @@ var ValueHash;
|
|
|
159
181
|
else if (IsString(value)) {
|
|
160
182
|
String(value);
|
|
161
183
|
}
|
|
184
|
+
else if (IsSymbol(value)) {
|
|
185
|
+
Symbol(value);
|
|
186
|
+
}
|
|
162
187
|
else if (IsUint8Array(value)) {
|
|
163
188
|
Uint8Array(value);
|
|
164
189
|
}
|
|
@@ -169,7 +194,7 @@ var ValueHash;
|
|
|
169
194
|
throw new ValueHashError(value);
|
|
170
195
|
}
|
|
171
196
|
}
|
|
172
|
-
function
|
|
197
|
+
function FNV1A64(byte) {
|
|
173
198
|
Hash = Hash ^ Bytes[byte];
|
|
174
199
|
Hash = (Hash * Prime) % Size;
|
|
175
200
|
}
|
package/value/index.d.ts
CHANGED
package/value/index.js
CHANGED
|
@@ -41,9 +41,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
41
41
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
42
42
|
};
|
|
43
43
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
-
exports.Delete = exports.Update = exports.Insert = exports.Edit = exports.ValueErrorType = void 0;
|
|
44
|
+
exports.Delete = exports.Update = exports.Insert = exports.Edit = exports.ValueHash = exports.ValueErrorType = void 0;
|
|
45
45
|
var index_1 = require("../errors/index");
|
|
46
46
|
Object.defineProperty(exports, "ValueErrorType", { enumerable: true, get: function () { return index_1.ValueErrorType; } });
|
|
47
|
+
var hash_1 = require("./hash");
|
|
48
|
+
Object.defineProperty(exports, "ValueHash", { enumerable: true, get: function () { return hash_1.ValueHash; } });
|
|
47
49
|
var delta_1 = require("./delta");
|
|
48
50
|
Object.defineProperty(exports, "Edit", { enumerable: true, get: function () { return delta_1.Edit; } });
|
|
49
51
|
Object.defineProperty(exports, "Insert", { enumerable: true, get: function () { return delta_1.Insert; } });
|
package/value/value.d.ts
CHANGED
|
@@ -3,23 +3,17 @@ import { ValueError } from '../errors/index';
|
|
|
3
3
|
import { Edit } from './delta';
|
|
4
4
|
/** Provides functions to perform structural updates to JavaScript values */
|
|
5
5
|
export declare namespace Value {
|
|
6
|
-
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number, boolean and date values if a reasonable conversion is possible. */
|
|
7
|
-
function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): Types.Static<T>;
|
|
8
6
|
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number, boolean and date values if a reasonable conversion is possible. */
|
|
9
7
|
function Cast<T extends Types.TSchema>(schema: T, value: unknown): Types.Static<T>;
|
|
10
8
|
/** Creates a value from the given type */
|
|
11
|
-
function Create<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R]): Types.Static<T>;
|
|
12
|
-
/** Creates a value from the given type */
|
|
13
9
|
function Create<T extends Types.TSchema>(schema: T): Types.Static<T>;
|
|
14
10
|
/** Returns true if the value matches the given type. */
|
|
15
|
-
function Check<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): value is Types.Static<T>;
|
|
16
|
-
/** Returns true if the value matches the given type. */
|
|
17
11
|
function Check<T extends Types.TSchema>(schema: T, value: unknown): value is Types.Static<T>;
|
|
12
|
+
/** Converts any type mismatched values to their target type if a conversion is possible. */
|
|
13
|
+
function Convert<T extends Types.TSchema>(schema: T, value: unknown): unknown;
|
|
18
14
|
/** Returns a structural clone of the given value */
|
|
19
15
|
function Clone<T>(value: T): T;
|
|
20
16
|
/** Returns an iterator for each error in this value. */
|
|
21
|
-
function Errors<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): IterableIterator<ValueError>;
|
|
22
|
-
/** Returns an iterator for each error in this value. */
|
|
23
17
|
function Errors<T extends Types.TSchema>(schema: T, value: unknown): IterableIterator<ValueError>;
|
|
24
18
|
/** Returns true if left and right values are structurally equal */
|
|
25
19
|
function Equal<T>(left: T, right: unknown): right is T;
|