@peninsula-med/beisen-ehr-plugin 1.0.6 → 1.1.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/dist/index.cjs.js CHANGED
@@ -2,9 +2,3456 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var fs = require('fs');
6
- var path = require('path');
7
- var os = require('os');
5
+ /** Returns true if this value is an async iterator */
6
+ function IsAsyncIterator$2(value) {
7
+ return IsObject$2(value) && !IsArray$2(value) && !IsUint8Array$2(value) && Symbol.asyncIterator in value;
8
+ }
9
+ /** Returns true if this value is an array */
10
+ function IsArray$2(value) {
11
+ return Array.isArray(value);
12
+ }
13
+ /** Returns true if this value is bigint */
14
+ function IsBigInt$2(value) {
15
+ return typeof value === 'bigint';
16
+ }
17
+ /** Returns true if this value is a boolean */
18
+ function IsBoolean$2(value) {
19
+ return typeof value === 'boolean';
20
+ }
21
+ /** Returns true if this value is a Date object */
22
+ function IsDate$2(value) {
23
+ return value instanceof globalThis.Date;
24
+ }
25
+ /** Returns true if this value is a function */
26
+ function IsFunction$2(value) {
27
+ return typeof value === 'function';
28
+ }
29
+ /** Returns true if this value is an iterator */
30
+ function IsIterator$2(value) {
31
+ return IsObject$2(value) && !IsArray$2(value) && !IsUint8Array$2(value) && Symbol.iterator in value;
32
+ }
33
+ /** Returns true if this value is null */
34
+ function IsNull$2(value) {
35
+ return value === null;
36
+ }
37
+ /** Returns true if this value is number */
38
+ function IsNumber$2(value) {
39
+ return typeof value === 'number';
40
+ }
41
+ /** Returns true if this value is an object */
42
+ function IsObject$2(value) {
43
+ return typeof value === 'object' && value !== null;
44
+ }
45
+ /** Returns true if this value is RegExp */
46
+ function IsRegExp$2(value) {
47
+ return value instanceof globalThis.RegExp;
48
+ }
49
+ /** Returns true if this value is string */
50
+ function IsString$2(value) {
51
+ return typeof value === 'string';
52
+ }
53
+ /** Returns true if this value is symbol */
54
+ function IsSymbol$2(value) {
55
+ return typeof value === 'symbol';
56
+ }
57
+ /** Returns true if this value is a Uint8Array */
58
+ function IsUint8Array$2(value) {
59
+ return value instanceof globalThis.Uint8Array;
60
+ }
61
+ /** Returns true if this value is undefined */
62
+ function IsUndefined$2(value) {
63
+ return value === undefined;
64
+ }
65
+
66
+ function ArrayType(value) {
67
+ return value.map((value) => Visit$2(value));
68
+ }
69
+ function DateType(value) {
70
+ return new Date(value.getTime());
71
+ }
72
+ function Uint8ArrayType(value) {
73
+ return new Uint8Array(value);
74
+ }
75
+ function RegExpType(value) {
76
+ return new RegExp(value.source, value.flags);
77
+ }
78
+ function ObjectType(value) {
79
+ const result = {};
80
+ for (const key of Object.getOwnPropertyNames(value)) {
81
+ result[key] = Visit$2(value[key]);
82
+ }
83
+ for (const key of Object.getOwnPropertySymbols(value)) {
84
+ result[key] = Visit$2(value[key]);
85
+ }
86
+ return result;
87
+ }
88
+ // prettier-ignore
89
+ function Visit$2(value) {
90
+ return (IsArray$2(value) ? ArrayType(value) :
91
+ IsDate$2(value) ? DateType(value) :
92
+ IsUint8Array$2(value) ? Uint8ArrayType(value) :
93
+ IsRegExp$2(value) ? RegExpType(value) :
94
+ IsObject$2(value) ? ObjectType(value) :
95
+ value);
96
+ }
97
+ /** Clones a value */
98
+ function Clone(value) {
99
+ return Visit$2(value);
100
+ }
101
+
102
+ /** Clones a Rest */
103
+ function CloneRest(schemas) {
104
+ return schemas.map((schema) => CloneType(schema));
105
+ }
106
+ /** Clones a Type */
107
+ function CloneType(schema, options = {}) {
108
+ return { ...Clone(schema), ...options };
109
+ }
110
+
111
+ /** The base Error type thrown for all TypeBox exceptions */
112
+ class TypeBoxError extends Error {
113
+ constructor(message) {
114
+ super(message);
115
+ }
116
+ }
117
+
118
+ /** Symbol key applied to transform types */
119
+ const TransformKind = Symbol.for('TypeBox.Transform');
120
+ /** Symbol key applied to readonly types */
121
+ const ReadonlyKind = Symbol.for('TypeBox.Readonly');
122
+ /** Symbol key applied to optional types */
123
+ const OptionalKind = Symbol.for('TypeBox.Optional');
124
+ /** Symbol key applied to types */
125
+ const Hint = Symbol.for('TypeBox.Hint');
126
+ /** Symbol key applied to types */
127
+ const Kind = Symbol.for('TypeBox.Kind');
128
+
129
+ /** `[Kind-Only]` Returns true if this value has a Readonly symbol */
130
+ function IsReadonly(value) {
131
+ return IsObject$2(value) && value[ReadonlyKind] === 'Readonly';
132
+ }
133
+ /** `[Kind-Only]` Returns true if this value has a Optional symbol */
134
+ function IsOptional$1(value) {
135
+ return IsObject$2(value) && value[OptionalKind] === 'Optional';
136
+ }
137
+ /** `[Kind-Only]` Returns true if the given value is TAny */
138
+ function IsAny$1(value) {
139
+ return IsKindOf$1(value, 'Any');
140
+ }
141
+ /** `[Kind-Only]` Returns true if the given value is TArray */
142
+ function IsArray$1(value) {
143
+ return IsKindOf$1(value, 'Array');
144
+ }
145
+ /** `[Kind-Only]` Returns true if the given value is TAsyncIterator */
146
+ function IsAsyncIterator$1(value) {
147
+ return IsKindOf$1(value, 'AsyncIterator');
148
+ }
149
+ /** `[Kind-Only]` Returns true if the given value is TBigInt */
150
+ function IsBigInt$1(value) {
151
+ return IsKindOf$1(value, 'BigInt');
152
+ }
153
+ /** `[Kind-Only]` Returns true if the given value is TBoolean */
154
+ function IsBoolean$1(value) {
155
+ return IsKindOf$1(value, 'Boolean');
156
+ }
157
+ /** `[Kind-Only]` Returns true if the given value is TConstructor */
158
+ function IsConstructor$1(value) {
159
+ return IsKindOf$1(value, 'Constructor');
160
+ }
161
+ /** `[Kind-Only]` Returns true if the given value is TDate */
162
+ function IsDate$1(value) {
163
+ return IsKindOf$1(value, 'Date');
164
+ }
165
+ /** `[Kind-Only]` Returns true if the given value is TFunction */
166
+ function IsFunction$1(value) {
167
+ return IsKindOf$1(value, 'Function');
168
+ }
169
+ /** `[Kind-Only]` Returns true if the given value is TInteger */
170
+ function IsInteger$1(value) {
171
+ return IsKindOf$1(value, 'Integer');
172
+ }
173
+ /** `[Kind-Only]` Returns true if the given value is TIntersect */
174
+ function IsIntersect$1(value) {
175
+ return IsKindOf$1(value, 'Intersect');
176
+ }
177
+ /** `[Kind-Only]` Returns true if the given value is TIterator */
178
+ function IsIterator$1(value) {
179
+ return IsKindOf$1(value, 'Iterator');
180
+ }
181
+ /** `[Kind-Only]` Returns true if the given value is a TKind with the given name. */
182
+ function IsKindOf$1(value, kind) {
183
+ return IsObject$2(value) && Kind in value && value[Kind] === kind;
184
+ }
185
+ /** `[Kind-Only]` Returns true if the given value is TLiteral */
186
+ function IsLiteral$1(value) {
187
+ return IsKindOf$1(value, 'Literal');
188
+ }
189
+ /** `[Kind-Only]` Returns true if the given value is a TMappedKey */
190
+ function IsMappedKey$1(value) {
191
+ return IsKindOf$1(value, 'MappedKey');
192
+ }
193
+ /** `[Kind-Only]` Returns true if the given value is TMappedResult */
194
+ function IsMappedResult$1(value) {
195
+ return IsKindOf$1(value, 'MappedResult');
196
+ }
197
+ /** `[Kind-Only]` Returns true if the given value is TNever */
198
+ function IsNever$1(value) {
199
+ return IsKindOf$1(value, 'Never');
200
+ }
201
+ /** `[Kind-Only]` Returns true if the given value is TNot */
202
+ function IsNot$1(value) {
203
+ return IsKindOf$1(value, 'Not');
204
+ }
205
+ /** `[Kind-Only]` Returns true if the given value is TNull */
206
+ function IsNull$1(value) {
207
+ return IsKindOf$1(value, 'Null');
208
+ }
209
+ /** `[Kind-Only]` Returns true if the given value is TNumber */
210
+ function IsNumber$1(value) {
211
+ return IsKindOf$1(value, 'Number');
212
+ }
213
+ /** `[Kind-Only]` Returns true if the given value is TObject */
214
+ function IsObject$1(value) {
215
+ return IsKindOf$1(value, 'Object');
216
+ }
217
+ /** `[Kind-Only]` Returns true if the given value is TPromise */
218
+ function IsPromise$1(value) {
219
+ return IsKindOf$1(value, 'Promise');
220
+ }
221
+ /** `[Kind-Only]` Returns true if the given value is TRecord */
222
+ function IsRecord$1(value) {
223
+ return IsKindOf$1(value, 'Record');
224
+ }
225
+ /** `[Kind-Only]` Returns true if the given value is TRef */
226
+ function IsRef$1(value) {
227
+ return IsKindOf$1(value, 'Ref');
228
+ }
229
+ /** `[Kind-Only]` Returns true if the given value is TRegExp */
230
+ function IsRegExp$1(value) {
231
+ return IsKindOf$1(value, 'RegExp');
232
+ }
233
+ /** `[Kind-Only]` Returns true if the given value is TString */
234
+ function IsString$1(value) {
235
+ return IsKindOf$1(value, 'String');
236
+ }
237
+ /** `[Kind-Only]` Returns true if the given value is TSymbol */
238
+ function IsSymbol$1(value) {
239
+ return IsKindOf$1(value, 'Symbol');
240
+ }
241
+ /** `[Kind-Only]` Returns true if the given value is TTemplateLiteral */
242
+ function IsTemplateLiteral$1(value) {
243
+ return IsKindOf$1(value, 'TemplateLiteral');
244
+ }
245
+ /** `[Kind-Only]` Returns true if the given value is TThis */
246
+ function IsThis$1(value) {
247
+ return IsKindOf$1(value, 'This');
248
+ }
249
+ /** `[Kind-Only]` Returns true of this value is TTransform */
250
+ function IsTransform$1(value) {
251
+ return IsObject$2(value) && TransformKind in value;
252
+ }
253
+ /** `[Kind-Only]` Returns true if the given value is TTuple */
254
+ function IsTuple$1(value) {
255
+ return IsKindOf$1(value, 'Tuple');
256
+ }
257
+ /** `[Kind-Only]` Returns true if the given value is TUndefined */
258
+ function IsUndefined$1(value) {
259
+ return IsKindOf$1(value, 'Undefined');
260
+ }
261
+ /** `[Kind-Only]` Returns true if the given value is TUnion */
262
+ function IsUnion$1(value) {
263
+ return IsKindOf$1(value, 'Union');
264
+ }
265
+ /** `[Kind-Only]` Returns true if the given value is TUint8Array */
266
+ function IsUint8Array$1(value) {
267
+ return IsKindOf$1(value, 'Uint8Array');
268
+ }
269
+ /** `[Kind-Only]` Returns true if the given value is TUnknown */
270
+ function IsUnknown$1(value) {
271
+ return IsKindOf$1(value, 'Unknown');
272
+ }
273
+ /** `[Kind-Only]` Returns true if the given value is a raw TUnsafe */
274
+ function IsUnsafe$1(value) {
275
+ return IsKindOf$1(value, 'Unsafe');
276
+ }
277
+ /** `[Kind-Only]` Returns true if the given value is TVoid */
278
+ function IsVoid$1(value) {
279
+ return IsKindOf$1(value, 'Void');
280
+ }
281
+ /** `[Kind-Only]` Returns true if the given value is TKind */
282
+ function IsKind$1(value) {
283
+ return IsObject$2(value) && Kind in value && IsString$2(value[Kind]);
284
+ }
285
+ /** `[Kind-Only]` Returns true if the given value is TSchema */
286
+ function IsSchema$1(value) {
287
+ // prettier-ignore
288
+ return (IsAny$1(value) ||
289
+ IsArray$1(value) ||
290
+ IsBoolean$1(value) ||
291
+ IsBigInt$1(value) ||
292
+ IsAsyncIterator$1(value) ||
293
+ IsConstructor$1(value) ||
294
+ IsDate$1(value) ||
295
+ IsFunction$1(value) ||
296
+ IsInteger$1(value) ||
297
+ IsIntersect$1(value) ||
298
+ IsIterator$1(value) ||
299
+ IsLiteral$1(value) ||
300
+ IsMappedKey$1(value) ||
301
+ IsMappedResult$1(value) ||
302
+ IsNever$1(value) ||
303
+ IsNot$1(value) ||
304
+ IsNull$1(value) ||
305
+ IsNumber$1(value) ||
306
+ IsObject$1(value) ||
307
+ IsPromise$1(value) ||
308
+ IsRecord$1(value) ||
309
+ IsRef$1(value) ||
310
+ IsRegExp$1(value) ||
311
+ IsString$1(value) ||
312
+ IsSymbol$1(value) ||
313
+ IsTemplateLiteral$1(value) ||
314
+ IsThis$1(value) ||
315
+ IsTuple$1(value) ||
316
+ IsUndefined$1(value) ||
317
+ IsUnion$1(value) ||
318
+ IsUint8Array$1(value) ||
319
+ IsUnknown$1(value) ||
320
+ IsUnsafe$1(value) ||
321
+ IsVoid$1(value) ||
322
+ IsKind$1(value));
323
+ }
324
+
325
+ const KnownTypes = [
326
+ 'Any',
327
+ 'Array',
328
+ 'AsyncIterator',
329
+ 'BigInt',
330
+ 'Boolean',
331
+ 'Constructor',
332
+ 'Date',
333
+ 'Enum',
334
+ 'Function',
335
+ 'Integer',
336
+ 'Intersect',
337
+ 'Iterator',
338
+ 'Literal',
339
+ 'MappedKey',
340
+ 'MappedResult',
341
+ 'Not',
342
+ 'Null',
343
+ 'Number',
344
+ 'Object',
345
+ 'Promise',
346
+ 'Record',
347
+ 'Ref',
348
+ 'RegExp',
349
+ 'String',
350
+ 'Symbol',
351
+ 'TemplateLiteral',
352
+ 'This',
353
+ 'Tuple',
354
+ 'Undefined',
355
+ 'Union',
356
+ 'Uint8Array',
357
+ 'Unknown',
358
+ 'Void',
359
+ ];
360
+ function IsPattern(value) {
361
+ try {
362
+ new RegExp(value);
363
+ return true;
364
+ }
365
+ catch {
366
+ return false;
367
+ }
368
+ }
369
+ function IsControlCharacterFree(value) {
370
+ if (!IsString$2(value))
371
+ return false;
372
+ for (let i = 0; i < value.length; i++) {
373
+ const code = value.charCodeAt(i);
374
+ if ((code >= 7 && code <= 13) || code === 27 || code === 127) {
375
+ return false;
376
+ }
377
+ }
378
+ return true;
379
+ }
380
+ function IsAdditionalProperties(value) {
381
+ return IsOptionalBoolean(value) || IsSchema(value);
382
+ }
383
+ function IsOptionalBigInt(value) {
384
+ return IsUndefined$2(value) || IsBigInt$2(value);
385
+ }
386
+ function IsOptionalNumber(value) {
387
+ return IsUndefined$2(value) || IsNumber$2(value);
388
+ }
389
+ function IsOptionalBoolean(value) {
390
+ return IsUndefined$2(value) || IsBoolean$2(value);
391
+ }
392
+ function IsOptionalString(value) {
393
+ return IsUndefined$2(value) || IsString$2(value);
394
+ }
395
+ function IsOptionalPattern(value) {
396
+ return IsUndefined$2(value) || (IsString$2(value) && IsControlCharacterFree(value) && IsPattern(value));
397
+ }
398
+ function IsOptionalFormat(value) {
399
+ return IsUndefined$2(value) || (IsString$2(value) && IsControlCharacterFree(value));
400
+ }
401
+ function IsOptionalSchema(value) {
402
+ return IsUndefined$2(value) || IsSchema(value);
403
+ }
404
+ /** Returns true if this value has a Optional symbol */
405
+ function IsOptional(value) {
406
+ return IsObject$2(value) && value[OptionalKind] === 'Optional';
407
+ }
408
+ // ------------------------------------------------------------------
409
+ // Types
410
+ // ------------------------------------------------------------------
411
+ /** Returns true if the given value is TAny */
412
+ function IsAny(value) {
413
+ // prettier-ignore
414
+ return (IsKindOf(value, 'Any') &&
415
+ IsOptionalString(value.$id));
416
+ }
417
+ /** Returns true if the given value is TArray */
418
+ function IsArray(value) {
419
+ return (IsKindOf(value, 'Array') &&
420
+ value.type === 'array' &&
421
+ IsOptionalString(value.$id) &&
422
+ IsSchema(value.items) &&
423
+ IsOptionalNumber(value.minItems) &&
424
+ IsOptionalNumber(value.maxItems) &&
425
+ IsOptionalBoolean(value.uniqueItems) &&
426
+ IsOptionalSchema(value.contains) &&
427
+ IsOptionalNumber(value.minContains) &&
428
+ IsOptionalNumber(value.maxContains));
429
+ }
430
+ /** Returns true if the given value is TAsyncIterator */
431
+ function IsAsyncIterator(value) {
432
+ // prettier-ignore
433
+ return (IsKindOf(value, 'AsyncIterator') &&
434
+ value.type === 'AsyncIterator' &&
435
+ IsOptionalString(value.$id) &&
436
+ IsSchema(value.items));
437
+ }
438
+ /** Returns true if the given value is TBigInt */
439
+ function IsBigInt(value) {
440
+ // prettier-ignore
441
+ return (IsKindOf(value, 'BigInt') &&
442
+ value.type === 'bigint' &&
443
+ IsOptionalString(value.$id) &&
444
+ IsOptionalBigInt(value.exclusiveMaximum) &&
445
+ IsOptionalBigInt(value.exclusiveMinimum) &&
446
+ IsOptionalBigInt(value.maximum) &&
447
+ IsOptionalBigInt(value.minimum) &&
448
+ IsOptionalBigInt(value.multipleOf));
449
+ }
450
+ /** Returns true if the given value is TBoolean */
451
+ function IsBoolean(value) {
452
+ // prettier-ignore
453
+ return (IsKindOf(value, 'Boolean') &&
454
+ value.type === 'boolean' &&
455
+ IsOptionalString(value.$id));
456
+ }
457
+ /** Returns true if the given value is TConstructor */
458
+ function IsConstructor(value) {
459
+ // prettier-ignore
460
+ return (IsKindOf(value, 'Constructor') &&
461
+ value.type === 'Constructor' &&
462
+ IsOptionalString(value.$id) &&
463
+ IsArray$2(value.parameters) &&
464
+ value.parameters.every(schema => IsSchema(schema)) &&
465
+ IsSchema(value.returns));
466
+ }
467
+ /** Returns true if the given value is TDate */
468
+ function IsDate(value) {
469
+ return (IsKindOf(value, 'Date') &&
470
+ value.type === 'Date' &&
471
+ IsOptionalString(value.$id) &&
472
+ IsOptionalNumber(value.exclusiveMaximumTimestamp) &&
473
+ IsOptionalNumber(value.exclusiveMinimumTimestamp) &&
474
+ IsOptionalNumber(value.maximumTimestamp) &&
475
+ IsOptionalNumber(value.minimumTimestamp) &&
476
+ IsOptionalNumber(value.multipleOfTimestamp));
477
+ }
478
+ /** Returns true if the given value is TFunction */
479
+ function IsFunction(value) {
480
+ // prettier-ignore
481
+ return (IsKindOf(value, 'Function') &&
482
+ value.type === 'Function' &&
483
+ IsOptionalString(value.$id) &&
484
+ IsArray$2(value.parameters) &&
485
+ value.parameters.every(schema => IsSchema(schema)) &&
486
+ IsSchema(value.returns));
487
+ }
488
+ /** Returns true if the given value is TInteger */
489
+ function IsInteger(value) {
490
+ return (IsKindOf(value, 'Integer') &&
491
+ value.type === 'integer' &&
492
+ IsOptionalString(value.$id) &&
493
+ IsOptionalNumber(value.exclusiveMaximum) &&
494
+ IsOptionalNumber(value.exclusiveMinimum) &&
495
+ IsOptionalNumber(value.maximum) &&
496
+ IsOptionalNumber(value.minimum) &&
497
+ IsOptionalNumber(value.multipleOf));
498
+ }
499
+ /** Returns true if the given schema is TProperties */
500
+ function IsProperties(value) {
501
+ // prettier-ignore
502
+ return (IsObject$2(value) &&
503
+ Object.entries(value).every(([key, schema]) => IsControlCharacterFree(key) && IsSchema(schema)));
504
+ }
505
+ /** Returns true if the given value is TIntersect */
506
+ function IsIntersect(value) {
507
+ // prettier-ignore
508
+ return (IsKindOf(value, 'Intersect') &&
509
+ (IsString$2(value.type) && value.type !== 'object' ? false : true) &&
510
+ IsArray$2(value.allOf) &&
511
+ value.allOf.every(schema => IsSchema(schema) && !IsTransform(schema)) &&
512
+ IsOptionalString(value.type) &&
513
+ (IsOptionalBoolean(value.unevaluatedProperties) || IsOptionalSchema(value.unevaluatedProperties)) &&
514
+ IsOptionalString(value.$id));
515
+ }
516
+ /** Returns true if the given value is TIterator */
517
+ function IsIterator(value) {
518
+ // prettier-ignore
519
+ return (IsKindOf(value, 'Iterator') &&
520
+ value.type === 'Iterator' &&
521
+ IsOptionalString(value.$id) &&
522
+ IsSchema(value.items));
523
+ }
524
+ /** Returns true if the given value is a TKind with the given name. */
525
+ function IsKindOf(value, kind) {
526
+ return IsObject$2(value) && Kind in value && value[Kind] === kind;
527
+ }
528
+ /** Returns true if the given value is TLiteral<string> */
529
+ function IsLiteralString(value) {
530
+ return IsLiteral(value) && IsString$2(value.const);
531
+ }
532
+ /** Returns true if the given value is TLiteral<number> */
533
+ function IsLiteralNumber(value) {
534
+ return IsLiteral(value) && IsNumber$2(value.const);
535
+ }
536
+ /** Returns true if the given value is TLiteral<boolean> */
537
+ function IsLiteralBoolean(value) {
538
+ return IsLiteral(value) && IsBoolean$2(value.const);
539
+ }
540
+ /** Returns true if the given value is TLiteral */
541
+ function IsLiteral(value) {
542
+ // prettier-ignore
543
+ return (IsKindOf(value, 'Literal') &&
544
+ IsOptionalString(value.$id) && IsLiteralValue(value.const));
545
+ }
546
+ /** Returns true if the given value is a TLiteralValue */
547
+ function IsLiteralValue(value) {
548
+ return IsBoolean$2(value) || IsNumber$2(value) || IsString$2(value);
549
+ }
550
+ /** Returns true if the given value is a TMappedKey */
551
+ function IsMappedKey(value) {
552
+ // prettier-ignore
553
+ return (IsKindOf(value, 'MappedKey') &&
554
+ IsArray$2(value.keys) &&
555
+ value.keys.every(key => IsNumber$2(key) || IsString$2(key)));
556
+ }
557
+ /** Returns true if the given value is TMappedResult */
558
+ function IsMappedResult(value) {
559
+ // prettier-ignore
560
+ return (IsKindOf(value, 'MappedResult') &&
561
+ IsProperties(value.properties));
562
+ }
563
+ /** Returns true if the given value is TNever */
564
+ function IsNever(value) {
565
+ // prettier-ignore
566
+ return (IsKindOf(value, 'Never') &&
567
+ IsObject$2(value.not) &&
568
+ Object.getOwnPropertyNames(value.not).length === 0);
569
+ }
570
+ /** Returns true if the given value is TNot */
571
+ function IsNot(value) {
572
+ // prettier-ignore
573
+ return (IsKindOf(value, 'Not') &&
574
+ IsSchema(value.not));
575
+ }
576
+ /** Returns true if the given value is TNull */
577
+ function IsNull(value) {
578
+ // prettier-ignore
579
+ return (IsKindOf(value, 'Null') &&
580
+ value.type === 'null' &&
581
+ IsOptionalString(value.$id));
582
+ }
583
+ /** Returns true if the given value is TNumber */
584
+ function IsNumber(value) {
585
+ return (IsKindOf(value, 'Number') &&
586
+ value.type === 'number' &&
587
+ IsOptionalString(value.$id) &&
588
+ IsOptionalNumber(value.exclusiveMaximum) &&
589
+ IsOptionalNumber(value.exclusiveMinimum) &&
590
+ IsOptionalNumber(value.maximum) &&
591
+ IsOptionalNumber(value.minimum) &&
592
+ IsOptionalNumber(value.multipleOf));
593
+ }
594
+ /** Returns true if the given value is TObject */
595
+ function IsObject(value) {
596
+ // prettier-ignore
597
+ return (IsKindOf(value, 'Object') &&
598
+ value.type === 'object' &&
599
+ IsOptionalString(value.$id) &&
600
+ IsProperties(value.properties) &&
601
+ IsAdditionalProperties(value.additionalProperties) &&
602
+ IsOptionalNumber(value.minProperties) &&
603
+ IsOptionalNumber(value.maxProperties));
604
+ }
605
+ /** Returns true if the given value is TPromise */
606
+ function IsPromise(value) {
607
+ // prettier-ignore
608
+ return (IsKindOf(value, 'Promise') &&
609
+ value.type === 'Promise' &&
610
+ IsOptionalString(value.$id) &&
611
+ IsSchema(value.item));
612
+ }
613
+ /** Returns true if the given value is TRecord */
614
+ function IsRecord(value) {
615
+ // prettier-ignore
616
+ return (IsKindOf(value, 'Record') &&
617
+ value.type === 'object' &&
618
+ IsOptionalString(value.$id) &&
619
+ IsAdditionalProperties(value.additionalProperties) &&
620
+ IsObject$2(value.patternProperties) &&
621
+ ((schema) => {
622
+ const keys = Object.getOwnPropertyNames(schema.patternProperties);
623
+ return (keys.length === 1 &&
624
+ IsPattern(keys[0]) &&
625
+ IsObject$2(schema.patternProperties) &&
626
+ IsSchema(schema.patternProperties[keys[0]]));
627
+ })(value));
628
+ }
629
+ /** Returns true if the given value is TRef */
630
+ function IsRef(value) {
631
+ // prettier-ignore
632
+ return (IsKindOf(value, 'Ref') &&
633
+ IsOptionalString(value.$id) &&
634
+ IsString$2(value.$ref));
635
+ }
636
+ /** Returns true if the given value is TRegExp */
637
+ function IsRegExp(value) {
638
+ // prettier-ignore
639
+ return (IsKindOf(value, 'RegExp') &&
640
+ IsOptionalString(value.$id) &&
641
+ IsString$2(value.source) &&
642
+ IsString$2(value.flags) &&
643
+ IsOptionalNumber(value.maxLength) &&
644
+ IsOptionalNumber(value.minLength));
645
+ }
646
+ /** Returns true if the given value is TString */
647
+ function IsString(value) {
648
+ // prettier-ignore
649
+ return (IsKindOf(value, 'String') &&
650
+ value.type === 'string' &&
651
+ IsOptionalString(value.$id) &&
652
+ IsOptionalNumber(value.minLength) &&
653
+ IsOptionalNumber(value.maxLength) &&
654
+ IsOptionalPattern(value.pattern) &&
655
+ IsOptionalFormat(value.format));
656
+ }
657
+ /** Returns true if the given value is TSymbol */
658
+ function IsSymbol(value) {
659
+ // prettier-ignore
660
+ return (IsKindOf(value, 'Symbol') &&
661
+ value.type === 'symbol' &&
662
+ IsOptionalString(value.$id));
663
+ }
664
+ /** Returns true if the given value is TTemplateLiteral */
665
+ function IsTemplateLiteral(value) {
666
+ // prettier-ignore
667
+ return (IsKindOf(value, 'TemplateLiteral') &&
668
+ value.type === 'string' &&
669
+ IsString$2(value.pattern) &&
670
+ value.pattern[0] === '^' &&
671
+ value.pattern[value.pattern.length - 1] === '$');
672
+ }
673
+ /** Returns true if the given value is TThis */
674
+ function IsThis(value) {
675
+ // prettier-ignore
676
+ return (IsKindOf(value, 'This') &&
677
+ IsOptionalString(value.$id) &&
678
+ IsString$2(value.$ref));
679
+ }
680
+ /** Returns true of this value is TTransform */
681
+ function IsTransform(value) {
682
+ return IsObject$2(value) && TransformKind in value;
683
+ }
684
+ /** Returns true if the given value is TTuple */
685
+ function IsTuple(value) {
686
+ // prettier-ignore
687
+ return (IsKindOf(value, 'Tuple') &&
688
+ value.type === 'array' &&
689
+ IsOptionalString(value.$id) &&
690
+ IsNumber$2(value.minItems) &&
691
+ IsNumber$2(value.maxItems) &&
692
+ value.minItems === value.maxItems &&
693
+ (( // empty
694
+ IsUndefined$2(value.items) &&
695
+ IsUndefined$2(value.additionalItems) &&
696
+ value.minItems === 0) || (IsArray$2(value.items) &&
697
+ value.items.every(schema => IsSchema(schema)))));
698
+ }
699
+ /** Returns true if the given value is TUndefined */
700
+ function IsUndefined(value) {
701
+ // prettier-ignore
702
+ return (IsKindOf(value, 'Undefined') &&
703
+ value.type === 'undefined' &&
704
+ IsOptionalString(value.$id));
705
+ }
706
+ /** Returns true if the given value is TUnion */
707
+ function IsUnion(value) {
708
+ // prettier-ignore
709
+ return (IsKindOf(value, 'Union') &&
710
+ IsOptionalString(value.$id) &&
711
+ IsObject$2(value) &&
712
+ IsArray$2(value.anyOf) &&
713
+ value.anyOf.every(schema => IsSchema(schema)));
714
+ }
715
+ /** Returns true if the given value is TUint8Array */
716
+ function IsUint8Array(value) {
717
+ // prettier-ignore
718
+ return (IsKindOf(value, 'Uint8Array') &&
719
+ value.type === 'Uint8Array' &&
720
+ IsOptionalString(value.$id) &&
721
+ IsOptionalNumber(value.minByteLength) &&
722
+ IsOptionalNumber(value.maxByteLength));
723
+ }
724
+ /** Returns true if the given value is TUnknown */
725
+ function IsUnknown(value) {
726
+ // prettier-ignore
727
+ return (IsKindOf(value, 'Unknown') &&
728
+ IsOptionalString(value.$id));
729
+ }
730
+ /** Returns true if the given value is a raw TUnsafe */
731
+ function IsUnsafe(value) {
732
+ return IsKindOf(value, 'Unsafe');
733
+ }
734
+ /** Returns true if the given value is TVoid */
735
+ function IsVoid(value) {
736
+ // prettier-ignore
737
+ return (IsKindOf(value, 'Void') &&
738
+ value.type === 'void' &&
739
+ IsOptionalString(value.$id));
740
+ }
741
+ /** Returns true if the given value is TKind */
742
+ function IsKind(value) {
743
+ return IsObject$2(value) && Kind in value && IsString$2(value[Kind]) && !KnownTypes.includes(value[Kind]);
744
+ }
745
+ /** Returns true if the given value is TSchema */
746
+ function IsSchema(value) {
747
+ // prettier-ignore
748
+ return (IsObject$2(value)) && (IsAny(value) ||
749
+ IsArray(value) ||
750
+ IsBoolean(value) ||
751
+ IsBigInt(value) ||
752
+ IsAsyncIterator(value) ||
753
+ IsConstructor(value) ||
754
+ IsDate(value) ||
755
+ IsFunction(value) ||
756
+ IsInteger(value) ||
757
+ IsIntersect(value) ||
758
+ IsIterator(value) ||
759
+ IsLiteral(value) ||
760
+ IsMappedKey(value) ||
761
+ IsMappedResult(value) ||
762
+ IsNever(value) ||
763
+ IsNot(value) ||
764
+ IsNull(value) ||
765
+ IsNumber(value) ||
766
+ IsObject(value) ||
767
+ IsPromise(value) ||
768
+ IsRecord(value) ||
769
+ IsRef(value) ||
770
+ IsRegExp(value) ||
771
+ IsString(value) ||
772
+ IsSymbol(value) ||
773
+ IsTemplateLiteral(value) ||
774
+ IsThis(value) ||
775
+ IsTuple(value) ||
776
+ IsUndefined(value) ||
777
+ IsUnion(value) ||
778
+ IsUint8Array(value) ||
779
+ IsUnknown(value) ||
780
+ IsUnsafe(value) ||
781
+ IsVoid(value) ||
782
+ IsKind(value));
783
+ }
784
+
785
+ const PatternBoolean = '(true|false)';
786
+ const PatternNumber = '(0|[1-9][0-9]*)';
787
+ const PatternString = '(.*)';
788
+ const PatternNever = '(?!.*)';
789
+ const PatternNumberExact = `^${PatternNumber}$`;
790
+ const PatternStringExact = `^${PatternString}$`;
791
+ const PatternNeverExact = `^${PatternNever}$`;
792
+
793
+ /** Returns true if element right is in the set of left */
794
+ // prettier-ignore
795
+ function SetIncludes(T, S) {
796
+ return T.includes(S);
797
+ }
798
+ /** Returns a distinct set of elements */
799
+ function SetDistinct(T) {
800
+ return [...new Set(T)];
801
+ }
802
+ /** Returns the Intersect of the given sets */
803
+ function SetIntersect(T, S) {
804
+ return T.filter((L) => S.includes(L));
805
+ }
806
+ // prettier-ignore
807
+ function SetIntersectManyResolve(T, Init) {
808
+ return T.reduce((Acc, L) => {
809
+ return SetIntersect(Acc, L);
810
+ }, Init);
811
+ }
812
+ // prettier-ignore
813
+ function SetIntersectMany(T) {
814
+ return (T.length === 1
815
+ ? T[0]
816
+ // Use left to initialize the accumulator for resolve
817
+ : T.length > 1
818
+ ? SetIntersectManyResolve(T.slice(1), T[0])
819
+ : []);
820
+ }
821
+ /** Returns the Union of multiple sets */
822
+ function SetUnionMany(T) {
823
+ const Acc = [];
824
+ for (const L of T)
825
+ Acc.push(...L);
826
+ return Acc;
827
+ }
828
+
829
+ /** `[Json]` Creates an Any type */
830
+ function Any(options = {}) {
831
+ return { ...options, [Kind]: 'Any' };
832
+ }
833
+
834
+ /** `[Json]` Creates an Array type */
835
+ function Array$1(schema, options = {}) {
836
+ return {
837
+ ...options,
838
+ [Kind]: 'Array',
839
+ type: 'array',
840
+ items: CloneType(schema),
841
+ };
842
+ }
843
+
844
+ /** `[JavaScript]` Creates a AsyncIterator type */
845
+ function AsyncIterator(items, options = {}) {
846
+ return {
847
+ ...options,
848
+ [Kind]: 'AsyncIterator',
849
+ type: 'AsyncIterator',
850
+ items: CloneType(items),
851
+ };
852
+ }
853
+
854
+ function DiscardKey(value, key) {
855
+ const { [key]: _, ...rest } = value;
856
+ return rest;
857
+ }
858
+ function Discard(value, keys) {
859
+ return keys.reduce((acc, key) => DiscardKey(acc, key), value);
860
+ }
861
+
862
+ /** `[Json]` Creates a Never type */
863
+ function Never(options = {}) {
864
+ return {
865
+ ...options,
866
+ [Kind]: 'Never',
867
+ not: {},
868
+ };
869
+ }
870
+
871
+ // prettier-ignore
872
+ function MappedResult(properties) {
873
+ return {
874
+ [Kind]: 'MappedResult',
875
+ properties
876
+ };
877
+ }
878
+
879
+ /** `[JavaScript]` Creates a Constructor type */
880
+ function Constructor(parameters, returns, options) {
881
+ return {
882
+ ...options,
883
+ [Kind]: 'Constructor',
884
+ type: 'Constructor',
885
+ parameters: CloneRest(parameters),
886
+ returns: CloneType(returns),
887
+ };
888
+ }
889
+
890
+ /** `[JavaScript]` Creates a Function type */
891
+ function Function(parameters, returns, options) {
892
+ return {
893
+ ...options,
894
+ [Kind]: 'Function',
895
+ type: 'Function',
896
+ parameters: CloneRest(parameters),
897
+ returns: CloneType(returns),
898
+ };
899
+ }
900
+
901
+ function UnionCreate(T, options) {
902
+ return { ...options, [Kind]: 'Union', anyOf: CloneRest(T) };
903
+ }
904
+
905
+ // prettier-ignore
906
+ function IsUnionOptional(T) {
907
+ return T.some(L => IsOptional$1(L));
908
+ }
909
+ // prettier-ignore
910
+ function RemoveOptionalFromRest$1(T) {
911
+ return T.map(L => IsOptional$1(L) ? RemoveOptionalFromType$1(L) : L);
912
+ }
913
+ // prettier-ignore
914
+ function RemoveOptionalFromType$1(T) {
915
+ return (Discard(T, [OptionalKind]));
916
+ }
917
+ // prettier-ignore
918
+ function ResolveUnion(T, options) {
919
+ return (IsUnionOptional(T)
920
+ ? Optional(UnionCreate(RemoveOptionalFromRest$1(T), options))
921
+ : UnionCreate(RemoveOptionalFromRest$1(T), options));
922
+ }
923
+ /** `[Json]` Creates an evaluated Union type */
924
+ function UnionEvaluated(T, options = {}) {
925
+ // prettier-ignore
926
+ return (T.length === 0 ? Never(options) :
927
+ T.length === 1 ? CloneType(T[0], options) :
928
+ ResolveUnion(T, options));
929
+ }
930
+
931
+ /** `[Json]` Creates a Union type */
932
+ function Union(T, options = {}) {
933
+ // prettier-ignore
934
+ return (T.length === 0 ? Never(options) :
935
+ T.length === 1 ? CloneType(T[0], options) :
936
+ UnionCreate(T, options));
937
+ }
938
+
939
+ // ------------------------------------------------------------------
940
+ // TemplateLiteralParserError
941
+ // ------------------------------------------------------------------
942
+ class TemplateLiteralParserError extends TypeBoxError {
943
+ }
944
+ // -------------------------------------------------------------------
945
+ // Unescape
946
+ //
947
+ // Unescape for these control characters specifically. Note that this
948
+ // function is only called on non union group content, and where we
949
+ // still want to allow the user to embed control characters in that
950
+ // content. For review.
951
+ // -------------------------------------------------------------------
952
+ // prettier-ignore
953
+ function Unescape(pattern) {
954
+ return pattern
955
+ .replace(/\\\$/g, '$')
956
+ .replace(/\\\*/g, '*')
957
+ .replace(/\\\^/g, '^')
958
+ .replace(/\\\|/g, '|')
959
+ .replace(/\\\(/g, '(')
960
+ .replace(/\\\)/g, ')');
961
+ }
962
+ // -------------------------------------------------------------------
963
+ // Control Characters
964
+ // -------------------------------------------------------------------
965
+ function IsNonEscaped(pattern, index, char) {
966
+ return pattern[index] === char && pattern.charCodeAt(index - 1) !== 92;
967
+ }
968
+ function IsOpenParen(pattern, index) {
969
+ return IsNonEscaped(pattern, index, '(');
970
+ }
971
+ function IsCloseParen(pattern, index) {
972
+ return IsNonEscaped(pattern, index, ')');
973
+ }
974
+ function IsSeparator(pattern, index) {
975
+ return IsNonEscaped(pattern, index, '|');
976
+ }
977
+ // -------------------------------------------------------------------
978
+ // Control Groups
979
+ // -------------------------------------------------------------------
980
+ function IsGroup(pattern) {
981
+ if (!(IsOpenParen(pattern, 0) && IsCloseParen(pattern, pattern.length - 1)))
982
+ return false;
983
+ let count = 0;
984
+ for (let index = 0; index < pattern.length; index++) {
985
+ if (IsOpenParen(pattern, index))
986
+ count += 1;
987
+ if (IsCloseParen(pattern, index))
988
+ count -= 1;
989
+ if (count === 0 && index !== pattern.length - 1)
990
+ return false;
991
+ }
992
+ return true;
993
+ }
994
+ // prettier-ignore
995
+ function InGroup(pattern) {
996
+ return pattern.slice(1, pattern.length - 1);
997
+ }
998
+ // prettier-ignore
999
+ function IsPrecedenceOr(pattern) {
1000
+ let count = 0;
1001
+ for (let index = 0; index < pattern.length; index++) {
1002
+ if (IsOpenParen(pattern, index))
1003
+ count += 1;
1004
+ if (IsCloseParen(pattern, index))
1005
+ count -= 1;
1006
+ if (IsSeparator(pattern, index) && count === 0)
1007
+ return true;
1008
+ }
1009
+ return false;
1010
+ }
1011
+ // prettier-ignore
1012
+ function IsPrecedenceAnd(pattern) {
1013
+ for (let index = 0; index < pattern.length; index++) {
1014
+ if (IsOpenParen(pattern, index))
1015
+ return true;
1016
+ }
1017
+ return false;
1018
+ }
1019
+ // prettier-ignore
1020
+ function Or(pattern) {
1021
+ let [count, start] = [0, 0];
1022
+ const expressions = [];
1023
+ for (let index = 0; index < pattern.length; index++) {
1024
+ if (IsOpenParen(pattern, index))
1025
+ count += 1;
1026
+ if (IsCloseParen(pattern, index))
1027
+ count -= 1;
1028
+ if (IsSeparator(pattern, index) && count === 0) {
1029
+ const range = pattern.slice(start, index);
1030
+ if (range.length > 0)
1031
+ expressions.push(TemplateLiteralParse(range));
1032
+ start = index + 1;
1033
+ }
1034
+ }
1035
+ const range = pattern.slice(start);
1036
+ if (range.length > 0)
1037
+ expressions.push(TemplateLiteralParse(range));
1038
+ if (expressions.length === 0)
1039
+ return { type: 'const', const: '' };
1040
+ if (expressions.length === 1)
1041
+ return expressions[0];
1042
+ return { type: 'or', expr: expressions };
1043
+ }
1044
+ // prettier-ignore
1045
+ function And(pattern) {
1046
+ function Group(value, index) {
1047
+ if (!IsOpenParen(value, index))
1048
+ throw new TemplateLiteralParserError(`TemplateLiteralParser: Index must point to open parens`);
1049
+ let count = 0;
1050
+ for (let scan = index; scan < value.length; scan++) {
1051
+ if (IsOpenParen(value, scan))
1052
+ count += 1;
1053
+ if (IsCloseParen(value, scan))
1054
+ count -= 1;
1055
+ if (count === 0)
1056
+ return [index, scan];
1057
+ }
1058
+ throw new TemplateLiteralParserError(`TemplateLiteralParser: Unclosed group parens in expression`);
1059
+ }
1060
+ function Range(pattern, index) {
1061
+ for (let scan = index; scan < pattern.length; scan++) {
1062
+ if (IsOpenParen(pattern, scan))
1063
+ return [index, scan];
1064
+ }
1065
+ return [index, pattern.length];
1066
+ }
1067
+ const expressions = [];
1068
+ for (let index = 0; index < pattern.length; index++) {
1069
+ if (IsOpenParen(pattern, index)) {
1070
+ const [start, end] = Group(pattern, index);
1071
+ const range = pattern.slice(start, end + 1);
1072
+ expressions.push(TemplateLiteralParse(range));
1073
+ index = end;
1074
+ }
1075
+ else {
1076
+ const [start, end] = Range(pattern, index);
1077
+ const range = pattern.slice(start, end);
1078
+ if (range.length > 0)
1079
+ expressions.push(TemplateLiteralParse(range));
1080
+ index = end - 1;
1081
+ }
1082
+ }
1083
+ return ((expressions.length === 0) ? { type: 'const', const: '' } :
1084
+ (expressions.length === 1) ? expressions[0] :
1085
+ { type: 'and', expr: expressions });
1086
+ }
1087
+ // ------------------------------------------------------------------
1088
+ // TemplateLiteralParse
1089
+ // ------------------------------------------------------------------
1090
+ /** Parses a pattern and returns an expression tree */
1091
+ function TemplateLiteralParse(pattern) {
1092
+ // prettier-ignore
1093
+ return (IsGroup(pattern) ? TemplateLiteralParse(InGroup(pattern)) :
1094
+ IsPrecedenceOr(pattern) ? Or(pattern) :
1095
+ IsPrecedenceAnd(pattern) ? And(pattern) :
1096
+ { type: 'const', const: Unescape(pattern) });
1097
+ }
1098
+ // ------------------------------------------------------------------
1099
+ // TemplateLiteralParseExact
1100
+ // ------------------------------------------------------------------
1101
+ /** Parses a pattern and strips forward and trailing ^ and $ */
1102
+ function TemplateLiteralParseExact(pattern) {
1103
+ return TemplateLiteralParse(pattern.slice(1, pattern.length - 1));
1104
+ }
1105
+
1106
+ // ------------------------------------------------------------------
1107
+ // TemplateLiteralFiniteError
1108
+ // ------------------------------------------------------------------
1109
+ class TemplateLiteralFiniteError extends TypeBoxError {
1110
+ }
1111
+ // ------------------------------------------------------------------
1112
+ // IsTemplateLiteralFiniteCheck
1113
+ // ------------------------------------------------------------------
1114
+ // prettier-ignore
1115
+ function IsNumberExpression(expression) {
1116
+ return (expression.type === 'or' &&
1117
+ expression.expr.length === 2 &&
1118
+ expression.expr[0].type === 'const' &&
1119
+ expression.expr[0].const === '0' &&
1120
+ expression.expr[1].type === 'const' &&
1121
+ expression.expr[1].const === '[1-9][0-9]*');
1122
+ }
1123
+ // prettier-ignore
1124
+ function IsBooleanExpression(expression) {
1125
+ return (expression.type === 'or' &&
1126
+ expression.expr.length === 2 &&
1127
+ expression.expr[0].type === 'const' &&
1128
+ expression.expr[0].const === 'true' &&
1129
+ expression.expr[1].type === 'const' &&
1130
+ expression.expr[1].const === 'false');
1131
+ }
1132
+ // prettier-ignore
1133
+ function IsStringExpression(expression) {
1134
+ return expression.type === 'const' && expression.const === '.*';
1135
+ }
1136
+ // ------------------------------------------------------------------
1137
+ // IsTemplateLiteralExpressionFinite
1138
+ // ------------------------------------------------------------------
1139
+ // prettier-ignore
1140
+ function IsTemplateLiteralExpressionFinite(expression) {
1141
+ return (IsNumberExpression(expression) || IsStringExpression(expression) ? false :
1142
+ IsBooleanExpression(expression) ? true :
1143
+ (expression.type === 'and') ? expression.expr.every((expr) => IsTemplateLiteralExpressionFinite(expr)) :
1144
+ (expression.type === 'or') ? expression.expr.every((expr) => IsTemplateLiteralExpressionFinite(expr)) :
1145
+ (expression.type === 'const') ? true :
1146
+ (() => { throw new TemplateLiteralFiniteError(`Unknown expression type`); })());
1147
+ }
1148
+ /** Returns true if this TemplateLiteral resolves to a finite set of values */
1149
+ function IsTemplateLiteralFinite(schema) {
1150
+ const expression = TemplateLiteralParseExact(schema.pattern);
1151
+ return IsTemplateLiteralExpressionFinite(expression);
1152
+ }
1153
+
1154
+ // ------------------------------------------------------------------
1155
+ // TemplateLiteralGenerateError
1156
+ // ------------------------------------------------------------------
1157
+ class TemplateLiteralGenerateError extends TypeBoxError {
1158
+ }
1159
+ // ------------------------------------------------------------------
1160
+ // TemplateLiteralExpressionGenerate
1161
+ // ------------------------------------------------------------------
1162
+ // prettier-ignore
1163
+ function* GenerateReduce(buffer) {
1164
+ if (buffer.length === 1)
1165
+ return yield* buffer[0];
1166
+ for (const left of buffer[0]) {
1167
+ for (const right of GenerateReduce(buffer.slice(1))) {
1168
+ yield `${left}${right}`;
1169
+ }
1170
+ }
1171
+ }
1172
+ // prettier-ignore
1173
+ function* GenerateAnd(expression) {
1174
+ return yield* GenerateReduce(expression.expr.map((expr) => [...TemplateLiteralExpressionGenerate(expr)]));
1175
+ }
1176
+ // prettier-ignore
1177
+ function* GenerateOr(expression) {
1178
+ for (const expr of expression.expr)
1179
+ yield* TemplateLiteralExpressionGenerate(expr);
1180
+ }
1181
+ // prettier-ignore
1182
+ function* GenerateConst(expression) {
1183
+ return yield expression.const;
1184
+ }
1185
+ function* TemplateLiteralExpressionGenerate(expression) {
1186
+ return expression.type === 'and'
1187
+ ? yield* GenerateAnd(expression)
1188
+ : expression.type === 'or'
1189
+ ? yield* GenerateOr(expression)
1190
+ : expression.type === 'const'
1191
+ ? yield* GenerateConst(expression)
1192
+ : (() => {
1193
+ throw new TemplateLiteralGenerateError('Unknown expression');
1194
+ })();
1195
+ }
1196
+ /** Generates a tuple of strings from the given TemplateLiteral. Returns an empty tuple if infinite. */
1197
+ function TemplateLiteralGenerate(schema) {
1198
+ const expression = TemplateLiteralParseExact(schema.pattern);
1199
+ // prettier-ignore
1200
+ return (IsTemplateLiteralExpressionFinite(expression)
1201
+ ? [...TemplateLiteralExpressionGenerate(expression)]
1202
+ : []);
1203
+ }
1204
+
1205
+ /** `[Json]` Creates a Literal type */
1206
+ function Literal(value, options = {}) {
1207
+ return {
1208
+ ...options,
1209
+ [Kind]: 'Literal',
1210
+ const: value,
1211
+ type: typeof value,
1212
+ };
1213
+ }
1214
+
1215
+ /** `[Json]` Creates a Boolean type */
1216
+ function Boolean(options = {}) {
1217
+ return {
1218
+ ...options,
1219
+ [Kind]: 'Boolean',
1220
+ type: 'boolean',
1221
+ };
1222
+ }
1223
+
1224
+ /** `[JavaScript]` Creates a BigInt type */
1225
+ function BigInt(options = {}) {
1226
+ return {
1227
+ ...options,
1228
+ [Kind]: 'BigInt',
1229
+ type: 'bigint',
1230
+ };
1231
+ }
1232
+
1233
+ /** `[Json]` Creates a Number type */
1234
+ function Number(options = {}) {
1235
+ return {
1236
+ ...options,
1237
+ [Kind]: 'Number',
1238
+ type: 'number',
1239
+ };
1240
+ }
1241
+
1242
+ /** `[Json]` Creates a String type */
1243
+ function String(options = {}) {
1244
+ return { ...options, [Kind]: 'String', type: 'string' };
1245
+ }
1246
+
1247
+ // ------------------------------------------------------------------
1248
+ // SyntaxParsers
1249
+ // ------------------------------------------------------------------
1250
+ // prettier-ignore
1251
+ function* FromUnion$8(syntax) {
1252
+ const trim = syntax.trim().replace(/"|'/g, '');
1253
+ return (trim === 'boolean' ? yield Boolean() :
1254
+ trim === 'number' ? yield Number() :
1255
+ trim === 'bigint' ? yield BigInt() :
1256
+ trim === 'string' ? yield String() :
1257
+ yield (() => {
1258
+ const literals = trim.split('|').map((literal) => Literal(literal.trim()));
1259
+ return (literals.length === 0 ? Never() :
1260
+ literals.length === 1 ? literals[0] :
1261
+ UnionEvaluated(literals));
1262
+ })());
1263
+ }
1264
+ // prettier-ignore
1265
+ function* FromTerminal(syntax) {
1266
+ if (syntax[1] !== '{') {
1267
+ const L = Literal('$');
1268
+ const R = FromSyntax(syntax.slice(1));
1269
+ return yield* [L, ...R];
1270
+ }
1271
+ for (let i = 2; i < syntax.length; i++) {
1272
+ if (syntax[i] === '}') {
1273
+ const L = FromUnion$8(syntax.slice(2, i));
1274
+ const R = FromSyntax(syntax.slice(i + 1));
1275
+ return yield* [...L, ...R];
1276
+ }
1277
+ }
1278
+ yield Literal(syntax);
1279
+ }
1280
+ // prettier-ignore
1281
+ function* FromSyntax(syntax) {
1282
+ for (let i = 0; i < syntax.length; i++) {
1283
+ if (syntax[i] === '$') {
1284
+ const L = Literal(syntax.slice(0, i));
1285
+ const R = FromTerminal(syntax.slice(i));
1286
+ return yield* [L, ...R];
1287
+ }
1288
+ }
1289
+ yield Literal(syntax);
1290
+ }
1291
+ /** Parses TemplateLiteralSyntax and returns a tuple of TemplateLiteralKinds */
1292
+ function TemplateLiteralSyntax(syntax) {
1293
+ return [...FromSyntax(syntax)];
1294
+ }
1295
+
1296
+ // ------------------------------------------------------------------
1297
+ // TemplateLiteralPatternError
1298
+ // ------------------------------------------------------------------
1299
+ class TemplateLiteralPatternError extends TypeBoxError {
1300
+ }
1301
+ // ------------------------------------------------------------------
1302
+ // TemplateLiteralPattern
1303
+ // ------------------------------------------------------------------
1304
+ function Escape(value) {
1305
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1306
+ }
1307
+ // prettier-ignore
1308
+ function Visit$1(schema, acc) {
1309
+ return (IsTemplateLiteral$1(schema) ? schema.pattern.slice(1, schema.pattern.length - 1) :
1310
+ IsUnion$1(schema) ? `(${schema.anyOf.map((schema) => Visit$1(schema, acc)).join('|')})` :
1311
+ IsNumber$1(schema) ? `${acc}${PatternNumber}` :
1312
+ IsInteger$1(schema) ? `${acc}${PatternNumber}` :
1313
+ IsBigInt$1(schema) ? `${acc}${PatternNumber}` :
1314
+ IsString$1(schema) ? `${acc}${PatternString}` :
1315
+ IsLiteral$1(schema) ? `${acc}${Escape(schema.const.toString())}` :
1316
+ IsBoolean$1(schema) ? `${acc}${PatternBoolean}` :
1317
+ (() => { throw new TemplateLiteralPatternError(`Unexpected Kind '${schema[Kind]}'`); })());
1318
+ }
1319
+ function TemplateLiteralPattern(kinds) {
1320
+ return `^${kinds.map((schema) => Visit$1(schema, '')).join('')}\$`;
1321
+ }
1322
+
1323
+ /** Returns a Union from the given TemplateLiteral */
1324
+ function TemplateLiteralToUnion(schema) {
1325
+ const R = TemplateLiteralGenerate(schema);
1326
+ const L = R.map((S) => Literal(S));
1327
+ return UnionEvaluated(L);
1328
+ }
1329
+
1330
+ /** `[Json]` Creates a TemplateLiteral type */
1331
+ // prettier-ignore
1332
+ function TemplateLiteral(unresolved, options = {}) {
1333
+ const pattern = IsString$2(unresolved)
1334
+ ? TemplateLiteralPattern(TemplateLiteralSyntax(unresolved))
1335
+ : TemplateLiteralPattern(unresolved);
1336
+ return { ...options, [Kind]: 'TemplateLiteral', type: 'string', pattern };
1337
+ }
1338
+
1339
+ // prettier-ignore
1340
+ function FromTemplateLiteral$2(T) {
1341
+ const R = TemplateLiteralGenerate(T);
1342
+ return R.map(S => S.toString());
1343
+ }
1344
+ // prettier-ignore
1345
+ function FromUnion$7(T) {
1346
+ const Acc = [];
1347
+ for (const L of T)
1348
+ Acc.push(...IndexPropertyKeys(L));
1349
+ return Acc;
1350
+ }
1351
+ // prettier-ignore
1352
+ function FromLiteral$1(T) {
1353
+ return ([T.toString()] // TS 5.4 observes TLiteralValue as not having a toString()
1354
+ );
1355
+ }
1356
+ /** Returns a tuple of PropertyKeys derived from the given TSchema */
1357
+ // prettier-ignore
1358
+ function IndexPropertyKeys(T) {
1359
+ return [...new Set((IsTemplateLiteral$1(T) ? FromTemplateLiteral$2(T) :
1360
+ IsUnion$1(T) ? FromUnion$7(T.anyOf) :
1361
+ IsLiteral$1(T) ? FromLiteral$1(T.const) :
1362
+ IsNumber$1(T) ? ['[number]'] :
1363
+ IsInteger$1(T) ? ['[number]'] :
1364
+ []))];
1365
+ }
1366
+
1367
+ // prettier-ignore
1368
+ function FromProperties$i(T, P, options) {
1369
+ const Acc = {};
1370
+ for (const K2 of Object.getOwnPropertyNames(P)) {
1371
+ Acc[K2] = Index(T, IndexPropertyKeys(P[K2]), options);
1372
+ }
1373
+ return Acc;
1374
+ }
1375
+ // prettier-ignore
1376
+ function FromMappedResult$b(T, R, options) {
1377
+ return FromProperties$i(T, R.properties, options);
1378
+ }
1379
+ // prettier-ignore
1380
+ function IndexFromMappedResult(T, R, options) {
1381
+ const P = FromMappedResult$b(T, R, options);
1382
+ return MappedResult(P);
1383
+ }
1384
+
1385
+ // prettier-ignore
1386
+ function FromRest$7(T, K) {
1387
+ return T.map(L => IndexFromPropertyKey(L, K));
1388
+ }
1389
+ // prettier-ignore
1390
+ function FromIntersectRest(T) {
1391
+ return T.filter(L => !IsNever$1(L));
1392
+ }
1393
+ // prettier-ignore
1394
+ function FromIntersect$6(T, K) {
1395
+ return (IntersectEvaluated(FromIntersectRest(FromRest$7(T, K))));
1396
+ }
1397
+ // prettier-ignore
1398
+ function FromUnionRest(T) {
1399
+ return (T.some(L => IsNever$1(L))
1400
+ ? []
1401
+ : T);
1402
+ }
1403
+ // prettier-ignore
1404
+ function FromUnion$6(T, K) {
1405
+ return (UnionEvaluated(FromUnionRest(FromRest$7(T, K))));
1406
+ }
1407
+ // prettier-ignore
1408
+ function FromTuple$3(T, K) {
1409
+ return (K in T ? T[K] :
1410
+ K === '[number]' ? UnionEvaluated(T) :
1411
+ Never());
1412
+ }
1413
+ // prettier-ignore
1414
+ function FromArray$4(T, K) {
1415
+ return (K === '[number]'
1416
+ ? T
1417
+ : Never());
1418
+ }
1419
+ // prettier-ignore
1420
+ function FromProperty$1(T, K) {
1421
+ return (K in T ? T[K] : Never());
1422
+ }
1423
+ // prettier-ignore
1424
+ function IndexFromPropertyKey(T, K) {
1425
+ return (IsIntersect$1(T) ? FromIntersect$6(T.allOf, K) :
1426
+ IsUnion$1(T) ? FromUnion$6(T.anyOf, K) :
1427
+ IsTuple$1(T) ? FromTuple$3(T.items ?? [], K) :
1428
+ IsArray$1(T) ? FromArray$4(T.items, K) :
1429
+ IsObject$1(T) ? FromProperty$1(T.properties, K) :
1430
+ Never());
1431
+ }
1432
+ // prettier-ignore
1433
+ function IndexFromPropertyKeys(T, K) {
1434
+ return K.map(L => IndexFromPropertyKey(T, L));
1435
+ }
1436
+ // prettier-ignore
1437
+ function FromSchema(T, K) {
1438
+ return (UnionEvaluated(IndexFromPropertyKeys(T, K)));
1439
+ }
1440
+ /** `[Json]` Returns an Indexed property type for the given keys */
1441
+ function Index(T, K, options = {}) {
1442
+ // prettier-ignore
1443
+ return (IsMappedResult$1(K) ? CloneType(IndexFromMappedResult(T, K, options)) :
1444
+ IsMappedKey$1(K) ? CloneType(IndexFromMappedKey(T, K, options)) :
1445
+ IsSchema$1(K) ? CloneType(FromSchema(T, IndexPropertyKeys(K)), options) :
1446
+ CloneType(FromSchema(T, K), options));
1447
+ }
1448
+
1449
+ // prettier-ignore
1450
+ function MappedIndexPropertyKey(T, K, options) {
1451
+ return { [K]: Index(T, [K], options) };
1452
+ }
1453
+ // prettier-ignore
1454
+ function MappedIndexPropertyKeys(T, K, options) {
1455
+ return K.reduce((Acc, L) => {
1456
+ return { ...Acc, ...MappedIndexPropertyKey(T, L, options) };
1457
+ }, {});
1458
+ }
1459
+ // prettier-ignore
1460
+ function MappedIndexProperties(T, K, options) {
1461
+ return MappedIndexPropertyKeys(T, K.keys, options);
1462
+ }
1463
+ // prettier-ignore
1464
+ function IndexFromMappedKey(T, K, options) {
1465
+ const P = MappedIndexProperties(T, K, options);
1466
+ return MappedResult(P);
1467
+ }
1468
+
1469
+ /** `[JavaScript]` Creates an Iterator type */
1470
+ function Iterator(items, options = {}) {
1471
+ return {
1472
+ ...options,
1473
+ [Kind]: 'Iterator',
1474
+ type: 'Iterator',
1475
+ items: CloneType(items),
1476
+ };
1477
+ }
1478
+
1479
+ /** `[Json]` Creates an Object type */
1480
+ function _Object(properties, options = {}) {
1481
+ const propertyKeys = globalThis.Object.getOwnPropertyNames(properties);
1482
+ const optionalKeys = propertyKeys.filter((key) => IsOptional$1(properties[key]));
1483
+ const requiredKeys = propertyKeys.filter((name) => !optionalKeys.includes(name));
1484
+ const clonedAdditionalProperties = IsSchema$1(options.additionalProperties) ? { additionalProperties: CloneType(options.additionalProperties) } : {};
1485
+ const clonedProperties = {};
1486
+ for (const key of propertyKeys)
1487
+ clonedProperties[key] = CloneType(properties[key]);
1488
+ return (requiredKeys.length > 0
1489
+ ? { ...options, ...clonedAdditionalProperties, [Kind]: 'Object', type: 'object', properties: clonedProperties, required: requiredKeys }
1490
+ : { ...options, ...clonedAdditionalProperties, [Kind]: 'Object', type: 'object', properties: clonedProperties });
1491
+ }
1492
+ /** `[Json]` Creates an Object type */
1493
+ const Object$1 = _Object;
1494
+
1495
+ /** `[JavaScript]` Creates a Promise type */
1496
+ function Promise$1(item, options = {}) {
1497
+ return {
1498
+ ...options,
1499
+ [Kind]: 'Promise',
1500
+ type: 'Promise',
1501
+ item: CloneType(item),
1502
+ };
1503
+ }
1504
+
1505
+ function RemoveReadonly(schema) {
1506
+ return Discard(CloneType(schema), [ReadonlyKind]);
1507
+ }
1508
+ function AddReadonly(schema) {
1509
+ return { ...CloneType(schema), [ReadonlyKind]: 'Readonly' };
1510
+ }
1511
+ // prettier-ignore
1512
+ function ReadonlyWithFlag(schema, F) {
1513
+ return (F === false
1514
+ ? RemoveReadonly(schema)
1515
+ : AddReadonly(schema));
1516
+ }
1517
+ /** `[Json]` Creates a Readonly property */
1518
+ function Readonly(schema, enable) {
1519
+ const F = enable ?? true;
1520
+ return IsMappedResult$1(schema) ? ReadonlyFromMappedResult(schema, F) : ReadonlyWithFlag(schema, F);
1521
+ }
1522
+
1523
+ // prettier-ignore
1524
+ function FromProperties$h(K, F) {
1525
+ const Acc = {};
1526
+ for (const K2 of globalThis.Object.getOwnPropertyNames(K))
1527
+ Acc[K2] = Readonly(K[K2], F);
1528
+ return Acc;
1529
+ }
1530
+ // prettier-ignore
1531
+ function FromMappedResult$a(R, F) {
1532
+ return FromProperties$h(R.properties, F);
1533
+ }
1534
+ // prettier-ignore
1535
+ function ReadonlyFromMappedResult(R, F) {
1536
+ const P = FromMappedResult$a(R, F);
1537
+ return MappedResult(P);
1538
+ }
1539
+
1540
+ /** `[Json]` Creates a Tuple type */
1541
+ function Tuple(items, options = {}) {
1542
+ // return TupleResolver.Resolve(T)
1543
+ const [additionalItems, minItems, maxItems] = [false, items.length, items.length];
1544
+ // prettier-ignore
1545
+ return (items.length > 0 ?
1546
+ { ...options, [Kind]: 'Tuple', type: 'array', items: CloneRest(items), additionalItems, minItems, maxItems } :
1547
+ { ...options, [Kind]: 'Tuple', type: 'array', minItems, maxItems });
1548
+ }
1549
+
1550
+ // prettier-ignore
1551
+ function FromMappedResult$9(K, P) {
1552
+ return (K in P
1553
+ ? FromSchemaType(K, P[K])
1554
+ : MappedResult(P));
1555
+ }
1556
+ // prettier-ignore
1557
+ function MappedKeyToKnownMappedResultProperties(K) {
1558
+ return { [K]: Literal(K) };
1559
+ }
1560
+ // prettier-ignore
1561
+ function MappedKeyToUnknownMappedResultProperties(P) {
1562
+ const Acc = {};
1563
+ for (const L of P)
1564
+ Acc[L] = Literal(L);
1565
+ return Acc;
1566
+ }
1567
+ // prettier-ignore
1568
+ function MappedKeyToMappedResultProperties(K, P) {
1569
+ return (SetIncludes(P, K)
1570
+ ? MappedKeyToKnownMappedResultProperties(K)
1571
+ : MappedKeyToUnknownMappedResultProperties(P));
1572
+ }
1573
+ // prettier-ignore
1574
+ function FromMappedKey$3(K, P) {
1575
+ const R = MappedKeyToMappedResultProperties(K, P);
1576
+ return FromMappedResult$9(K, R);
1577
+ }
1578
+ // prettier-ignore
1579
+ function FromRest$6(K, T) {
1580
+ return T.map(L => FromSchemaType(K, L));
1581
+ }
1582
+ // prettier-ignore
1583
+ function FromProperties$g(K, T) {
1584
+ const Acc = {};
1585
+ for (const K2 of globalThis.Object.getOwnPropertyNames(T))
1586
+ Acc[K2] = FromSchemaType(K, T[K2]);
1587
+ return Acc;
1588
+ }
1589
+ // prettier-ignore
1590
+ function FromSchemaType(K, T) {
1591
+ return (
1592
+ // unevaluated modifier types
1593
+ IsOptional$1(T) ? Optional(FromSchemaType(K, Discard(T, [OptionalKind]))) :
1594
+ IsReadonly(T) ? Readonly(FromSchemaType(K, Discard(T, [ReadonlyKind]))) :
1595
+ // unevaluated mapped types
1596
+ IsMappedResult$1(T) ? FromMappedResult$9(K, T.properties) :
1597
+ IsMappedKey$1(T) ? FromMappedKey$3(K, T.keys) :
1598
+ // unevaluated types
1599
+ IsConstructor$1(T) ? Constructor(FromRest$6(K, T.parameters), FromSchemaType(K, T.returns)) :
1600
+ IsFunction$1(T) ? Function(FromRest$6(K, T.parameters), FromSchemaType(K, T.returns)) :
1601
+ IsAsyncIterator$1(T) ? AsyncIterator(FromSchemaType(K, T.items)) :
1602
+ IsIterator$1(T) ? Iterator(FromSchemaType(K, T.items)) :
1603
+ IsIntersect$1(T) ? Intersect(FromRest$6(K, T.allOf)) :
1604
+ IsUnion$1(T) ? Union(FromRest$6(K, T.anyOf)) :
1605
+ IsTuple$1(T) ? Tuple(FromRest$6(K, T.items ?? [])) :
1606
+ IsObject$1(T) ? Object$1(FromProperties$g(K, T.properties)) :
1607
+ IsArray$1(T) ? Array$1(FromSchemaType(K, T.items)) :
1608
+ IsPromise$1(T) ? Promise$1(FromSchemaType(K, T.item)) :
1609
+ T);
1610
+ }
1611
+ // prettier-ignore
1612
+ function MappedFunctionReturnType(K, T) {
1613
+ const Acc = {};
1614
+ for (const L of K)
1615
+ Acc[L] = FromSchemaType(L, T);
1616
+ return Acc;
1617
+ }
1618
+ /** `[Json]` Creates a Mapped object type */
1619
+ function Mapped(key, map, options = {}) {
1620
+ const K = IsSchema$1(key) ? IndexPropertyKeys(key) : key;
1621
+ const RT = map({ [Kind]: 'MappedKey', keys: K });
1622
+ const R = MappedFunctionReturnType(K, RT);
1623
+ return CloneType(Object$1(R), options);
1624
+ }
1625
+
1626
+ function RemoveOptional(schema) {
1627
+ return Discard(CloneType(schema), [OptionalKind]);
1628
+ }
1629
+ function AddOptional(schema) {
1630
+ return { ...CloneType(schema), [OptionalKind]: 'Optional' };
1631
+ }
1632
+ // prettier-ignore
1633
+ function OptionalWithFlag(schema, F) {
1634
+ return (F === false
1635
+ ? RemoveOptional(schema)
1636
+ : AddOptional(schema));
1637
+ }
1638
+ /** `[Json]` Creates a Optional property */
1639
+ function Optional(schema, enable) {
1640
+ const F = enable ?? true;
1641
+ return IsMappedResult$1(schema) ? OptionalFromMappedResult(schema, F) : OptionalWithFlag(schema, F);
1642
+ }
1643
+
1644
+ // prettier-ignore
1645
+ function FromProperties$f(P, F) {
1646
+ const Acc = {};
1647
+ for (const K2 of globalThis.Object.getOwnPropertyNames(P))
1648
+ Acc[K2] = Optional(P[K2], F);
1649
+ return Acc;
1650
+ }
1651
+ // prettier-ignore
1652
+ function FromMappedResult$8(R, F) {
1653
+ return FromProperties$f(R.properties, F);
1654
+ }
1655
+ // prettier-ignore
1656
+ function OptionalFromMappedResult(R, F) {
1657
+ const P = FromMappedResult$8(R, F);
1658
+ return MappedResult(P);
1659
+ }
1660
+
1661
+ // ------------------------------------------------------------------
1662
+ // IntersectCreate
1663
+ // ------------------------------------------------------------------
1664
+ // prettier-ignore
1665
+ function IntersectCreate(T, options) {
1666
+ const allObjects = T.every((schema) => IsObject$1(schema));
1667
+ const clonedUnevaluatedProperties = IsSchema$1(options.unevaluatedProperties)
1668
+ ? { unevaluatedProperties: CloneType(options.unevaluatedProperties) }
1669
+ : {};
1670
+ return ((options.unevaluatedProperties === false || IsSchema$1(options.unevaluatedProperties) || allObjects
1671
+ ? { ...options, ...clonedUnevaluatedProperties, [Kind]: 'Intersect', type: 'object', allOf: CloneRest(T) }
1672
+ : { ...options, ...clonedUnevaluatedProperties, [Kind]: 'Intersect', allOf: CloneRest(T) }));
1673
+ }
1674
+
1675
+ // prettier-ignore
1676
+ function IsIntersectOptional(T) {
1677
+ return T.every(L => IsOptional$1(L));
1678
+ }
1679
+ // prettier-ignore
1680
+ function RemoveOptionalFromType(T) {
1681
+ return (Discard(T, [OptionalKind]));
1682
+ }
1683
+ // prettier-ignore
1684
+ function RemoveOptionalFromRest(T) {
1685
+ return T.map(L => IsOptional$1(L) ? RemoveOptionalFromType(L) : L);
1686
+ }
1687
+ // prettier-ignore
1688
+ function ResolveIntersect(T, options) {
1689
+ return (IsIntersectOptional(T)
1690
+ ? Optional(IntersectCreate(RemoveOptionalFromRest(T), options))
1691
+ : IntersectCreate(RemoveOptionalFromRest(T), options));
1692
+ }
1693
+ /** `[Json]` Creates an evaluated Intersect type */
1694
+ function IntersectEvaluated(T, options = {}) {
1695
+ if (T.length === 0)
1696
+ return Never(options);
1697
+ if (T.length === 1)
1698
+ return CloneType(T[0], options);
1699
+ if (T.some((schema) => IsTransform$1(schema)))
1700
+ throw new Error('Cannot intersect transform types');
1701
+ return ResolveIntersect(T, options);
1702
+ }
1703
+
1704
+ /** `[Json]` Creates an evaluated Intersect type */
1705
+ function Intersect(T, options = {}) {
1706
+ if (T.length === 0)
1707
+ return Never(options);
1708
+ if (T.length === 1)
1709
+ return CloneType(T[0], options);
1710
+ if (T.some((schema) => IsTransform$1(schema)))
1711
+ throw new Error('Cannot intersect transform types');
1712
+ return IntersectCreate(T, options);
1713
+ }
1714
+
1715
+ // prettier-ignore
1716
+ function FromRest$5(T) {
1717
+ return T.map(L => AwaitedResolve(L));
1718
+ }
1719
+ // prettier-ignore
1720
+ function FromIntersect$5(T) {
1721
+ return Intersect(FromRest$5(T));
1722
+ }
1723
+ // prettier-ignore
1724
+ function FromUnion$5(T) {
1725
+ return Union(FromRest$5(T));
1726
+ }
1727
+ // prettier-ignore
1728
+ function FromPromise$2(T) {
1729
+ return AwaitedResolve(T);
1730
+ }
1731
+ // ----------------------------------------------------------------
1732
+ // AwaitedResolve
1733
+ // ----------------------------------------------------------------
1734
+ // prettier-ignore
1735
+ function AwaitedResolve(T) {
1736
+ return (IsIntersect$1(T) ? FromIntersect$5(T.allOf) :
1737
+ IsUnion$1(T) ? FromUnion$5(T.anyOf) :
1738
+ IsPromise$1(T) ? FromPromise$2(T.item) :
1739
+ T);
1740
+ }
1741
+ /** `[JavaScript]` Constructs a type by recursively unwrapping Promise types */
1742
+ function Awaited(T, options = {}) {
1743
+ return CloneType(AwaitedResolve(T), options);
1744
+ }
1745
+
1746
+ // prettier-ignore
1747
+ function FromRest$4(T) {
1748
+ const Acc = [];
1749
+ for (const L of T)
1750
+ Acc.push(KeyOfPropertyKeys(L));
1751
+ return Acc;
1752
+ }
1753
+ // prettier-ignore
1754
+ function FromIntersect$4(T) {
1755
+ const C = FromRest$4(T);
1756
+ const R = SetUnionMany(C);
1757
+ return R;
1758
+ }
1759
+ // prettier-ignore
1760
+ function FromUnion$4(T) {
1761
+ const C = FromRest$4(T);
1762
+ const R = SetIntersectMany(C);
1763
+ return R;
1764
+ }
1765
+ // prettier-ignore
1766
+ function FromTuple$2(T) {
1767
+ return T.map((_, I) => I.toString());
1768
+ }
1769
+ // prettier-ignore
1770
+ function FromArray$3(_) {
1771
+ return (['[number]']);
1772
+ }
1773
+ // prettier-ignore
1774
+ function FromProperties$e(T) {
1775
+ return (globalThis.Object.getOwnPropertyNames(T));
1776
+ }
1777
+ // ------------------------------------------------------------------
1778
+ // FromPatternProperties
1779
+ // ------------------------------------------------------------------
1780
+ // prettier-ignore
1781
+ function FromPatternProperties(patternProperties) {
1782
+ return [];
1783
+ }
1784
+ /** Returns a tuple of PropertyKeys derived from the given TSchema. */
1785
+ // prettier-ignore
1786
+ function KeyOfPropertyKeys(T) {
1787
+ return (IsIntersect$1(T) ? FromIntersect$4(T.allOf) :
1788
+ IsUnion$1(T) ? FromUnion$4(T.anyOf) :
1789
+ IsTuple$1(T) ? FromTuple$2(T.items ?? []) :
1790
+ IsArray$1(T) ? FromArray$3(T.items) :
1791
+ IsObject$1(T) ? FromProperties$e(T.properties) :
1792
+ IsRecord$1(T) ? FromPatternProperties(T.patternProperties) :
1793
+ []);
1794
+ }
1795
+
1796
+ // prettier-ignore
1797
+ function KeyOfPropertyKeysToRest(T) {
1798
+ return T.map(L => L === '[number]' ? Number() : Literal(L));
1799
+ }
1800
+ /** `[Json]` Creates a KeyOf type */
1801
+ function KeyOf(T, options = {}) {
1802
+ if (IsMappedResult$1(T)) {
1803
+ return KeyOfFromMappedResult(T, options);
1804
+ }
1805
+ else {
1806
+ const K = KeyOfPropertyKeys(T);
1807
+ const S = KeyOfPropertyKeysToRest(K);
1808
+ const U = UnionEvaluated(S);
1809
+ return CloneType(U, options);
1810
+ }
1811
+ }
1812
+
1813
+ // prettier-ignore
1814
+ function FromProperties$d(K, options) {
1815
+ const Acc = {};
1816
+ for (const K2 of globalThis.Object.getOwnPropertyNames(K))
1817
+ Acc[K2] = KeyOf(K[K2], options);
1818
+ return Acc;
1819
+ }
1820
+ // prettier-ignore
1821
+ function FromMappedResult$7(R, options) {
1822
+ return FromProperties$d(R.properties, options);
1823
+ }
1824
+ // prettier-ignore
1825
+ function KeyOfFromMappedResult(R, options) {
1826
+ const P = FromMappedResult$7(R, options);
1827
+ return MappedResult(P);
1828
+ }
1829
+
1830
+ // prettier-ignore
1831
+ function CompositeKeys(T) {
1832
+ const Acc = [];
1833
+ for (const L of T)
1834
+ Acc.push(...KeyOfPropertyKeys(L));
1835
+ return SetDistinct(Acc);
1836
+ }
1837
+ // prettier-ignore
1838
+ function FilterNever(T) {
1839
+ return T.filter(L => !IsNever$1(L));
1840
+ }
1841
+ // prettier-ignore
1842
+ function CompositeProperty(T, K) {
1843
+ const Acc = [];
1844
+ for (const L of T)
1845
+ Acc.push(...IndexFromPropertyKeys(L, [K]));
1846
+ return FilterNever(Acc);
1847
+ }
1848
+ // prettier-ignore
1849
+ function CompositeProperties(T, K) {
1850
+ const Acc = {};
1851
+ for (const L of K) {
1852
+ Acc[L] = IntersectEvaluated(CompositeProperty(T, L));
1853
+ }
1854
+ return Acc;
1855
+ }
1856
+ // prettier-ignore
1857
+ function Composite(T, options = {}) {
1858
+ const K = CompositeKeys(T);
1859
+ const P = CompositeProperties(T, K);
1860
+ const R = Object$1(P, options);
1861
+ return R;
1862
+ }
1863
+
1864
+ /** `[JavaScript]` Creates a Date type */
1865
+ function Date$1(options = {}) {
1866
+ return {
1867
+ ...options,
1868
+ [Kind]: 'Date',
1869
+ type: 'Date',
1870
+ };
1871
+ }
1872
+
1873
+ /** `[Json]` Creates a Null type */
1874
+ function Null(options = {}) {
1875
+ return {
1876
+ ...options,
1877
+ [Kind]: 'Null',
1878
+ type: 'null',
1879
+ };
1880
+ }
1881
+
1882
+ /** `[JavaScript]` Creates a Symbol type */
1883
+ function Symbol$1(options) {
1884
+ return { ...options, [Kind]: 'Symbol', type: 'symbol' };
1885
+ }
1886
+
1887
+ /** `[JavaScript]` Creates a Undefined type */
1888
+ function Undefined(options = {}) {
1889
+ return { ...options, [Kind]: 'Undefined', type: 'undefined' };
1890
+ }
1891
+
1892
+ /** `[JavaScript]` Creates a Uint8Array type */
1893
+ function Uint8Array$1(options = {}) {
1894
+ return { ...options, [Kind]: 'Uint8Array', type: 'Uint8Array' };
1895
+ }
1896
+
1897
+ /** `[Json]` Creates an Unknown type */
1898
+ function Unknown(options = {}) {
1899
+ return {
1900
+ ...options,
1901
+ [Kind]: 'Unknown',
1902
+ };
1903
+ }
1904
+
1905
+ // prettier-ignore
1906
+ function FromArray$2(T) {
1907
+ return T.map(L => FromValue(L, false));
1908
+ }
1909
+ // prettier-ignore
1910
+ function FromProperties$c(value) {
1911
+ const Acc = {};
1912
+ for (const K of globalThis.Object.getOwnPropertyNames(value))
1913
+ Acc[K] = Readonly(FromValue(value[K], false));
1914
+ return Acc;
1915
+ }
1916
+ function ConditionalReadonly(T, root) {
1917
+ return (root === true ? T : Readonly(T));
1918
+ }
1919
+ // prettier-ignore
1920
+ function FromValue(value, root) {
1921
+ return (IsAsyncIterator$2(value) ? ConditionalReadonly(Any(), root) :
1922
+ IsIterator$2(value) ? ConditionalReadonly(Any(), root) :
1923
+ IsArray$2(value) ? Readonly(Tuple(FromArray$2(value))) :
1924
+ IsUint8Array$2(value) ? Uint8Array$1() :
1925
+ IsDate$2(value) ? Date$1() :
1926
+ IsObject$2(value) ? ConditionalReadonly(Object$1(FromProperties$c(value)), root) :
1927
+ IsFunction$2(value) ? ConditionalReadonly(Function([], Unknown()), root) :
1928
+ IsUndefined$2(value) ? Undefined() :
1929
+ IsNull$2(value) ? Null() :
1930
+ IsSymbol$2(value) ? Symbol$1() :
1931
+ IsBigInt$2(value) ? BigInt() :
1932
+ IsNumber$2(value) ? Literal(value) :
1933
+ IsBoolean$2(value) ? Literal(value) :
1934
+ IsString$2(value) ? Literal(value) :
1935
+ Object$1({}));
1936
+ }
1937
+ /** `[JavaScript]` Creates a readonly const type from the given value. */
1938
+ function Const(T, options = {}) {
1939
+ return CloneType(FromValue(T, true), options);
1940
+ }
1941
+
1942
+ /** `[JavaScript]` Extracts the ConstructorParameters from the given Constructor type */
1943
+ function ConstructorParameters(schema, options = {}) {
1944
+ return Tuple(CloneRest(schema.parameters), { ...options });
1945
+ }
1946
+
1947
+ function FromRest$3(schema, references) {
1948
+ return schema.map((schema) => Deref(schema, references));
1949
+ }
1950
+ // prettier-ignore
1951
+ function FromProperties$b(properties, references) {
1952
+ const Acc = {};
1953
+ for (const K of globalThis.Object.getOwnPropertyNames(properties)) {
1954
+ Acc[K] = Deref(properties[K], references);
1955
+ }
1956
+ return Acc;
1957
+ }
1958
+ // prettier-ignore
1959
+ function FromConstructor$1(schema, references) {
1960
+ schema.parameters = FromRest$3(schema.parameters, references);
1961
+ schema.returns = Deref(schema.returns, references);
1962
+ return schema;
1963
+ }
1964
+ // prettier-ignore
1965
+ function FromFunction$1(schema, references) {
1966
+ schema.parameters = FromRest$3(schema.parameters, references);
1967
+ schema.returns = Deref(schema.returns, references);
1968
+ return schema;
1969
+ }
1970
+ // prettier-ignore
1971
+ function FromIntersect$3(schema, references) {
1972
+ schema.allOf = FromRest$3(schema.allOf, references);
1973
+ return schema;
1974
+ }
1975
+ // prettier-ignore
1976
+ function FromUnion$3(schema, references) {
1977
+ schema.anyOf = FromRest$3(schema.anyOf, references);
1978
+ return schema;
1979
+ }
1980
+ // prettier-ignore
1981
+ function FromTuple$1(schema, references) {
1982
+ if (IsUndefined$2(schema.items))
1983
+ return schema;
1984
+ schema.items = FromRest$3(schema.items, references);
1985
+ return schema;
1986
+ }
1987
+ // prettier-ignore
1988
+ function FromArray$1(schema, references) {
1989
+ schema.items = Deref(schema.items, references);
1990
+ return schema;
1991
+ }
1992
+ // prettier-ignore
1993
+ function FromObject$1(schema, references) {
1994
+ schema.properties = FromProperties$b(schema.properties, references);
1995
+ return schema;
1996
+ }
1997
+ // prettier-ignore
1998
+ function FromPromise$1(schema, references) {
1999
+ schema.item = Deref(schema.item, references);
2000
+ return schema;
2001
+ }
2002
+ // prettier-ignore
2003
+ function FromAsyncIterator$1(schema, references) {
2004
+ schema.items = Deref(schema.items, references);
2005
+ return schema;
2006
+ }
2007
+ // prettier-ignore
2008
+ function FromIterator$1(schema, references) {
2009
+ schema.items = Deref(schema.items, references);
2010
+ return schema;
2011
+ }
2012
+ // prettier-ignore
2013
+ function FromRef(schema, references) {
2014
+ const target = references.find(remote => remote.$id === schema.$ref);
2015
+ if (target === undefined)
2016
+ throw Error(`Unable to dereference schema with $id ${schema.$ref}`);
2017
+ const discard = Discard(target, ['$id']);
2018
+ return Deref(discard, references);
2019
+ }
2020
+ // prettier-ignore
2021
+ function DerefResolve(schema, references) {
2022
+ return (IsConstructor$1(schema) ? FromConstructor$1(schema, references) :
2023
+ IsFunction$1(schema) ? FromFunction$1(schema, references) :
2024
+ IsIntersect$1(schema) ? FromIntersect$3(schema, references) :
2025
+ IsUnion$1(schema) ? FromUnion$3(schema, references) :
2026
+ IsTuple$1(schema) ? FromTuple$1(schema, references) :
2027
+ IsArray$1(schema) ? FromArray$1(schema, references) :
2028
+ IsObject$1(schema) ? FromObject$1(schema, references) :
2029
+ IsPromise$1(schema) ? FromPromise$1(schema, references) :
2030
+ IsAsyncIterator$1(schema) ? FromAsyncIterator$1(schema, references) :
2031
+ IsIterator$1(schema) ? FromIterator$1(schema, references) :
2032
+ IsRef$1(schema) ? FromRef(schema, references) :
2033
+ schema);
2034
+ }
2035
+ // ------------------------------------------------------------------
2036
+ // TDeref
2037
+ // ------------------------------------------------------------------
2038
+ /** `[Json]` Creates a dereferenced type */
2039
+ function Deref(schema, references) {
2040
+ return DerefResolve(CloneType(schema), CloneRest(references));
2041
+ }
2042
+
2043
+ /** `[Json]` Creates a Enum type */
2044
+ function Enum(item, options = {}) {
2045
+ if (IsUndefined$2(item))
2046
+ throw new Error('Enum undefined or empty');
2047
+ const values1 = globalThis.Object.getOwnPropertyNames(item)
2048
+ .filter((key) => isNaN(key))
2049
+ .map((key) => item[key]);
2050
+ const values2 = [...new Set(values1)];
2051
+ const anyOf = values2.map((value) => Literal(value));
2052
+ return Union(anyOf, { ...options, [Hint]: 'Enum' });
2053
+ }
2054
+
2055
+ class ExtendsResolverError extends TypeBoxError {
2056
+ }
2057
+ var ExtendsResult;
2058
+ (function (ExtendsResult) {
2059
+ ExtendsResult[ExtendsResult["Union"] = 0] = "Union";
2060
+ ExtendsResult[ExtendsResult["True"] = 1] = "True";
2061
+ ExtendsResult[ExtendsResult["False"] = 2] = "False";
2062
+ })(ExtendsResult || (ExtendsResult = {}));
2063
+ // ------------------------------------------------------------------
2064
+ // IntoBooleanResult
2065
+ // ------------------------------------------------------------------
2066
+ // prettier-ignore
2067
+ function IntoBooleanResult(result) {
2068
+ return result === ExtendsResult.False ? result : ExtendsResult.True;
2069
+ }
2070
+ // ------------------------------------------------------------------
2071
+ // Throw
2072
+ // ------------------------------------------------------------------
2073
+ // prettier-ignore
2074
+ function Throw(message) {
2075
+ throw new ExtendsResolverError(message);
2076
+ }
2077
+ // ------------------------------------------------------------------
2078
+ // StructuralRight
2079
+ // ------------------------------------------------------------------
2080
+ // prettier-ignore
2081
+ function IsStructuralRight(right) {
2082
+ return (IsNever(right) ||
2083
+ IsIntersect(right) ||
2084
+ IsUnion(right) ||
2085
+ IsUnknown(right) ||
2086
+ IsAny(right));
2087
+ }
2088
+ // prettier-ignore
2089
+ function StructuralRight(left, right) {
2090
+ return (IsNever(right) ? FromNeverRight() :
2091
+ IsIntersect(right) ? FromIntersectRight(left, right) :
2092
+ IsUnion(right) ? FromUnionRight(left, right) :
2093
+ IsUnknown(right) ? FromUnknownRight() :
2094
+ IsAny(right) ? FromAnyRight() :
2095
+ Throw('StructuralRight'));
2096
+ }
2097
+ // ------------------------------------------------------------------
2098
+ // Any
2099
+ // ------------------------------------------------------------------
2100
+ // prettier-ignore
2101
+ function FromAnyRight(left, right) {
2102
+ return ExtendsResult.True;
2103
+ }
2104
+ // prettier-ignore
2105
+ function FromAny(left, right) {
2106
+ return (IsIntersect(right) ? FromIntersectRight(left, right) :
2107
+ (IsUnion(right) && right.anyOf.some((schema) => IsAny(schema) || IsUnknown(schema))) ? ExtendsResult.True :
2108
+ IsUnion(right) ? ExtendsResult.Union :
2109
+ IsUnknown(right) ? ExtendsResult.True :
2110
+ IsAny(right) ? ExtendsResult.True :
2111
+ ExtendsResult.Union);
2112
+ }
2113
+ // ------------------------------------------------------------------
2114
+ // Array
2115
+ // ------------------------------------------------------------------
2116
+ // prettier-ignore
2117
+ function FromArrayRight(left, right) {
2118
+ return (IsUnknown(left) ? ExtendsResult.False :
2119
+ IsAny(left) ? ExtendsResult.Union :
2120
+ IsNever(left) ? ExtendsResult.True :
2121
+ ExtendsResult.False);
2122
+ }
2123
+ // prettier-ignore
2124
+ function FromArray(left, right) {
2125
+ return (IsObject(right) && IsObjectArrayLike(right) ? ExtendsResult.True :
2126
+ IsStructuralRight(right) ? StructuralRight(left, right) :
2127
+ !IsArray(right) ? ExtendsResult.False :
2128
+ IntoBooleanResult(Visit(left.items, right.items)));
2129
+ }
2130
+ // ------------------------------------------------------------------
2131
+ // AsyncIterator
2132
+ // ------------------------------------------------------------------
2133
+ // prettier-ignore
2134
+ function FromAsyncIterator(left, right) {
2135
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2136
+ !IsAsyncIterator(right) ? ExtendsResult.False :
2137
+ IntoBooleanResult(Visit(left.items, right.items)));
2138
+ }
2139
+ // ------------------------------------------------------------------
2140
+ // BigInt
2141
+ // ------------------------------------------------------------------
2142
+ // prettier-ignore
2143
+ function FromBigInt(left, right) {
2144
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2145
+ IsObject(right) ? FromObjectRight(left, right) :
2146
+ IsRecord(right) ? FromRecordRight(left, right) :
2147
+ IsBigInt(right) ? ExtendsResult.True :
2148
+ ExtendsResult.False);
2149
+ }
2150
+ // ------------------------------------------------------------------
2151
+ // Boolean
2152
+ // ------------------------------------------------------------------
2153
+ // prettier-ignore
2154
+ function FromBooleanRight(left, right) {
2155
+ return (IsLiteralBoolean(left) ? ExtendsResult.True :
2156
+ IsBoolean(left) ? ExtendsResult.True :
2157
+ ExtendsResult.False);
2158
+ }
2159
+ // prettier-ignore
2160
+ function FromBoolean(left, right) {
2161
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2162
+ IsObject(right) ? FromObjectRight(left, right) :
2163
+ IsRecord(right) ? FromRecordRight(left, right) :
2164
+ IsBoolean(right) ? ExtendsResult.True :
2165
+ ExtendsResult.False);
2166
+ }
2167
+ // ------------------------------------------------------------------
2168
+ // Constructor
2169
+ // ------------------------------------------------------------------
2170
+ // prettier-ignore
2171
+ function FromConstructor(left, right) {
2172
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2173
+ IsObject(right) ? FromObjectRight(left, right) :
2174
+ !IsConstructor(right) ? ExtendsResult.False :
2175
+ left.parameters.length > right.parameters.length ? ExtendsResult.False :
2176
+ (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === ExtendsResult.True)) ? ExtendsResult.False :
2177
+ IntoBooleanResult(Visit(left.returns, right.returns)));
2178
+ }
2179
+ // ------------------------------------------------------------------
2180
+ // Date
2181
+ // ------------------------------------------------------------------
2182
+ // prettier-ignore
2183
+ function FromDate(left, right) {
2184
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2185
+ IsObject(right) ? FromObjectRight(left, right) :
2186
+ IsRecord(right) ? FromRecordRight(left, right) :
2187
+ IsDate(right) ? ExtendsResult.True :
2188
+ ExtendsResult.False);
2189
+ }
2190
+ // ------------------------------------------------------------------
2191
+ // Function
2192
+ // ------------------------------------------------------------------
2193
+ // prettier-ignore
2194
+ function FromFunction(left, right) {
2195
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2196
+ IsObject(right) ? FromObjectRight(left, right) :
2197
+ !IsFunction(right) ? ExtendsResult.False :
2198
+ left.parameters.length > right.parameters.length ? ExtendsResult.False :
2199
+ (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === ExtendsResult.True)) ? ExtendsResult.False :
2200
+ IntoBooleanResult(Visit(left.returns, right.returns)));
2201
+ }
2202
+ // ------------------------------------------------------------------
2203
+ // Integer
2204
+ // ------------------------------------------------------------------
2205
+ // prettier-ignore
2206
+ function FromIntegerRight(left, right) {
2207
+ return (IsLiteral(left) && IsNumber$2(left.const) ? ExtendsResult.True :
2208
+ IsNumber(left) || IsInteger(left) ? ExtendsResult.True :
2209
+ ExtendsResult.False);
2210
+ }
2211
+ // prettier-ignore
2212
+ function FromInteger(left, right) {
2213
+ return (IsInteger(right) || IsNumber(right) ? ExtendsResult.True :
2214
+ IsStructuralRight(right) ? StructuralRight(left, right) :
2215
+ IsObject(right) ? FromObjectRight(left, right) :
2216
+ IsRecord(right) ? FromRecordRight(left, right) :
2217
+ ExtendsResult.False);
2218
+ }
2219
+ // ------------------------------------------------------------------
2220
+ // Intersect
2221
+ // ------------------------------------------------------------------
2222
+ // prettier-ignore
2223
+ function FromIntersectRight(left, right) {
2224
+ return right.allOf.every((schema) => Visit(left, schema) === ExtendsResult.True)
2225
+ ? ExtendsResult.True
2226
+ : ExtendsResult.False;
2227
+ }
2228
+ // prettier-ignore
2229
+ function FromIntersect$2(left, right) {
2230
+ return left.allOf.some((schema) => Visit(schema, right) === ExtendsResult.True)
2231
+ ? ExtendsResult.True
2232
+ : ExtendsResult.False;
2233
+ }
2234
+ // ------------------------------------------------------------------
2235
+ // Iterator
2236
+ // ------------------------------------------------------------------
2237
+ // prettier-ignore
2238
+ function FromIterator(left, right) {
2239
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2240
+ !IsIterator(right) ? ExtendsResult.False :
2241
+ IntoBooleanResult(Visit(left.items, right.items)));
2242
+ }
2243
+ // ------------------------------------------------------------------
2244
+ // Literal
2245
+ // ------------------------------------------------------------------
2246
+ // prettier-ignore
2247
+ function FromLiteral(left, right) {
2248
+ return (IsLiteral(right) && right.const === left.const ? ExtendsResult.True :
2249
+ IsStructuralRight(right) ? StructuralRight(left, right) :
2250
+ IsObject(right) ? FromObjectRight(left, right) :
2251
+ IsRecord(right) ? FromRecordRight(left, right) :
2252
+ IsString(right) ? FromStringRight(left) :
2253
+ IsNumber(right) ? FromNumberRight(left) :
2254
+ IsInteger(right) ? FromIntegerRight(left) :
2255
+ IsBoolean(right) ? FromBooleanRight(left) :
2256
+ ExtendsResult.False);
2257
+ }
2258
+ // ------------------------------------------------------------------
2259
+ // Never
2260
+ // ------------------------------------------------------------------
2261
+ // prettier-ignore
2262
+ function FromNeverRight(left, right) {
2263
+ return ExtendsResult.False;
2264
+ }
2265
+ // prettier-ignore
2266
+ function FromNever(left, right) {
2267
+ return ExtendsResult.True;
2268
+ }
2269
+ // ------------------------------------------------------------------
2270
+ // Not
2271
+ // ------------------------------------------------------------------
2272
+ // prettier-ignore
2273
+ function UnwrapTNot(schema) {
2274
+ let [current, depth] = [schema, 0];
2275
+ while (true) {
2276
+ if (!IsNot(current))
2277
+ break;
2278
+ current = current.not;
2279
+ depth += 1;
2280
+ }
2281
+ return depth % 2 === 0 ? current : Unknown();
2282
+ }
2283
+ // prettier-ignore
2284
+ function FromNot(left, right) {
2285
+ // TypeScript has no concept of negated types, and attempts to correctly check the negated
2286
+ // type at runtime would put TypeBox at odds with TypeScripts ability to statically infer
2287
+ // the type. Instead we unwrap to either unknown or T and continue evaluating.
2288
+ // prettier-ignore
2289
+ return (IsNot(left) ? Visit(UnwrapTNot(left), right) :
2290
+ IsNot(right) ? Visit(left, UnwrapTNot(right)) :
2291
+ Throw('Invalid fallthrough for Not'));
2292
+ }
2293
+ // ------------------------------------------------------------------
2294
+ // Null
2295
+ // ------------------------------------------------------------------
2296
+ // prettier-ignore
2297
+ function FromNull(left, right) {
2298
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2299
+ IsObject(right) ? FromObjectRight(left, right) :
2300
+ IsRecord(right) ? FromRecordRight(left, right) :
2301
+ IsNull(right) ? ExtendsResult.True :
2302
+ ExtendsResult.False);
2303
+ }
2304
+ // ------------------------------------------------------------------
2305
+ // Number
2306
+ // ------------------------------------------------------------------
2307
+ // prettier-ignore
2308
+ function FromNumberRight(left, right) {
2309
+ return (IsLiteralNumber(left) ? ExtendsResult.True :
2310
+ IsNumber(left) || IsInteger(left) ? ExtendsResult.True :
2311
+ ExtendsResult.False);
2312
+ }
2313
+ // prettier-ignore
2314
+ function FromNumber(left, right) {
2315
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2316
+ IsObject(right) ? FromObjectRight(left, right) :
2317
+ IsRecord(right) ? FromRecordRight(left, right) :
2318
+ IsInteger(right) || IsNumber(right) ? ExtendsResult.True :
2319
+ ExtendsResult.False);
2320
+ }
2321
+ // ------------------------------------------------------------------
2322
+ // Object
2323
+ // ------------------------------------------------------------------
2324
+ // prettier-ignore
2325
+ function IsObjectPropertyCount(schema, count) {
2326
+ return Object.getOwnPropertyNames(schema.properties).length === count;
2327
+ }
2328
+ // prettier-ignore
2329
+ function IsObjectStringLike(schema) {
2330
+ return IsObjectArrayLike(schema);
2331
+ }
2332
+ // prettier-ignore
2333
+ function IsObjectSymbolLike(schema) {
2334
+ return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'description' in schema.properties && IsUnion(schema.properties.description) && schema.properties.description.anyOf.length === 2 && ((IsString(schema.properties.description.anyOf[0]) &&
2335
+ IsUndefined(schema.properties.description.anyOf[1])) || (IsString(schema.properties.description.anyOf[1]) &&
2336
+ IsUndefined(schema.properties.description.anyOf[0]))));
2337
+ }
2338
+ // prettier-ignore
2339
+ function IsObjectNumberLike(schema) {
2340
+ return IsObjectPropertyCount(schema, 0);
2341
+ }
2342
+ // prettier-ignore
2343
+ function IsObjectBooleanLike(schema) {
2344
+ return IsObjectPropertyCount(schema, 0);
2345
+ }
2346
+ // prettier-ignore
2347
+ function IsObjectBigIntLike(schema) {
2348
+ return IsObjectPropertyCount(schema, 0);
2349
+ }
2350
+ // prettier-ignore
2351
+ function IsObjectDateLike(schema) {
2352
+ return IsObjectPropertyCount(schema, 0);
2353
+ }
2354
+ // prettier-ignore
2355
+ function IsObjectUint8ArrayLike(schema) {
2356
+ return IsObjectArrayLike(schema);
2357
+ }
2358
+ // prettier-ignore
2359
+ function IsObjectFunctionLike(schema) {
2360
+ const length = Number();
2361
+ return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'length' in schema.properties && IntoBooleanResult(Visit(schema.properties['length'], length)) === ExtendsResult.True);
2362
+ }
2363
+ // prettier-ignore
2364
+ function IsObjectConstructorLike(schema) {
2365
+ return IsObjectPropertyCount(schema, 0);
2366
+ }
2367
+ // prettier-ignore
2368
+ function IsObjectArrayLike(schema) {
2369
+ const length = Number();
2370
+ return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'length' in schema.properties && IntoBooleanResult(Visit(schema.properties['length'], length)) === ExtendsResult.True);
2371
+ }
2372
+ // prettier-ignore
2373
+ function IsObjectPromiseLike(schema) {
2374
+ const then = Function([Any()], Any());
2375
+ return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'then' in schema.properties && IntoBooleanResult(Visit(schema.properties['then'], then)) === ExtendsResult.True);
2376
+ }
2377
+ // ------------------------------------------------------------------
2378
+ // Property
2379
+ // ------------------------------------------------------------------
2380
+ // prettier-ignore
2381
+ function Property(left, right) {
2382
+ return (Visit(left, right) === ExtendsResult.False ? ExtendsResult.False :
2383
+ IsOptional(left) && !IsOptional(right) ? ExtendsResult.False :
2384
+ ExtendsResult.True);
2385
+ }
2386
+ // prettier-ignore
2387
+ function FromObjectRight(left, right) {
2388
+ return (IsUnknown(left) ? ExtendsResult.False :
2389
+ IsAny(left) ? ExtendsResult.Union : (IsNever(left) ||
2390
+ (IsLiteralString(left) && IsObjectStringLike(right)) ||
2391
+ (IsLiteralNumber(left) && IsObjectNumberLike(right)) ||
2392
+ (IsLiteralBoolean(left) && IsObjectBooleanLike(right)) ||
2393
+ (IsSymbol(left) && IsObjectSymbolLike(right)) ||
2394
+ (IsBigInt(left) && IsObjectBigIntLike(right)) ||
2395
+ (IsString(left) && IsObjectStringLike(right)) ||
2396
+ (IsSymbol(left) && IsObjectSymbolLike(right)) ||
2397
+ (IsNumber(left) && IsObjectNumberLike(right)) ||
2398
+ (IsInteger(left) && IsObjectNumberLike(right)) ||
2399
+ (IsBoolean(left) && IsObjectBooleanLike(right)) ||
2400
+ (IsUint8Array(left) && IsObjectUint8ArrayLike(right)) ||
2401
+ (IsDate(left) && IsObjectDateLike(right)) ||
2402
+ (IsConstructor(left) && IsObjectConstructorLike(right)) ||
2403
+ (IsFunction(left) && IsObjectFunctionLike(right))) ? ExtendsResult.True :
2404
+ (IsRecord(left) && IsString(RecordKey(left))) ? (() => {
2405
+ // When expressing a Record with literal key values, the Record is converted into a Object with
2406
+ // the Hint assigned as `Record`. This is used to invert the extends logic.
2407
+ return right[Hint] === 'Record' ? ExtendsResult.True : ExtendsResult.False;
2408
+ })() :
2409
+ (IsRecord(left) && IsNumber(RecordKey(left))) ? (() => {
2410
+ return IsObjectPropertyCount(right, 0) ? ExtendsResult.True : ExtendsResult.False;
2411
+ })() :
2412
+ ExtendsResult.False);
2413
+ }
2414
+ // prettier-ignore
2415
+ function FromObject(left, right) {
2416
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2417
+ IsRecord(right) ? FromRecordRight(left, right) :
2418
+ !IsObject(right) ? ExtendsResult.False :
2419
+ (() => {
2420
+ for (const key of Object.getOwnPropertyNames(right.properties)) {
2421
+ if (!(key in left.properties) && !IsOptional(right.properties[key])) {
2422
+ return ExtendsResult.False;
2423
+ }
2424
+ if (IsOptional(right.properties[key])) {
2425
+ return ExtendsResult.True;
2426
+ }
2427
+ if (Property(left.properties[key], right.properties[key]) === ExtendsResult.False) {
2428
+ return ExtendsResult.False;
2429
+ }
2430
+ }
2431
+ return ExtendsResult.True;
2432
+ })());
2433
+ }
2434
+ // ------------------------------------------------------------------
2435
+ // Promise
2436
+ // ------------------------------------------------------------------
2437
+ // prettier-ignore
2438
+ function FromPromise(left, right) {
2439
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2440
+ IsObject(right) && IsObjectPromiseLike(right) ? ExtendsResult.True :
2441
+ !IsPromise(right) ? ExtendsResult.False :
2442
+ IntoBooleanResult(Visit(left.item, right.item)));
2443
+ }
2444
+ // ------------------------------------------------------------------
2445
+ // Record
2446
+ // ------------------------------------------------------------------
2447
+ // prettier-ignore
2448
+ function RecordKey(schema) {
2449
+ return (PatternNumberExact in schema.patternProperties ? Number() :
2450
+ PatternStringExact in schema.patternProperties ? String() :
2451
+ Throw('Unknown record key pattern'));
2452
+ }
2453
+ // prettier-ignore
2454
+ function RecordValue(schema) {
2455
+ return (PatternNumberExact in schema.patternProperties ? schema.patternProperties[PatternNumberExact] :
2456
+ PatternStringExact in schema.patternProperties ? schema.patternProperties[PatternStringExact] :
2457
+ Throw('Unable to get record value schema'));
2458
+ }
2459
+ // prettier-ignore
2460
+ function FromRecordRight(left, right) {
2461
+ const [Key, Value] = [RecordKey(right), RecordValue(right)];
2462
+ return ((IsLiteralString(left) && IsNumber(Key) && IntoBooleanResult(Visit(left, Value)) === ExtendsResult.True) ? ExtendsResult.True :
2463
+ IsUint8Array(left) && IsNumber(Key) ? Visit(left, Value) :
2464
+ IsString(left) && IsNumber(Key) ? Visit(left, Value) :
2465
+ IsArray(left) && IsNumber(Key) ? Visit(left, Value) :
2466
+ IsObject(left) ? (() => {
2467
+ for (const key of Object.getOwnPropertyNames(left.properties)) {
2468
+ if (Property(Value, left.properties[key]) === ExtendsResult.False) {
2469
+ return ExtendsResult.False;
2470
+ }
2471
+ }
2472
+ return ExtendsResult.True;
2473
+ })() :
2474
+ ExtendsResult.False);
2475
+ }
2476
+ // prettier-ignore
2477
+ function FromRecord(left, right) {
2478
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2479
+ IsObject(right) ? FromObjectRight(left, right) :
2480
+ !IsRecord(right) ? ExtendsResult.False :
2481
+ Visit(RecordValue(left), RecordValue(right)));
2482
+ }
2483
+ // ------------------------------------------------------------------
2484
+ // RegExp
2485
+ // ------------------------------------------------------------------
2486
+ // prettier-ignore
2487
+ function FromRegExp(left, right) {
2488
+ // Note: RegExp types evaluate as strings, not RegExp objects.
2489
+ // Here we remap either into string and continue evaluating.
2490
+ const L = IsRegExp(left) ? String() : left;
2491
+ const R = IsRegExp(right) ? String() : right;
2492
+ return Visit(L, R);
2493
+ }
2494
+ // ------------------------------------------------------------------
2495
+ // String
2496
+ // ------------------------------------------------------------------
2497
+ // prettier-ignore
2498
+ function FromStringRight(left, right) {
2499
+ return (IsLiteral(left) && IsString$2(left.const) ? ExtendsResult.True :
2500
+ IsString(left) ? ExtendsResult.True :
2501
+ ExtendsResult.False);
2502
+ }
2503
+ // prettier-ignore
2504
+ function FromString(left, right) {
2505
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2506
+ IsObject(right) ? FromObjectRight(left, right) :
2507
+ IsRecord(right) ? FromRecordRight(left, right) :
2508
+ IsString(right) ? ExtendsResult.True :
2509
+ ExtendsResult.False);
2510
+ }
2511
+ // ------------------------------------------------------------------
2512
+ // Symbol
2513
+ // ------------------------------------------------------------------
2514
+ // prettier-ignore
2515
+ function FromSymbol(left, right) {
2516
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2517
+ IsObject(right) ? FromObjectRight(left, right) :
2518
+ IsRecord(right) ? FromRecordRight(left, right) :
2519
+ IsSymbol(right) ? ExtendsResult.True :
2520
+ ExtendsResult.False);
2521
+ }
2522
+ // ------------------------------------------------------------------
2523
+ // TemplateLiteral
2524
+ // ------------------------------------------------------------------
2525
+ // prettier-ignore
2526
+ function FromTemplateLiteral$1(left, right) {
2527
+ // TemplateLiteral types are resolved to either unions for finite expressions or string
2528
+ // for infinite expressions. Here we call to TemplateLiteralResolver to resolve for
2529
+ // either type and continue evaluating.
2530
+ return (IsTemplateLiteral(left) ? Visit(TemplateLiteralToUnion(left), right) :
2531
+ IsTemplateLiteral(right) ? Visit(left, TemplateLiteralToUnion(right)) :
2532
+ Throw('Invalid fallthrough for TemplateLiteral'));
2533
+ }
2534
+ // ------------------------------------------------------------------
2535
+ // Tuple
2536
+ // ------------------------------------------------------------------
2537
+ // prettier-ignore
2538
+ function IsArrayOfTuple(left, right) {
2539
+ return (IsArray(right) &&
2540
+ left.items !== undefined &&
2541
+ left.items.every((schema) => Visit(schema, right.items) === ExtendsResult.True));
2542
+ }
2543
+ // prettier-ignore
2544
+ function FromTupleRight(left, right) {
2545
+ return (IsNever(left) ? ExtendsResult.True :
2546
+ IsUnknown(left) ? ExtendsResult.False :
2547
+ IsAny(left) ? ExtendsResult.Union :
2548
+ ExtendsResult.False);
2549
+ }
2550
+ // prettier-ignore
2551
+ function FromTuple(left, right) {
2552
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2553
+ IsObject(right) && IsObjectArrayLike(right) ? ExtendsResult.True :
2554
+ IsArray(right) && IsArrayOfTuple(left, right) ? ExtendsResult.True :
2555
+ !IsTuple(right) ? ExtendsResult.False :
2556
+ (IsUndefined$2(left.items) && !IsUndefined$2(right.items)) || (!IsUndefined$2(left.items) && IsUndefined$2(right.items)) ? ExtendsResult.False :
2557
+ (IsUndefined$2(left.items) && !IsUndefined$2(right.items)) ? ExtendsResult.True :
2558
+ left.items.every((schema, index) => Visit(schema, right.items[index]) === ExtendsResult.True) ? ExtendsResult.True :
2559
+ ExtendsResult.False);
2560
+ }
2561
+ // ------------------------------------------------------------------
2562
+ // Uint8Array
2563
+ // ------------------------------------------------------------------
2564
+ // prettier-ignore
2565
+ function FromUint8Array(left, right) {
2566
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2567
+ IsObject(right) ? FromObjectRight(left, right) :
2568
+ IsRecord(right) ? FromRecordRight(left, right) :
2569
+ IsUint8Array(right) ? ExtendsResult.True :
2570
+ ExtendsResult.False);
2571
+ }
2572
+ // ------------------------------------------------------------------
2573
+ // Undefined
2574
+ // ------------------------------------------------------------------
2575
+ // prettier-ignore
2576
+ function FromUndefined(left, right) {
2577
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
2578
+ IsObject(right) ? FromObjectRight(left, right) :
2579
+ IsRecord(right) ? FromRecordRight(left, right) :
2580
+ IsVoid(right) ? FromVoidRight(left) :
2581
+ IsUndefined(right) ? ExtendsResult.True :
2582
+ ExtendsResult.False);
2583
+ }
2584
+ // ------------------------------------------------------------------
2585
+ // Union
2586
+ // ------------------------------------------------------------------
2587
+ // prettier-ignore
2588
+ function FromUnionRight(left, right) {
2589
+ return right.anyOf.some((schema) => Visit(left, schema) === ExtendsResult.True)
2590
+ ? ExtendsResult.True
2591
+ : ExtendsResult.False;
2592
+ }
2593
+ // prettier-ignore
2594
+ function FromUnion$2(left, right) {
2595
+ return left.anyOf.every((schema) => Visit(schema, right) === ExtendsResult.True)
2596
+ ? ExtendsResult.True
2597
+ : ExtendsResult.False;
2598
+ }
2599
+ // ------------------------------------------------------------------
2600
+ // Unknown
2601
+ // ------------------------------------------------------------------
2602
+ // prettier-ignore
2603
+ function FromUnknownRight(left, right) {
2604
+ return ExtendsResult.True;
2605
+ }
2606
+ // prettier-ignore
2607
+ function FromUnknown(left, right) {
2608
+ return (IsNever(right) ? FromNeverRight() :
2609
+ IsIntersect(right) ? FromIntersectRight(left, right) :
2610
+ IsUnion(right) ? FromUnionRight(left, right) :
2611
+ IsAny(right) ? FromAnyRight() :
2612
+ IsString(right) ? FromStringRight(left) :
2613
+ IsNumber(right) ? FromNumberRight(left) :
2614
+ IsInteger(right) ? FromIntegerRight(left) :
2615
+ IsBoolean(right) ? FromBooleanRight(left) :
2616
+ IsArray(right) ? FromArrayRight(left) :
2617
+ IsTuple(right) ? FromTupleRight(left) :
2618
+ IsObject(right) ? FromObjectRight(left, right) :
2619
+ IsUnknown(right) ? ExtendsResult.True :
2620
+ ExtendsResult.False);
2621
+ }
2622
+ // ------------------------------------------------------------------
2623
+ // Void
2624
+ // ------------------------------------------------------------------
2625
+ // prettier-ignore
2626
+ function FromVoidRight(left, right) {
2627
+ return (IsUndefined(left) ? ExtendsResult.True :
2628
+ IsUndefined(left) ? ExtendsResult.True :
2629
+ ExtendsResult.False);
2630
+ }
2631
+ // prettier-ignore
2632
+ function FromVoid(left, right) {
2633
+ return (IsIntersect(right) ? FromIntersectRight(left, right) :
2634
+ IsUnion(right) ? FromUnionRight(left, right) :
2635
+ IsUnknown(right) ? FromUnknownRight() :
2636
+ IsAny(right) ? FromAnyRight() :
2637
+ IsObject(right) ? FromObjectRight(left, right) :
2638
+ IsVoid(right) ? ExtendsResult.True :
2639
+ ExtendsResult.False);
2640
+ }
2641
+ // prettier-ignore
2642
+ function Visit(left, right) {
2643
+ return (
2644
+ // resolvable
2645
+ (IsTemplateLiteral(left) || IsTemplateLiteral(right)) ? FromTemplateLiteral$1(left, right) :
2646
+ (IsRegExp(left) || IsRegExp(right)) ? FromRegExp(left, right) :
2647
+ (IsNot(left) || IsNot(right)) ? FromNot(left, right) :
2648
+ // standard
2649
+ IsAny(left) ? FromAny(left, right) :
2650
+ IsArray(left) ? FromArray(left, right) :
2651
+ IsBigInt(left) ? FromBigInt(left, right) :
2652
+ IsBoolean(left) ? FromBoolean(left, right) :
2653
+ IsAsyncIterator(left) ? FromAsyncIterator(left, right) :
2654
+ IsConstructor(left) ? FromConstructor(left, right) :
2655
+ IsDate(left) ? FromDate(left, right) :
2656
+ IsFunction(left) ? FromFunction(left, right) :
2657
+ IsInteger(left) ? FromInteger(left, right) :
2658
+ IsIntersect(left) ? FromIntersect$2(left, right) :
2659
+ IsIterator(left) ? FromIterator(left, right) :
2660
+ IsLiteral(left) ? FromLiteral(left, right) :
2661
+ IsNever(left) ? FromNever() :
2662
+ IsNull(left) ? FromNull(left, right) :
2663
+ IsNumber(left) ? FromNumber(left, right) :
2664
+ IsObject(left) ? FromObject(left, right) :
2665
+ IsRecord(left) ? FromRecord(left, right) :
2666
+ IsString(left) ? FromString(left, right) :
2667
+ IsSymbol(left) ? FromSymbol(left, right) :
2668
+ IsTuple(left) ? FromTuple(left, right) :
2669
+ IsPromise(left) ? FromPromise(left, right) :
2670
+ IsUint8Array(left) ? FromUint8Array(left, right) :
2671
+ IsUndefined(left) ? FromUndefined(left, right) :
2672
+ IsUnion(left) ? FromUnion$2(left, right) :
2673
+ IsUnknown(left) ? FromUnknown(left, right) :
2674
+ IsVoid(left) ? FromVoid(left, right) :
2675
+ Throw(`Unknown left type operand '${left[Kind]}'`));
2676
+ }
2677
+ function ExtendsCheck(left, right) {
2678
+ return Visit(left, right);
2679
+ }
2680
+
2681
+ // prettier-ignore
2682
+ function FromProperties$a(P, Right, True, False, options) {
2683
+ const Acc = {};
2684
+ for (const K2 of globalThis.Object.getOwnPropertyNames(P))
2685
+ Acc[K2] = Extends(P[K2], Right, True, False, options);
2686
+ return Acc;
2687
+ }
2688
+ // prettier-ignore
2689
+ function FromMappedResult$6(Left, Right, True, False, options) {
2690
+ return FromProperties$a(Left.properties, Right, True, False, options);
2691
+ }
2692
+ // prettier-ignore
2693
+ function ExtendsFromMappedResult(Left, Right, True, False, options) {
2694
+ const P = FromMappedResult$6(Left, Right, True, False, options);
2695
+ return MappedResult(P);
2696
+ }
2697
+
2698
+ // prettier-ignore
2699
+ function ExtendsResolve(left, right, trueType, falseType) {
2700
+ const R = ExtendsCheck(left, right);
2701
+ return (R === ExtendsResult.Union ? Union([trueType, falseType]) :
2702
+ R === ExtendsResult.True ? trueType :
2703
+ falseType);
2704
+ }
2705
+ /** `[Json]` Creates a Conditional type */
2706
+ function Extends(L, R, T, F, options = {}) {
2707
+ // prettier-ignore
2708
+ return (IsMappedResult$1(L) ? ExtendsFromMappedResult(L, R, T, F, options) :
2709
+ IsMappedKey$1(L) ? CloneType(ExtendsFromMappedKey(L, R, T, F, options)) :
2710
+ CloneType(ExtendsResolve(L, R, T, F), options));
2711
+ }
2712
+
2713
+ // prettier-ignore
2714
+ function FromPropertyKey$2(K, U, L, R, options) {
2715
+ return {
2716
+ [K]: Extends(Literal(K), U, L, R, options)
2717
+ };
2718
+ }
2719
+ // prettier-ignore
2720
+ function FromPropertyKeys$2(K, U, L, R, options) {
2721
+ return K.reduce((Acc, LK) => {
2722
+ return { ...Acc, ...FromPropertyKey$2(LK, U, L, R, options) };
2723
+ }, {});
2724
+ }
2725
+ // prettier-ignore
2726
+ function FromMappedKey$2(K, U, L, R, options) {
2727
+ return FromPropertyKeys$2(K.keys, U, L, R, options);
2728
+ }
2729
+ // prettier-ignore
2730
+ function ExtendsFromMappedKey(T, U, L, R, options) {
2731
+ const P = FromMappedKey$2(T, U, L, R, options);
2732
+ return MappedResult(P);
2733
+ }
2734
+
2735
+ function ExcludeFromTemplateLiteral(L, R) {
2736
+ return Exclude(TemplateLiteralToUnion(L), R);
2737
+ }
2738
+
2739
+ function ExcludeRest(L, R) {
2740
+ const excluded = L.filter((inner) => ExtendsCheck(inner, R) === ExtendsResult.False);
2741
+ return excluded.length === 1 ? excluded[0] : Union(excluded);
2742
+ }
2743
+ /** `[Json]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
2744
+ function Exclude(L, R, options = {}) {
2745
+ // overloads
2746
+ if (IsTemplateLiteral$1(L))
2747
+ return CloneType(ExcludeFromTemplateLiteral(L, R), options);
2748
+ if (IsMappedResult$1(L))
2749
+ return CloneType(ExcludeFromMappedResult(L, R), options);
2750
+ // prettier-ignore
2751
+ return CloneType(IsUnion$1(L) ? ExcludeRest(L.anyOf, R) :
2752
+ ExtendsCheck(L, R) !== ExtendsResult.False ? Never() : L, options);
2753
+ }
2754
+
2755
+ // prettier-ignore
2756
+ function FromProperties$9(P, U) {
2757
+ const Acc = {};
2758
+ for (const K2 of globalThis.Object.getOwnPropertyNames(P))
2759
+ Acc[K2] = Exclude(P[K2], U);
2760
+ return Acc;
2761
+ }
2762
+ // prettier-ignore
2763
+ function FromMappedResult$5(R, T) {
2764
+ return FromProperties$9(R.properties, T);
2765
+ }
2766
+ // prettier-ignore
2767
+ function ExcludeFromMappedResult(R, T) {
2768
+ const P = FromMappedResult$5(R, T);
2769
+ return MappedResult(P);
2770
+ }
2771
+
2772
+ function ExtractFromTemplateLiteral(L, R) {
2773
+ return Extract(TemplateLiteralToUnion(L), R);
2774
+ }
2775
+
2776
+ function ExtractRest(L, R) {
2777
+ const extracted = L.filter((inner) => ExtendsCheck(inner, R) !== ExtendsResult.False);
2778
+ return extracted.length === 1 ? extracted[0] : Union(extracted);
2779
+ }
2780
+ /** `[Json]` Constructs a type by extracting from type all union members that are assignable to union */
2781
+ function Extract(L, R, options = {}) {
2782
+ // overloads
2783
+ if (IsTemplateLiteral$1(L))
2784
+ return CloneType(ExtractFromTemplateLiteral(L, R), options);
2785
+ if (IsMappedResult$1(L))
2786
+ return CloneType(ExtractFromMappedResult(L, R), options);
2787
+ // prettier-ignore
2788
+ return CloneType(IsUnion$1(L) ? ExtractRest(L.anyOf, R) :
2789
+ ExtendsCheck(L, R) !== ExtendsResult.False ? L : Never(), options);
2790
+ }
2791
+
2792
+ // prettier-ignore
2793
+ function FromProperties$8(P, T) {
2794
+ const Acc = {};
2795
+ for (const K2 of globalThis.Object.getOwnPropertyNames(P))
2796
+ Acc[K2] = Extract(P[K2], T);
2797
+ return Acc;
2798
+ }
2799
+ // prettier-ignore
2800
+ function FromMappedResult$4(R, T) {
2801
+ return FromProperties$8(R.properties, T);
2802
+ }
2803
+ // prettier-ignore
2804
+ function ExtractFromMappedResult(R, T) {
2805
+ const P = FromMappedResult$4(R, T);
2806
+ return MappedResult(P);
2807
+ }
2808
+
2809
+ /** `[JavaScript]` Extracts the InstanceType from the given Constructor type */
2810
+ function InstanceType(schema, options = {}) {
2811
+ return CloneType(schema.returns, options);
2812
+ }
2813
+
2814
+ /** `[Json]` Creates an Integer type */
2815
+ function Integer(options = {}) {
2816
+ return {
2817
+ ...options,
2818
+ [Kind]: 'Integer',
2819
+ type: 'integer',
2820
+ };
2821
+ }
2822
+
2823
+ // prettier-ignore
2824
+ function MappedIntrinsicPropertyKey(K, M, options) {
2825
+ return {
2826
+ [K]: Intrinsic(Literal(K), M, options)
2827
+ };
2828
+ }
2829
+ // prettier-ignore
2830
+ function MappedIntrinsicPropertyKeys(K, M, options) {
2831
+ return K.reduce((Acc, L) => {
2832
+ return { ...Acc, ...MappedIntrinsicPropertyKey(L, M, options) };
2833
+ }, {});
2834
+ }
2835
+ // prettier-ignore
2836
+ function MappedIntrinsicProperties(T, M, options) {
2837
+ return MappedIntrinsicPropertyKeys(T['keys'], M, options);
2838
+ }
2839
+ // prettier-ignore
2840
+ function IntrinsicFromMappedKey(T, M, options) {
2841
+ const P = MappedIntrinsicProperties(T, M, options);
2842
+ return MappedResult(P);
2843
+ }
2844
+
2845
+ // ------------------------------------------------------------------
2846
+ // Apply
2847
+ // ------------------------------------------------------------------
2848
+ function ApplyUncapitalize(value) {
2849
+ const [first, rest] = [value.slice(0, 1), value.slice(1)];
2850
+ return [first.toLowerCase(), rest].join('');
2851
+ }
2852
+ function ApplyCapitalize(value) {
2853
+ const [first, rest] = [value.slice(0, 1), value.slice(1)];
2854
+ return [first.toUpperCase(), rest].join('');
2855
+ }
2856
+ function ApplyUppercase(value) {
2857
+ return value.toUpperCase();
2858
+ }
2859
+ function ApplyLowercase(value) {
2860
+ return value.toLowerCase();
2861
+ }
2862
+ function FromTemplateLiteral(schema, mode, options) {
2863
+ // note: template literals require special runtime handling as they are encoded in string patterns.
2864
+ // This diverges from the mapped type which would otherwise map on the template literal kind.
2865
+ const expression = TemplateLiteralParseExact(schema.pattern);
2866
+ const finite = IsTemplateLiteralExpressionFinite(expression);
2867
+ if (!finite)
2868
+ return { ...schema, pattern: FromLiteralValue(schema.pattern, mode) };
2869
+ const strings = [...TemplateLiteralExpressionGenerate(expression)];
2870
+ const literals = strings.map((value) => Literal(value));
2871
+ const mapped = FromRest$2(literals, mode);
2872
+ const union = Union(mapped);
2873
+ return TemplateLiteral([union], options);
2874
+ }
2875
+ // prettier-ignore
2876
+ function FromLiteralValue(value, mode) {
2877
+ return (typeof value === 'string' ? (mode === 'Uncapitalize' ? ApplyUncapitalize(value) :
2878
+ mode === 'Capitalize' ? ApplyCapitalize(value) :
2879
+ mode === 'Uppercase' ? ApplyUppercase(value) :
2880
+ mode === 'Lowercase' ? ApplyLowercase(value) :
2881
+ value) : value.toString());
2882
+ }
2883
+ // prettier-ignore
2884
+ function FromRest$2(T, M) {
2885
+ return T.map(L => Intrinsic(L, M));
2886
+ }
2887
+ /** Applies an intrinsic string manipulation to the given type. */
2888
+ function Intrinsic(schema, mode, options = {}) {
2889
+ // prettier-ignore
2890
+ return (
2891
+ // Intrinsic-Mapped-Inference
2892
+ IsMappedKey$1(schema) ? IntrinsicFromMappedKey(schema, mode, options) :
2893
+ // Standard-Inference
2894
+ IsTemplateLiteral$1(schema) ? FromTemplateLiteral(schema, mode, schema) :
2895
+ IsUnion$1(schema) ? Union(FromRest$2(schema.anyOf, mode), options) :
2896
+ IsLiteral$1(schema) ? Literal(FromLiteralValue(schema.const, mode), options) :
2897
+ schema);
2898
+ }
2899
+
2900
+ /** `[Json]` Intrinsic function to Capitalize LiteralString types */
2901
+ function Capitalize(T, options = {}) {
2902
+ return Intrinsic(T, 'Capitalize', options);
2903
+ }
2904
+
2905
+ /** `[Json]` Intrinsic function to Lowercase LiteralString types */
2906
+ function Lowercase(T, options = {}) {
2907
+ return Intrinsic(T, 'Lowercase', options);
2908
+ }
2909
+
2910
+ /** `[Json]` Intrinsic function to Uncapitalize LiteralString types */
2911
+ function Uncapitalize(T, options = {}) {
2912
+ return Intrinsic(T, 'Uncapitalize', options);
2913
+ }
2914
+
2915
+ /** `[Json]` Intrinsic function to Uppercase LiteralString types */
2916
+ function Uppercase(T, options = {}) {
2917
+ return Intrinsic(T, 'Uppercase', options);
2918
+ }
2919
+
2920
+ /** `[Json]` Creates a Not type */
2921
+ function Not(schema, options) {
2922
+ return {
2923
+ ...options,
2924
+ [Kind]: 'Not',
2925
+ not: CloneType(schema),
2926
+ };
2927
+ }
2928
+
2929
+ // prettier-ignore
2930
+ function FromProperties$7(P, K, options) {
2931
+ const Acc = {};
2932
+ for (const K2 of globalThis.Object.getOwnPropertyNames(P))
2933
+ Acc[K2] = Omit(P[K2], K, options);
2934
+ return Acc;
2935
+ }
2936
+ // prettier-ignore
2937
+ function FromMappedResult$3(R, K, options) {
2938
+ return FromProperties$7(R.properties, K, options);
2939
+ }
2940
+ // prettier-ignore
2941
+ function OmitFromMappedResult(R, K, options) {
2942
+ const P = FromMappedResult$3(R, K, options);
2943
+ return MappedResult(P);
2944
+ }
2945
+
2946
+ // prettier-ignore
2947
+ function FromIntersect$1(T, K) {
2948
+ return T.map((T) => OmitResolve(T, K));
2949
+ }
2950
+ // prettier-ignore
2951
+ function FromUnion$1(T, K) {
2952
+ return T.map((T) => OmitResolve(T, K));
2953
+ }
2954
+ // ------------------------------------------------------------------
2955
+ // FromProperty
2956
+ // ------------------------------------------------------------------
2957
+ // prettier-ignore
2958
+ function FromProperty(T, K) {
2959
+ const { [K]: _, ...R } = T;
2960
+ return R;
2961
+ }
2962
+ // prettier-ignore
2963
+ function FromProperties$6(T, K) {
2964
+ return K.reduce((T, K2) => FromProperty(T, K2), T);
2965
+ }
2966
+ // ------------------------------------------------------------------
2967
+ // OmitResolve
2968
+ // ------------------------------------------------------------------
2969
+ // prettier-ignore
2970
+ function OmitResolve(T, K) {
2971
+ return (IsIntersect$1(T) ? Intersect(FromIntersect$1(T.allOf, K)) :
2972
+ IsUnion$1(T) ? Union(FromUnion$1(T.anyOf, K)) :
2973
+ IsObject$1(T) ? Object$1(FromProperties$6(T.properties, K)) :
2974
+ Object$1({}));
2975
+ }
2976
+ function Omit(T, K, options = {}) {
2977
+ // mapped
2978
+ if (IsMappedKey$1(K))
2979
+ return OmitFromMappedKey(T, K, options);
2980
+ if (IsMappedResult$1(T))
2981
+ return OmitFromMappedResult(T, K, options);
2982
+ // non-mapped
2983
+ const I = IsSchema$1(K) ? IndexPropertyKeys(K) : K;
2984
+ const D = Discard(T, [TransformKind, '$id', 'required']);
2985
+ const R = CloneType(OmitResolve(T, I), options);
2986
+ return { ...D, ...R };
2987
+ }
2988
+
2989
+ // prettier-ignore
2990
+ function FromPropertyKey$1(T, K, options) {
2991
+ return {
2992
+ [K]: Omit(T, [K], options)
2993
+ };
2994
+ }
2995
+ // prettier-ignore
2996
+ function FromPropertyKeys$1(T, K, options) {
2997
+ return K.reduce((Acc, LK) => {
2998
+ return { ...Acc, ...FromPropertyKey$1(T, LK, options) };
2999
+ }, {});
3000
+ }
3001
+ // prettier-ignore
3002
+ function FromMappedKey$1(T, K, options) {
3003
+ return FromPropertyKeys$1(T, K.keys, options);
3004
+ }
3005
+ // prettier-ignore
3006
+ function OmitFromMappedKey(T, K, options) {
3007
+ const P = FromMappedKey$1(T, K, options);
3008
+ return MappedResult(P);
3009
+ }
3010
+
3011
+ /** `[JavaScript]` Extracts the Parameters from the given Function type */
3012
+ function Parameters(schema, options = {}) {
3013
+ return Tuple(CloneRest(schema.parameters), { ...options });
3014
+ }
3015
+
3016
+ // prettier-ignore
3017
+ function FromRest$1(T) {
3018
+ return T.map(L => PartialResolve(L));
3019
+ }
3020
+ // prettier-ignore
3021
+ function FromProperties$5(T) {
3022
+ const Acc = {};
3023
+ for (const K of globalThis.Object.getOwnPropertyNames(T))
3024
+ Acc[K] = Optional(T[K]);
3025
+ return Acc;
3026
+ }
3027
+ // ------------------------------------------------------------------
3028
+ // PartialResolve
3029
+ // ------------------------------------------------------------------
3030
+ // prettier-ignore
3031
+ function PartialResolve(T) {
3032
+ return (IsIntersect$1(T) ? Intersect(FromRest$1(T.allOf)) :
3033
+ IsUnion$1(T) ? Union(FromRest$1(T.anyOf)) :
3034
+ IsObject$1(T) ? Object$1(FromProperties$5(T.properties)) :
3035
+ Object$1({}));
3036
+ }
3037
+ /** `[Json]` Constructs a type where all properties are optional */
3038
+ function Partial(T, options = {}) {
3039
+ if (IsMappedResult$1(T))
3040
+ return PartialFromMappedResult(T, options);
3041
+ const D = Discard(T, [TransformKind, '$id', 'required']);
3042
+ const R = CloneType(PartialResolve(T), options);
3043
+ return { ...D, ...R };
3044
+ }
3045
+
3046
+ // prettier-ignore
3047
+ function FromProperties$4(K, options) {
3048
+ const Acc = {};
3049
+ for (const K2 of globalThis.Object.getOwnPropertyNames(K))
3050
+ Acc[K2] = Partial(K[K2], options);
3051
+ return Acc;
3052
+ }
3053
+ // prettier-ignore
3054
+ function FromMappedResult$2(R, options) {
3055
+ return FromProperties$4(R.properties, options);
3056
+ }
3057
+ // prettier-ignore
3058
+ function PartialFromMappedResult(R, options) {
3059
+ const P = FromMappedResult$2(R, options);
3060
+ return MappedResult(P);
3061
+ }
3062
+
3063
+ // prettier-ignore
3064
+ function FromProperties$3(P, K, options) {
3065
+ const Acc = {};
3066
+ for (const K2 of globalThis.Object.getOwnPropertyNames(P))
3067
+ Acc[K2] = Pick(P[K2], K, options);
3068
+ return Acc;
3069
+ }
3070
+ // prettier-ignore
3071
+ function FromMappedResult$1(R, K, options) {
3072
+ return FromProperties$3(R.properties, K, options);
3073
+ }
3074
+ // prettier-ignore
3075
+ function PickFromMappedResult(R, K, options) {
3076
+ const P = FromMappedResult$1(R, K, options);
3077
+ return MappedResult(P);
3078
+ }
3079
+
3080
+ function FromIntersect(T, K) {
3081
+ return T.map((T) => PickResolve(T, K));
3082
+ }
3083
+ // prettier-ignore
3084
+ function FromUnion(T, K) {
3085
+ return T.map((T) => PickResolve(T, K));
3086
+ }
3087
+ // prettier-ignore
3088
+ function FromProperties$2(T, K) {
3089
+ const Acc = {};
3090
+ for (const K2 of K)
3091
+ if (K2 in T)
3092
+ Acc[K2] = T[K2];
3093
+ return Acc;
3094
+ }
3095
+ // ------------------------------------------------------------------
3096
+ // PickResolve
3097
+ // ------------------------------------------------------------------
3098
+ // prettier-ignore
3099
+ function PickResolve(T, K) {
3100
+ return (IsIntersect$1(T) ? Intersect(FromIntersect(T.allOf, K)) :
3101
+ IsUnion$1(T) ? Union(FromUnion(T.anyOf, K)) :
3102
+ IsObject$1(T) ? Object$1(FromProperties$2(T.properties, K)) :
3103
+ Object$1({}));
3104
+ }
3105
+ function Pick(T, K, options = {}) {
3106
+ // mapped
3107
+ if (IsMappedKey$1(K))
3108
+ return PickFromMappedKey(T, K, options);
3109
+ if (IsMappedResult$1(T))
3110
+ return PickFromMappedResult(T, K, options);
3111
+ // non-mapped
3112
+ const I = IsSchema$1(K) ? IndexPropertyKeys(K) : K;
3113
+ const D = Discard(T, [TransformKind, '$id', 'required']);
3114
+ const R = CloneType(PickResolve(T, I), options);
3115
+ return { ...D, ...R };
3116
+ }
3117
+
3118
+ // prettier-ignore
3119
+ function FromPropertyKey(T, K, options) {
3120
+ return {
3121
+ [K]: Pick(T, [K], options)
3122
+ };
3123
+ }
3124
+ // prettier-ignore
3125
+ function FromPropertyKeys(T, K, options) {
3126
+ return K.reduce((Acc, LK) => {
3127
+ return { ...Acc, ...FromPropertyKey(T, LK, options) };
3128
+ }, {});
3129
+ }
3130
+ // prettier-ignore
3131
+ function FromMappedKey(T, K, options) {
3132
+ return FromPropertyKeys(T, K.keys, options);
3133
+ }
3134
+ // prettier-ignore
3135
+ function PickFromMappedKey(T, K, options) {
3136
+ const P = FromMappedKey(T, K, options);
3137
+ return MappedResult(P);
3138
+ }
3139
+
3140
+ /** `[Json]` Creates a Readonly and Optional property */
3141
+ function ReadonlyOptional(schema) {
3142
+ return Readonly(Optional(schema));
3143
+ }
3144
+
3145
+ // ------------------------------------------------------------------
3146
+ // RecordCreateFromPattern
3147
+ // ------------------------------------------------------------------
3148
+ // prettier-ignore
3149
+ function RecordCreateFromPattern(pattern, T, options) {
3150
+ return {
3151
+ ...options,
3152
+ [Kind]: 'Record',
3153
+ type: 'object',
3154
+ patternProperties: { [pattern]: CloneType(T) }
3155
+ };
3156
+ }
3157
+ // ------------------------------------------------------------------
3158
+ // RecordCreateFromKeys
3159
+ // ------------------------------------------------------------------
3160
+ // prettier-ignore
3161
+ function RecordCreateFromKeys(K, T, options) {
3162
+ const Acc = {};
3163
+ for (const K2 of K)
3164
+ Acc[K2] = CloneType(T);
3165
+ return Object$1(Acc, { ...options, [Hint]: 'Record' });
3166
+ }
3167
+ // prettier-ignore
3168
+ function FromTemplateLiteralKey(K, T, options) {
3169
+ return (IsTemplateLiteralFinite(K)
3170
+ ? RecordCreateFromKeys(IndexPropertyKeys(K), T, options)
3171
+ : RecordCreateFromPattern(K.pattern, T, options));
3172
+ }
3173
+ // prettier-ignore
3174
+ function FromUnionKey(K, T, options) {
3175
+ return RecordCreateFromKeys(IndexPropertyKeys(Union(K)), T, options);
3176
+ }
3177
+ // prettier-ignore
3178
+ function FromLiteralKey(K, T, options) {
3179
+ return RecordCreateFromKeys([K.toString()], T, options);
3180
+ }
3181
+ // prettier-ignore
3182
+ function FromRegExpKey(K, T, options) {
3183
+ return RecordCreateFromPattern(K.source, T, options);
3184
+ }
3185
+ // prettier-ignore
3186
+ function FromStringKey(K, T, options) {
3187
+ const pattern = IsUndefined$2(K.pattern) ? PatternStringExact : K.pattern;
3188
+ return RecordCreateFromPattern(pattern, T, options);
3189
+ }
3190
+ // prettier-ignore
3191
+ function FromAnyKey(K, T, options) {
3192
+ return RecordCreateFromPattern(PatternStringExact, T, options);
3193
+ }
3194
+ // prettier-ignore
3195
+ function FromNeverKey(K, T, options) {
3196
+ return RecordCreateFromPattern(PatternNeverExact, T, options);
3197
+ }
3198
+ // prettier-ignore
3199
+ function FromIntegerKey(_, T, options) {
3200
+ return RecordCreateFromPattern(PatternNumberExact, T, options);
3201
+ }
3202
+ // prettier-ignore
3203
+ function FromNumberKey(_, T, options) {
3204
+ return RecordCreateFromPattern(PatternNumberExact, T, options);
3205
+ }
3206
+ // ------------------------------------------------------------------
3207
+ // TRecordOrObject
3208
+ // ------------------------------------------------------------------
3209
+ /** `[Json]` Creates a Record type */
3210
+ function Record(K, T, options = {}) {
3211
+ // prettier-ignore
3212
+ return (IsUnion$1(K) ? FromUnionKey(K.anyOf, T, options) :
3213
+ IsTemplateLiteral$1(K) ? FromTemplateLiteralKey(K, T, options) :
3214
+ IsLiteral$1(K) ? FromLiteralKey(K.const, T, options) :
3215
+ IsInteger$1(K) ? FromIntegerKey(K, T, options) :
3216
+ IsNumber$1(K) ? FromNumberKey(K, T, options) :
3217
+ IsRegExp$1(K) ? FromRegExpKey(K, T, options) :
3218
+ IsString$1(K) ? FromStringKey(K, T, options) :
3219
+ IsAny$1(K) ? FromAnyKey(K, T, options) :
3220
+ IsNever$1(K) ? FromNeverKey(K, T, options) :
3221
+ Never(options));
3222
+ }
3223
+
3224
+ // Auto Tracked For Recursive Types without ID's
3225
+ let Ordinal = 0;
3226
+ /** `[Json]` Creates a Recursive type */
3227
+ function Recursive(callback, options = {}) {
3228
+ if (IsUndefined$2(options.$id))
3229
+ options.$id = `T${Ordinal++}`;
3230
+ const thisType = callback({ [Kind]: 'This', $ref: `${options.$id}` });
3231
+ thisType.$id = options.$id;
3232
+ // prettier-ignore
3233
+ return CloneType({ ...options, [Hint]: 'Recursive', ...thisType });
3234
+ }
3235
+
3236
+ /** `[Json]` Creates a Ref type. */
3237
+ function Ref(unresolved, options = {}) {
3238
+ if (IsString$2(unresolved))
3239
+ return { ...options, [Kind]: 'Ref', $ref: unresolved };
3240
+ if (IsUndefined$2(unresolved.$id))
3241
+ throw new Error('Reference target type must specify an $id');
3242
+ return {
3243
+ ...options,
3244
+ [Kind]: 'Ref',
3245
+ $ref: unresolved.$id,
3246
+ };
3247
+ }
3248
+
3249
+ /** `[JavaScript]` Creates a RegExp type */
3250
+ function RegExp$1(unresolved, options = {}) {
3251
+ const expr = IsString$2(unresolved) ? new globalThis.RegExp(unresolved) : unresolved;
3252
+ return { ...options, [Kind]: 'RegExp', type: 'RegExp', source: expr.source, flags: expr.flags };
3253
+ }
3254
+
3255
+ // prettier-ignore
3256
+ function FromRest(T) {
3257
+ return T.map(L => RequiredResolve(L));
3258
+ }
3259
+ // prettier-ignore
3260
+ function FromProperties$1(T) {
3261
+ const Acc = {};
3262
+ for (const K of globalThis.Object.getOwnPropertyNames(T))
3263
+ Acc[K] = Discard(T[K], [OptionalKind]);
3264
+ return Acc;
3265
+ }
3266
+ // ------------------------------------------------------------------
3267
+ // RequiredResolve
3268
+ // ------------------------------------------------------------------
3269
+ // prettier-ignore
3270
+ function RequiredResolve(T) {
3271
+ return (IsIntersect$1(T) ? Intersect(FromRest(T.allOf)) :
3272
+ IsUnion$1(T) ? Union(FromRest(T.anyOf)) :
3273
+ IsObject$1(T) ? Object$1(FromProperties$1(T.properties)) :
3274
+ Object$1({}));
3275
+ }
3276
+ /** `[Json]` Constructs a type where all properties are required */
3277
+ function Required(T, options = {}) {
3278
+ if (IsMappedResult$1(T)) {
3279
+ return RequiredFromMappedResult(T, options);
3280
+ }
3281
+ else {
3282
+ const D = Discard(T, [TransformKind, '$id', 'required']);
3283
+ const R = CloneType(RequiredResolve(T), options);
3284
+ return { ...D, ...R };
3285
+ }
3286
+ }
3287
+
3288
+ // prettier-ignore
3289
+ function FromProperties(P, options) {
3290
+ const Acc = {};
3291
+ for (const K2 of globalThis.Object.getOwnPropertyNames(P))
3292
+ Acc[K2] = Required(P[K2], options);
3293
+ return Acc;
3294
+ }
3295
+ // prettier-ignore
3296
+ function FromMappedResult(R, options) {
3297
+ return FromProperties(R.properties, options);
3298
+ }
3299
+ // prettier-ignore
3300
+ function RequiredFromMappedResult(R, options) {
3301
+ const P = FromMappedResult(R, options);
3302
+ return MappedResult(P);
3303
+ }
3304
+
3305
+ // prettier-ignore
3306
+ function RestResolve(T) {
3307
+ return (IsIntersect$1(T) ? CloneRest(T.allOf) :
3308
+ IsUnion$1(T) ? CloneRest(T.anyOf) :
3309
+ IsTuple$1(T) ? CloneRest(T.items ?? []) :
3310
+ []);
3311
+ }
3312
+ /** `[Json]` Extracts interior Rest elements from Tuple, Intersect and Union types */
3313
+ function Rest(T) {
3314
+ return CloneRest(RestResolve(T));
3315
+ }
3316
+
3317
+ /** `[JavaScript]` Extracts the ReturnType from the given Function type */
3318
+ function ReturnType(schema, options = {}) {
3319
+ return CloneType(schema.returns, options);
3320
+ }
3321
+
3322
+ /** `[Json]` Omits compositing symbols from this schema. */
3323
+ function Strict(schema) {
3324
+ return JSON.parse(JSON.stringify(schema));
3325
+ }
3326
+
3327
+ // ------------------------------------------------------------------
3328
+ // TransformBuilders
3329
+ // ------------------------------------------------------------------
3330
+ class TransformDecodeBuilder {
3331
+ constructor(schema) {
3332
+ this.schema = schema;
3333
+ }
3334
+ Decode(decode) {
3335
+ return new TransformEncodeBuilder(this.schema, decode);
3336
+ }
3337
+ }
3338
+ // prettier-ignore
3339
+ class TransformEncodeBuilder {
3340
+ constructor(schema, decode) {
3341
+ this.schema = schema;
3342
+ this.decode = decode;
3343
+ }
3344
+ EncodeTransform(encode, schema) {
3345
+ const Encode = (value) => schema[TransformKind].Encode(encode(value));
3346
+ const Decode = (value) => this.decode(schema[TransformKind].Decode(value));
3347
+ const Codec = { Encode: Encode, Decode: Decode };
3348
+ return { ...schema, [TransformKind]: Codec };
3349
+ }
3350
+ EncodeSchema(encode, schema) {
3351
+ const Codec = { Decode: this.decode, Encode: encode };
3352
+ return { ...schema, [TransformKind]: Codec };
3353
+ }
3354
+ Encode(encode) {
3355
+ const schema = CloneType(this.schema);
3356
+ return (IsTransform$1(schema) ? this.EncodeTransform(encode, schema) : this.EncodeSchema(encode, schema));
3357
+ }
3358
+ }
3359
+ /** `[Json]` Creates a Transform type */
3360
+ function Transform(schema) {
3361
+ return new TransformDecodeBuilder(schema);
3362
+ }
3363
+
3364
+ /** `[Json]` Creates a Unsafe type that will infers as the generic argument T */
3365
+ function Unsafe(options = {}) {
3366
+ return {
3367
+ ...options,
3368
+ [Kind]: options[Kind] ?? 'Unsafe',
3369
+ };
3370
+ }
3371
+
3372
+ /** `[JavaScript]` Creates a Void type */
3373
+ function Void(options = {}) {
3374
+ return {
3375
+ ...options,
3376
+ [Kind]: 'Void',
3377
+ type: 'void',
3378
+ };
3379
+ }
3380
+
3381
+ // ------------------------------------------------------------------
3382
+ // Type: Module
3383
+ // ------------------------------------------------------------------
3384
+
3385
+ var TypeBuilder = /*#__PURE__*/Object.freeze({
3386
+ __proto__: null,
3387
+ Any: Any,
3388
+ Array: Array$1,
3389
+ AsyncIterator: AsyncIterator,
3390
+ Awaited: Awaited,
3391
+ BigInt: BigInt,
3392
+ Boolean: Boolean,
3393
+ Capitalize: Capitalize,
3394
+ Composite: Composite,
3395
+ Const: Const,
3396
+ Constructor: Constructor,
3397
+ ConstructorParameters: ConstructorParameters,
3398
+ Date: Date$1,
3399
+ Deref: Deref,
3400
+ Enum: Enum,
3401
+ Exclude: Exclude,
3402
+ Extends: Extends,
3403
+ Extract: Extract,
3404
+ Function: Function,
3405
+ Index: Index,
3406
+ InstanceType: InstanceType,
3407
+ Integer: Integer,
3408
+ Intersect: Intersect,
3409
+ Iterator: Iterator,
3410
+ KeyOf: KeyOf,
3411
+ Literal: Literal,
3412
+ Lowercase: Lowercase,
3413
+ Mapped: Mapped,
3414
+ Never: Never,
3415
+ Not: Not,
3416
+ Null: Null,
3417
+ Number: Number,
3418
+ Object: Object$1,
3419
+ Omit: Omit,
3420
+ Optional: Optional,
3421
+ Parameters: Parameters,
3422
+ Partial: Partial,
3423
+ Pick: Pick,
3424
+ Promise: Promise$1,
3425
+ Readonly: Readonly,
3426
+ ReadonlyOptional: ReadonlyOptional,
3427
+ Record: Record,
3428
+ Recursive: Recursive,
3429
+ Ref: Ref,
3430
+ RegExp: RegExp$1,
3431
+ Required: Required,
3432
+ Rest: Rest,
3433
+ ReturnType: ReturnType,
3434
+ Strict: Strict,
3435
+ String: String,
3436
+ Symbol: Symbol$1,
3437
+ TemplateLiteral: TemplateLiteral,
3438
+ Transform: Transform,
3439
+ Tuple: Tuple,
3440
+ Uint8Array: Uint8Array$1,
3441
+ Uncapitalize: Uncapitalize,
3442
+ Undefined: Undefined,
3443
+ Union: Union,
3444
+ Unknown: Unknown,
3445
+ Unsafe: Unsafe,
3446
+ Uppercase: Uppercase,
3447
+ Void: Void
3448
+ });
3449
+
3450
+ // ------------------------------------------------------------------
3451
+ // JsonTypeBuilder
3452
+ // ------------------------------------------------------------------
3453
+ /** JavaScript Type Builder with Static Resolution for TypeScript */
3454
+ const Type = TypeBuilder;
8
3455
 
9
3456
  /**
10
3457
  * 北森 EHR OpenClaw Plugin
@@ -14,65 +3461,138 @@ var os = require('os');
14
3461
  */
15
3462
  // 插件元数据
16
3463
  const pluginId = 'beisen-ehr-plugin';
17
- const pluginVersion = '1.0.0';
3464
+ const pluginVersion = '1.1.0';
18
3465
  /**
19
3466
  * 默认配置
20
3467
  */
21
3468
  const defaultConfig = {
22
3469
  apiUrl: 'https://openapi.italent.cn',
23
3470
  };
24
- /**
25
- * 从配置文件读取配置
26
- */
27
- function loadConfig() {
28
- try {
29
- const homeDir = os.homedir();
30
- const configPath = path.join(homeDir, '.openclaw', 'config.json');
31
- if (!fs.existsSync(configPath)) {
32
- return null;
3471
+ // ============ Token 管理 ============
3472
+ let cachedToken = null;
3473
+ let tokenExpiry = 0;
3474
+ async function getAccessToken(config) {
3475
+ // 如果已有有效 Token,直接返回
3476
+ if (cachedToken && Date.now() < tokenExpiry) {
3477
+ return cachedToken;
3478
+ }
3479
+ // 使用 AppKey + AppSecret 获取新 Token(北森官方格式)
3480
+ const tokenUrl = `${config.apiUrl || defaultConfig.apiUrl}/token`;
3481
+ const response = await fetch(tokenUrl, {
3482
+ method: "POST",
3483
+ headers: {
3484
+ "Content-Type": "application/json",
3485
+ },
3486
+ body: JSON.stringify({
3487
+ grant_type: "client_credentials",
3488
+ app_key: config.appKey,
3489
+ app_secret: config.appSecret,
3490
+ }),
3491
+ });
3492
+ if (!response.ok) {
3493
+ const errorText = await response.text();
3494
+ throw new Error(`获取 Token 失败:${response.status} - ${errorText}`);
3495
+ }
3496
+ const data = await response.json();
3497
+ cachedToken = data.accessToken || data.access_token;
3498
+ // Token 有效期通常 2 小时,提前 5 分钟刷新
3499
+ tokenExpiry = Date.now() + (data.expiresIn || data.expires_in || 7200) * 1000 - 5 * 60 * 1000;
3500
+ console.error(`🔑 已获取新 Token,有效期至:${new Date(tokenExpiry).toISOString()}`);
3501
+ return cachedToken;
3502
+ }
3503
+ // ============ 北森 API 客户端 ============
3504
+ class BeisenClient {
3505
+ constructor(config) {
3506
+ this.config = config;
3507
+ }
3508
+ async request(endpoint, options = {}) {
3509
+ const url = `${this.config.apiUrl || defaultConfig.apiUrl}${endpoint}`;
3510
+ const token = await getAccessToken(this.config);
3511
+ const headers = {
3512
+ "Content-Type": "application/json",
3513
+ "Authorization": `Bearer ${token}`,
3514
+ ...(this.config.tenantId && { "tenantId": this.config.tenantId }),
3515
+ ...(options.headers || {}),
3516
+ };
3517
+ console.error(`📡 请求:${options.method || 'GET'} ${url}`);
3518
+ console.error(`🏢 TenantId: ${this.config.tenantId}`);
3519
+ const response = await fetch(url, {
3520
+ ...options,
3521
+ headers,
3522
+ body: options.body ? JSON.stringify(options.body) : undefined,
3523
+ });
3524
+ if (!response.ok) {
3525
+ const errorText = await response.text();
3526
+ throw new Error(`北森 API 错误:${response.status} - ${errorText}`);
33
3527
  }
34
- const content = fs.readFileSync(configPath, 'utf-8');
35
- const config = JSON.parse(content);
36
- // 尝试读取插件配置(支持两种格式)
37
- const pluginConfig = config.plugins?.['beisen-ehr-plugin'] ||
38
- config.plugins?.['@peninsula-med/beisen-ehr-plugin'];
39
- if (!pluginConfig || !pluginConfig.config) {
40
- return null;
3528
+ return response.json();
3529
+ }
3530
+ // 提交加班申请(推送至北森系统并发起审批)
3531
+ async submitOvertime(data) {
3532
+ const overtimeData = {
3533
+ staffId: data.staffId,
3534
+ email: data.email,
3535
+ startDate: data.startDate,
3536
+ stopDate: data.stopDate,
3537
+ compensationType: data.compensationType !== undefined ? data.compensationType : 0,
3538
+ };
3539
+ if (data.properties) {
3540
+ overtimeData.properties = typeof data.properties === 'string' ? data.properties : JSON.stringify(data.properties);
41
3541
  }
42
- return pluginConfig.config;
3542
+ const payload = {
3543
+ attendance_overTime: overtimeData,
3544
+ };
3545
+ return this.request("/AttendanceOpen/api/v1/AttendanceOvertime/PostOverTimeWithApproval", {
3546
+ method: "POST",
3547
+ body: payload,
3548
+ });
43
3549
  }
44
- catch (error) {
45
- console.error('❌ 读取配置文件失败:', error.message);
46
- return null;
3550
+ // 查询假期余额
3551
+ async getLeaveBalance(staffId, email) {
3552
+ return this.request("/AttendanceOpen/api/v1/LeaveBalance/QueryLeaveBalance", {
3553
+ method: "POST",
3554
+ body: { staffId, email },
3555
+ });
3556
+ }
3557
+ // 查询考勤记录
3558
+ async getAttendanceRecords(startDate, endDate, employeeId) {
3559
+ return this.request("/api/v1/attendance/records", {
3560
+ method: "POST",
3561
+ body: { startDate, endDate, employeeId },
3562
+ });
3563
+ }
3564
+ // 查询审批状态
3565
+ async getApprovalStatus(applicationId) {
3566
+ return this.request(`/api/v1/approval/${applicationId}/status`);
47
3567
  }
48
3568
  }
49
3569
  /**
50
- * 插件激活函数
51
- *
52
- * 当插件被激活时,自动配置 MCP 服务器
3570
+ * 插件注册函数
53
3571
  */
54
- async function activate(runtime, config) {
55
- console.error(`🏢 北森 EHR 插件已激活 v${pluginVersion}`);
56
- // 如果 OpenClaw 没有传入配置,尝试从配置文件读取
57
- if (!config) {
58
- config = loadConfig();
59
- }
60
- // 处理空配置情况
3572
+ async function register(api) {
3573
+ console.error(`🏢 北森 EHR 插件已加载 v${pluginVersion}`);
3574
+ // 获取插件配置
3575
+ const config = api.config?.plugins?.entries?.['beisen-ehr-plugin']?.config;
61
3576
  if (!config) {
62
3577
  console.error('⚠️ 插件已安装,但未配置凭证');
63
3578
  console.error('');
64
- console.error('📝 请运行配置向导完成配置:');
65
- console.error('');
66
- console.error(' npx @peninsula-med/beisen-ehr-configure');
3579
+ console.error('📝 请在 OpenClaw 配置中添加:');
67
3580
  console.error('');
68
- console.error('按提示输入北森凭证即可!');
3581
+ console.error(' plugins: {');
3582
+ console.error(' entries: {');
3583
+ console.error(' "beisen-ehr-plugin": {');
3584
+ console.error(' enabled: true,');
3585
+ console.error(' config: {');
3586
+ console.error(' appKey: "your-app-key",');
3587
+ console.error(' appSecret: "your-app-secret",');
3588
+ console.error(' staffId: "your-staff-id",');
3589
+ console.error(' tenantId: "your-tenant-id",');
3590
+ console.error(' email: "your-email@company.com"');
3591
+ console.error(' }');
3592
+ console.error(' }');
3593
+ console.error(' }');
3594
+ console.error(' }');
69
3595
  console.error('');
70
- console.error('需要凭证?联系北森管理员获取:');
71
- console.error(' • App Key');
72
- console.error(' • App Secret');
73
- console.error(' • Tenant ID');
74
- console.error(' • Staff ID');
75
- console.error(' • 企业邮箱');
76
3596
  return;
77
3597
  }
78
3598
  // 验证必要配置
@@ -80,48 +3600,166 @@ async function activate(runtime, config) {
80
3600
  const missingFields = requiredFields.filter(field => !config[field]);
81
3601
  if (missingFields.length > 0) {
82
3602
  console.error(`❌ 缺少必要配置:${missingFields.join(', ')}`);
83
- console.error('');
84
- console.error('📝 请运行配置向导完成配置:');
85
- console.error('');
86
- console.error(' npx @peninsula-med/beisen-ehr-configure');
87
- console.error('');
88
- console.error('按提示输入北森凭证即可!');
3603
+ console.error('请补充完整配置后重启 Gateway');
89
3604
  return;
90
3605
  }
91
- // MCP 服务器配置会自动从 openclaw.plugin.json 加载
92
- // 这里只需要验证配置即可
93
3606
  console.error('✅ 北森 EHR 配置验证通过');
94
3607
  console.error(`📡 API: ${config.apiUrl || defaultConfig.apiUrl}`);
95
3608
  console.error(`👤 员工:${config.staffId}`);
96
3609
  console.error(`🏢 企业:${config.tenantId}`);
3610
+ // 创建 API 客户端
3611
+ const client = new BeisenClient(config);
3612
+ // ============ 注册工具 ============
3613
+ // 工具 1: 提交加班申请
3614
+ api.registerTool({
3615
+ name: 'beisen_submit_overtime',
3616
+ description: '提交加班申请到北森 EHR(推送数据并发起审批)',
3617
+ parameters: Type.Object({
3618
+ startDate: Type.String({ description: '开始时间 (YYYY-MM-DD HH:mm:ss),如:2020-07-17 22:00:00' }),
3619
+ stopDate: Type.String({ description: '结束时间 (YYYY-MM-DD HH:mm:ss),如:2020-07-17 23:00:00' }),
3620
+ compensationType: Type.Optional(Type.Number({ description: '补偿类型:0=调休假,1=加班费(默认 0)' })),
3621
+ properties: Type.Optional(Type.String({ description: '自定义字段 JSON 字符串' })),
3622
+ }),
3623
+ async execute(_id, params) {
3624
+ try {
3625
+ const result = await client.submitOvertime({
3626
+ staffId: config.staffId,
3627
+ email: config.email,
3628
+ startDate: params.startDate,
3629
+ stopDate: params.stopDate,
3630
+ compensationType: params.compensationType,
3631
+ properties: params.properties,
3632
+ });
3633
+ if (result.code === "200" || result.code === 200) {
3634
+ const compTypeText = params.compensationType === 1 ? "加班费" : "调休假";
3635
+ return {
3636
+ content: [
3637
+ {
3638
+ type: "text",
3639
+ text: `✅ 加班申请已提交!\n\n申请详情:\n- 员工 ID: ${config.staffId}\n- 邮箱:${config.email}\n- 开始:${params.startDate}\n- 结束:${params.stopDate}\n- 补偿类型:${compTypeText}\n- 申请 ID: ${result.data || "待返回"}`,
3640
+ },
3641
+ ],
3642
+ };
3643
+ }
3644
+ else {
3645
+ throw new Error(result.message || `API 返回错误:${result.code}`);
3646
+ }
3647
+ }
3648
+ catch (error) {
3649
+ return {
3650
+ content: [{ type: "text", text: `❌ 提交失败:${error.message}` }],
3651
+ isError: true,
3652
+ };
3653
+ }
3654
+ },
3655
+ });
3656
+ // 工具 2: 查询假期余额
3657
+ api.registerTool({
3658
+ name: 'beisen_get_leave_balance',
3659
+ description: '查询假期余额(年假、调休假等)',
3660
+ parameters: Type.Object({}),
3661
+ async execute(_id, _params) {
3662
+ try {
3663
+ const result = await client.getLeaveBalance(config.staffId, config.email);
3664
+ let balanceText = `📊 假期余额查询\n\n员工:${config.email}\n\n`;
3665
+ if (result.data && Array.isArray(result.data)) {
3666
+ result.data.forEach((item) => {
3667
+ balanceText += `• ${item.leaveTypeName || item.typeName || '未知假期'}: ${item.balanceDays || item.balance || 0} 天\n`;
3668
+ });
3669
+ }
3670
+ else if (result.data) {
3671
+ balanceText += JSON.stringify(result.data, null, 2);
3672
+ }
3673
+ else {
3674
+ balanceText += "暂无假期数据";
3675
+ }
3676
+ return {
3677
+ content: [{ type: "text", text: balanceText }],
3678
+ };
3679
+ }
3680
+ catch (error) {
3681
+ return {
3682
+ content: [{ type: "text", text: `❌ 查询失败:${error.message}` }],
3683
+ isError: true,
3684
+ };
3685
+ }
3686
+ },
3687
+ });
3688
+ // 工具 3: 查询考勤记录
3689
+ api.registerTool({
3690
+ name: 'beisen_query_attendance',
3691
+ description: '查询员工考勤记录',
3692
+ parameters: Type.Object({
3693
+ startDate: Type.String({ description: '开始日期 (YYYY-MM-DD)' }),
3694
+ endDate: Type.String({ description: '结束日期 (YYYY-MM-DD)' }),
3695
+ }),
3696
+ async execute(_id, params) {
3697
+ try {
3698
+ const records = await client.getAttendanceRecords(params.startDate, params.endDate, config.staffId);
3699
+ return {
3700
+ content: [
3701
+ {
3702
+ type: "text",
3703
+ text: `📅 考勤记录 (${params.startDate} ~ ${params.endDate})\n\n${JSON.stringify(records, null, 2)}`,
3704
+ },
3705
+ ],
3706
+ };
3707
+ }
3708
+ catch (error) {
3709
+ return {
3710
+ content: [{ type: "text", text: `❌ 查询失败:${error.message}` }],
3711
+ isError: true,
3712
+ };
3713
+ }
3714
+ },
3715
+ });
3716
+ // 工具 4: 查询审批状态
3717
+ api.registerTool({
3718
+ name: 'beisen_get_approval_status',
3719
+ description: '查询审批状态',
3720
+ parameters: Type.Object({
3721
+ applicationId: Type.String({ description: '申请 ID' }),
3722
+ }),
3723
+ async execute(_id, params) {
3724
+ try {
3725
+ const status = await client.getApprovalStatus(params.applicationId);
3726
+ return {
3727
+ content: [
3728
+ {
3729
+ type: "text",
3730
+ text: `📋 审批状态\n\n申请 ID: ${params.applicationId}\n状态:${status.status || "未知"}\n审批人:${status.approver || "待分配"}\n备注:${status.remark || "无"}`,
3731
+ },
3732
+ ],
3733
+ };
3734
+ }
3735
+ catch (error) {
3736
+ return {
3737
+ content: [{ type: "text", text: `❌ 查询失败:${error.message}` }],
3738
+ isError: true,
3739
+ };
3740
+ }
3741
+ },
3742
+ });
3743
+ console.error('✅ 已注册 4 个工具:beisen_submit_overtime, beisen_get_leave_balance, beisen_query_attendance, beisen_get_approval_status');
97
3744
  }
98
3745
  /**
99
3746
  * 插件停用函数
100
3747
  */
101
- async function deactivate() {
3748
+ async function unregister() {
102
3749
  console.error('🏢 北森 EHR 插件已停用');
103
3750
  }
104
- /**
105
- * 配置变更回调
106
- */
107
- async function onConfigChange(config) {
108
- console.error('🔄 北森 EHR 配置已更新');
109
- await activate(undefined, config);
110
- }
111
3751
  // 导出插件入口
112
3752
  const plugin = {
113
3753
  id: pluginId,
114
3754
  version: pluginVersion,
115
- activate,
116
- deactivate,
117
- onConfigChange,
3755
+ register,
3756
+ unregister,
118
3757
  };
119
3758
 
120
- exports.activate = activate;
121
- exports.deactivate = deactivate;
122
3759
  exports.default = plugin;
123
3760
  exports.defaultConfig = defaultConfig;
124
- exports.onConfigChange = onConfigChange;
125
3761
  exports.pluginId = pluginId;
126
3762
  exports.pluginVersion = pluginVersion;
3763
+ exports.register = register;
3764
+ exports.unregister = unregister;
127
3765
  //# sourceMappingURL=index.cjs.js.map