@sinclair/typebox 0.25.23 → 0.26.0-dev
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 +9 -4
- package/compiler/compiler.js +160 -119
- package/errors/errors.d.ts +56 -46
- package/errors/errors.js +234 -149
- package/package.json +1 -6
- package/readme.md +395 -396
- package/system/system.d.ts +9 -6
- package/system/system.js +17 -17
- package/typebox.d.ts +386 -162
- package/typebox.js +1710 -229
- package/value/cast.d.ts +2 -2
- package/value/cast.js +120 -192
- package/value/check.d.ts +1 -1
- package/value/check.js +162 -107
- 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 -107
- 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/guard.d.ts +0 -60
- package/guard/guard.js +0 -440
- package/guard/index.d.ts +0 -1
- package/guard/index.js +0 -44
- 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,74 +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
|
|
199
|
-
if (
|
|
200
|
-
return schema.default;
|
|
201
|
-
}
|
|
202
|
-
else {
|
|
203
|
-
throw new Error('ValueCreate.Recursive: Recursive types require a default value');
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
function Ref(schema, references) {
|
|
207
|
-
if (schema.default !== undefined) {
|
|
238
|
+
function Ref(schema) {
|
|
239
|
+
if ('default' in schema) {
|
|
208
240
|
return schema.default;
|
|
209
241
|
}
|
|
210
242
|
else {
|
|
211
|
-
|
|
212
|
-
if (reference === undefined)
|
|
213
|
-
throw new Error(`ValueCreate.Ref: Cannot find schema with $id '${schema.$ref}'.`);
|
|
214
|
-
return Visit(reference, references);
|
|
243
|
+
return Visit(Types.ReferenceRegistry.DerefOne(schema));
|
|
215
244
|
}
|
|
216
245
|
}
|
|
217
|
-
function Self(schema
|
|
218
|
-
if (
|
|
246
|
+
function Self(schema) {
|
|
247
|
+
if ('default' in schema) {
|
|
219
248
|
return schema.default;
|
|
220
249
|
}
|
|
221
250
|
else {
|
|
222
|
-
|
|
223
|
-
if (reference === undefined)
|
|
224
|
-
throw new Error(`ValueCreate.Self: Cannot locate schema with $id '${schema.$ref}'`);
|
|
225
|
-
return Visit(reference, references);
|
|
251
|
+
return Visit(Types.ReferenceRegistry.DerefOne(schema));
|
|
226
252
|
}
|
|
227
253
|
}
|
|
228
|
-
function String(schema
|
|
254
|
+
function String(schema) {
|
|
229
255
|
if (schema.pattern !== undefined) {
|
|
230
|
-
if (
|
|
256
|
+
if (!('default' in schema)) {
|
|
231
257
|
throw new Error('ValueCreate.String: String types with patterns must specify a default value');
|
|
232
258
|
}
|
|
233
259
|
else {
|
|
@@ -235,7 +261,7 @@ var ValueCreate;
|
|
|
235
261
|
}
|
|
236
262
|
}
|
|
237
263
|
else if (schema.format !== undefined) {
|
|
238
|
-
if (
|
|
264
|
+
if (!('default' in schema)) {
|
|
239
265
|
throw new Error('ValueCreate.String: String types with formats must specify a default value');
|
|
240
266
|
}
|
|
241
267
|
else {
|
|
@@ -243,7 +269,7 @@ var ValueCreate;
|
|
|
243
269
|
}
|
|
244
270
|
}
|
|
245
271
|
else {
|
|
246
|
-
if (
|
|
272
|
+
if ('default' in schema) {
|
|
247
273
|
return schema.default;
|
|
248
274
|
}
|
|
249
275
|
else if (schema.minLength !== undefined) {
|
|
@@ -256,38 +282,49 @@ var ValueCreate;
|
|
|
256
282
|
}
|
|
257
283
|
}
|
|
258
284
|
}
|
|
259
|
-
function
|
|
260
|
-
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) {
|
|
261
298
|
return schema.default;
|
|
262
299
|
}
|
|
263
300
|
if (schema.items === undefined) {
|
|
264
301
|
return [];
|
|
265
302
|
}
|
|
266
303
|
else {
|
|
267
|
-
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]));
|
|
268
305
|
}
|
|
269
306
|
}
|
|
270
|
-
function Undefined(schema
|
|
271
|
-
if (
|
|
307
|
+
function Undefined(schema) {
|
|
308
|
+
if ('default' in schema) {
|
|
272
309
|
return schema.default;
|
|
273
310
|
}
|
|
274
311
|
else {
|
|
275
312
|
return undefined;
|
|
276
313
|
}
|
|
277
314
|
}
|
|
278
|
-
function Union(schema
|
|
279
|
-
if (
|
|
315
|
+
function Union(schema) {
|
|
316
|
+
if ('default' in schema) {
|
|
280
317
|
return schema.default;
|
|
281
318
|
}
|
|
282
319
|
else if (schema.anyOf.length === 0) {
|
|
283
320
|
throw new Error('ValueCreate.Union: Cannot create Union with zero variants');
|
|
284
321
|
}
|
|
285
322
|
else {
|
|
286
|
-
return ValueCreate.Create(schema.anyOf[0]
|
|
323
|
+
return ValueCreate.Create(schema.anyOf[0]);
|
|
287
324
|
}
|
|
288
325
|
}
|
|
289
|
-
function Uint8Array(schema
|
|
290
|
-
if (
|
|
326
|
+
function Uint8Array(schema) {
|
|
327
|
+
if ('default' in schema) {
|
|
291
328
|
return schema.default;
|
|
292
329
|
}
|
|
293
330
|
else if (schema.minByteLength !== undefined) {
|
|
@@ -297,24 +334,24 @@ var ValueCreate;
|
|
|
297
334
|
return new globalThis.Uint8Array(0);
|
|
298
335
|
}
|
|
299
336
|
}
|
|
300
|
-
function Unknown(schema
|
|
301
|
-
if (
|
|
337
|
+
function Unknown(schema) {
|
|
338
|
+
if ('default' in schema) {
|
|
302
339
|
return schema.default;
|
|
303
340
|
}
|
|
304
341
|
else {
|
|
305
342
|
return {};
|
|
306
343
|
}
|
|
307
344
|
}
|
|
308
|
-
function Void(schema
|
|
309
|
-
if (
|
|
345
|
+
function Void(schema) {
|
|
346
|
+
if ('default' in schema) {
|
|
310
347
|
return schema.default;
|
|
311
348
|
}
|
|
312
349
|
else {
|
|
313
|
-
return
|
|
350
|
+
return void 0;
|
|
314
351
|
}
|
|
315
352
|
}
|
|
316
|
-
function UserDefined(schema
|
|
317
|
-
if (
|
|
353
|
+
function UserDefined(schema) {
|
|
354
|
+
if ('default' in schema) {
|
|
318
355
|
return schema.default;
|
|
319
356
|
}
|
|
320
357
|
else {
|
|
@@ -322,67 +359,72 @@ var ValueCreate;
|
|
|
322
359
|
}
|
|
323
360
|
}
|
|
324
361
|
/** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
|
|
325
|
-
function Visit(schema
|
|
326
|
-
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
362
|
+
function Visit(schema) {
|
|
327
363
|
const anySchema = schema;
|
|
328
364
|
switch (anySchema[Types.Kind]) {
|
|
329
365
|
case 'Any':
|
|
330
|
-
return Any(anySchema
|
|
366
|
+
return Any(anySchema);
|
|
331
367
|
case 'Array':
|
|
332
|
-
return Array(anySchema
|
|
368
|
+
return Array(anySchema);
|
|
369
|
+
case 'BigInt':
|
|
370
|
+
return BigInt(anySchema);
|
|
333
371
|
case 'Boolean':
|
|
334
|
-
return Boolean(anySchema
|
|
372
|
+
return Boolean(anySchema);
|
|
335
373
|
case 'Constructor':
|
|
336
|
-
return Constructor(anySchema
|
|
374
|
+
return Constructor(anySchema);
|
|
337
375
|
case 'Date':
|
|
338
|
-
return Date(anySchema
|
|
376
|
+
return Date(anySchema);
|
|
339
377
|
case 'Function':
|
|
340
|
-
return Function(anySchema
|
|
378
|
+
return Function(anySchema);
|
|
341
379
|
case 'Integer':
|
|
342
|
-
return Integer(anySchema
|
|
380
|
+
return Integer(anySchema);
|
|
381
|
+
case 'Intersect':
|
|
382
|
+
return Intersect(anySchema);
|
|
343
383
|
case 'Literal':
|
|
344
|
-
return Literal(anySchema
|
|
384
|
+
return Literal(anySchema);
|
|
345
385
|
case 'Never':
|
|
346
|
-
return Never(anySchema
|
|
386
|
+
return Never(anySchema);
|
|
387
|
+
case 'Not':
|
|
388
|
+
return Not(anySchema);
|
|
347
389
|
case 'Null':
|
|
348
|
-
return Null(anySchema
|
|
390
|
+
return Null(anySchema);
|
|
349
391
|
case 'Number':
|
|
350
|
-
return Number(anySchema
|
|
392
|
+
return Number(anySchema);
|
|
351
393
|
case 'Object':
|
|
352
|
-
return Object(anySchema
|
|
394
|
+
return Object(anySchema);
|
|
353
395
|
case 'Promise':
|
|
354
|
-
return Promise(anySchema
|
|
396
|
+
return Promise(anySchema);
|
|
355
397
|
case 'Record':
|
|
356
|
-
return Record(anySchema
|
|
357
|
-
case 'Rec':
|
|
358
|
-
return Recursive(anySchema, anyReferences);
|
|
398
|
+
return Record(anySchema);
|
|
359
399
|
case 'Ref':
|
|
360
|
-
return Ref(anySchema
|
|
400
|
+
return Ref(anySchema);
|
|
361
401
|
case 'Self':
|
|
362
|
-
return Self(anySchema
|
|
402
|
+
return Self(anySchema);
|
|
363
403
|
case 'String':
|
|
364
|
-
return String(anySchema
|
|
404
|
+
return String(anySchema);
|
|
405
|
+
case 'Symbol':
|
|
406
|
+
return Symbol(anySchema);
|
|
365
407
|
case 'Tuple':
|
|
366
|
-
return Tuple(anySchema
|
|
408
|
+
return Tuple(anySchema);
|
|
367
409
|
case 'Undefined':
|
|
368
|
-
return Undefined(anySchema
|
|
410
|
+
return Undefined(anySchema);
|
|
369
411
|
case 'Union':
|
|
370
|
-
return Union(anySchema
|
|
412
|
+
return Union(anySchema);
|
|
371
413
|
case 'Uint8Array':
|
|
372
|
-
return Uint8Array(anySchema
|
|
414
|
+
return Uint8Array(anySchema);
|
|
373
415
|
case 'Unknown':
|
|
374
|
-
return Unknown(anySchema
|
|
416
|
+
return Unknown(anySchema);
|
|
375
417
|
case 'Void':
|
|
376
|
-
return Void(anySchema
|
|
418
|
+
return Void(anySchema);
|
|
377
419
|
default:
|
|
378
|
-
if (!
|
|
420
|
+
if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
|
|
379
421
|
throw new ValueCreateUnknownTypeError(anySchema);
|
|
380
|
-
return UserDefined(anySchema
|
|
422
|
+
return UserDefined(anySchema);
|
|
381
423
|
}
|
|
382
424
|
}
|
|
383
425
|
ValueCreate.Visit = Visit;
|
|
384
|
-
function Create(schema
|
|
385
|
-
return Visit(schema
|
|
426
|
+
function Create(schema) {
|
|
427
|
+
return Visit(schema);
|
|
386
428
|
}
|
|
387
429
|
ValueCreate.Create = Create;
|
|
388
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;
|