@sinclair/typebox 0.30.2 → 0.30.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiler/compiler.js +10 -4
- package/package.json +1 -1
- package/readme.md +40 -34
package/compiler/compiler.js
CHANGED
|
@@ -429,7 +429,9 @@ var TypeCompiler;
|
|
|
429
429
|
yield IsVoidCheck(value);
|
|
430
430
|
}
|
|
431
431
|
function* TKind(schema, references, value) {
|
|
432
|
-
|
|
432
|
+
const instance = state.instances.size;
|
|
433
|
+
state.instances.set(instance, schema);
|
|
434
|
+
yield `kind('${schema[Types.Kind]}', ${instance}, ${value})`;
|
|
433
435
|
}
|
|
434
436
|
function* Visit(schema, references, value, useHoisting = true) {
|
|
435
437
|
const references_ = ValueGuard.IsString(schema.$id) ? [...references, schema] : references;
|
|
@@ -525,7 +527,8 @@ var TypeCompiler;
|
|
|
525
527
|
const state = {
|
|
526
528
|
language: 'javascript',
|
|
527
529
|
functions: new Map(),
|
|
528
|
-
variables: new Map(),
|
|
530
|
+
variables: new Map(),
|
|
531
|
+
instances: new Map() // exterior kind instances
|
|
529
532
|
};
|
|
530
533
|
// -------------------------------------------------------------------
|
|
531
534
|
// Compiler Factory
|
|
@@ -583,6 +586,7 @@ var TypeCompiler;
|
|
|
583
586
|
state.language = options.language;
|
|
584
587
|
state.variables.clear();
|
|
585
588
|
state.functions.clear();
|
|
589
|
+
state.instances.clear();
|
|
586
590
|
if (!Types.TypeGuard.TSchema(schema))
|
|
587
591
|
throw new TypeCompilerTypeGuardError(schema);
|
|
588
592
|
for (const schema of references)
|
|
@@ -595,9 +599,11 @@ var TypeCompiler;
|
|
|
595
599
|
function Compile(schema, references = []) {
|
|
596
600
|
const generatedCode = Code(schema, references, { language: 'javascript' });
|
|
597
601
|
const compiledFunction = globalThis.Function('kind', 'format', 'hash', generatedCode);
|
|
598
|
-
|
|
599
|
-
|
|
602
|
+
const instances = new Map(state.instances);
|
|
603
|
+
function typeRegistryFunction(kind, instance, value) {
|
|
604
|
+
if (!Types.TypeRegistry.Has(kind) || !instances.has(instance))
|
|
600
605
|
return false;
|
|
606
|
+
const schema = instances.get(instance);
|
|
601
607
|
const checkFunc = Types.TypeRegistry.Get(kind);
|
|
602
608
|
return checkFunc(schema, value);
|
|
603
609
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -72,7 +72,6 @@ License MIT
|
|
|
72
72
|
## Contents
|
|
73
73
|
- [Install](#install)
|
|
74
74
|
- [Overview](#overview)
|
|
75
|
-
- [Features](#features)
|
|
76
75
|
- [Usage](#usage)
|
|
77
76
|
- [Types](#types)
|
|
78
77
|
- [Standard](#types-standard)
|
|
@@ -84,9 +83,9 @@ License MIT
|
|
|
84
83
|
- [Recursive](#types-recursive)
|
|
85
84
|
- [Conditional](#types-conditional)
|
|
86
85
|
- [Template Literal](#types-template-literal)
|
|
87
|
-
- [Intrinsic String](#types-intrinsic-string)
|
|
88
86
|
- [Indexed](#types-indexed)
|
|
89
87
|
- [Negated](#types-negated)
|
|
88
|
+
- [Intrinsic](#types-intrinsic)
|
|
90
89
|
- [Rest](#types-rest)
|
|
91
90
|
- [Guards](#types-guards)
|
|
92
91
|
- [Unsafe](#types-unsafe)
|
|
@@ -111,7 +110,7 @@ License MIT
|
|
|
111
110
|
- [Types](#typesystem-types)
|
|
112
111
|
- [Formats](#typesystem-formats)
|
|
113
112
|
- [Policies](#typesystem-policies)
|
|
114
|
-
- [
|
|
113
|
+
- [Workbench](#workbench)
|
|
115
114
|
- [Ecosystem](#ecosystem)
|
|
116
115
|
- [Benchmark](#benchmark)
|
|
117
116
|
- [Compile](#benchmark-compile)
|
|
@@ -876,34 +875,6 @@ const R = Type.Record(T, Type.String()) // const R = {
|
|
|
876
875
|
// }
|
|
877
876
|
```
|
|
878
877
|
|
|
879
|
-
<a name='types-intrinsic-string'></a>
|
|
880
|
-
|
|
881
|
-
### Intrinsic String Types
|
|
882
|
-
|
|
883
|
-
TypeBox supports a set of intrinsic string mapping functions which can be used on string literals. These functions match the TypeScript string intrinsic types `Uppercase`, `Lowercase`, `Capitalize` and `Uncapitalize`. These functions are supported for literal strings, template literals and union types. The following shows the literal string usage.
|
|
884
|
-
|
|
885
|
-
```typescript
|
|
886
|
-
// TypeScript
|
|
887
|
-
|
|
888
|
-
type A = Uncapitalize<'HELLO'> // type A = 'hELLO'
|
|
889
|
-
|
|
890
|
-
type B = Capitalize<'hello'> // type B = 'Hello'
|
|
891
|
-
|
|
892
|
-
type C = Uppercase<'hello'> // type C = 'HELLO'
|
|
893
|
-
|
|
894
|
-
type D = Lowercase<'HELLO'> // type D = 'hello'
|
|
895
|
-
|
|
896
|
-
// TypeBox
|
|
897
|
-
|
|
898
|
-
const A = Type.Uncapitalize(Type.Literal('HELLO')) // const A: TLiteral<'hELLO'>
|
|
899
|
-
|
|
900
|
-
const B = Type.Capitalize(Type.Literal('hello')) // const B: TLiteral<'Hello'>
|
|
901
|
-
|
|
902
|
-
const C = Type.Uppercase(Type.Literal('hello')) // const C: TLiteral<'HELLO'>
|
|
903
|
-
|
|
904
|
-
const D = Type.Lowercase(Type.Literal('HELLO')) // const D: TLiteral<'hello'>
|
|
905
|
-
```
|
|
906
|
-
|
|
907
878
|
<a name='types-indexed'></a>
|
|
908
879
|
|
|
909
880
|
### Indexed Access Types
|
|
@@ -986,6 +957,41 @@ const Even = Type.Number({ multipleOf: 2 })
|
|
|
986
957
|
|
|
987
958
|
const Odd = Type.Intersect([Type.Number(), Type.Not(Even)])
|
|
988
959
|
```
|
|
960
|
+
|
|
961
|
+
<a name='types-intrinsic'></a>
|
|
962
|
+
|
|
963
|
+
### Intrinsic String Types
|
|
964
|
+
|
|
965
|
+
TypeBox supports TypeScript intrinsic string manipulation types `Uppercase`, `Lowercase`, `Capitalize` and `Uncapitalize`. These can be applied to string literals, template literals and unions. The following shows general usage.
|
|
966
|
+
|
|
967
|
+
```typescript
|
|
968
|
+
// TypeScript
|
|
969
|
+
|
|
970
|
+
type A = Capitalize<'hello'> // type A = 'Hello'
|
|
971
|
+
|
|
972
|
+
type B = Capitalize<'hello' | 'world'> // type C = 'Hello' | 'World'
|
|
973
|
+
|
|
974
|
+
type C = Capitalize<`hello${1|2|3}`> // type B = 'Hello1' | 'Hello2' | 'Hello3'
|
|
975
|
+
|
|
976
|
+
// TypeBox
|
|
977
|
+
|
|
978
|
+
const A = Type.Capitalize(Type.Literal('hello')) // const A: TLiteral<'Hello'>
|
|
979
|
+
|
|
980
|
+
const B = Type.Capitalize(Type.Union([ // const B: TUnion<[
|
|
981
|
+
Type.Literal('hello'), // TLiteral<'Hello'>,
|
|
982
|
+
Type.Literal('world') // TLiteral<'World'>
|
|
983
|
+
])) // ]>
|
|
984
|
+
|
|
985
|
+
const C = Type.Capitalize( // const C: TTemplateLiteral<[
|
|
986
|
+
Type.TemplateLiteral('hello${1|2|3}') // TLiteral<'Hello'>,
|
|
987
|
+
) // TUnion<[
|
|
988
|
+
// TLiteral<'1'>,
|
|
989
|
+
// TLiteral<'2'>,
|
|
990
|
+
// TLiteral<'3'>
|
|
991
|
+
// ]>
|
|
992
|
+
// ]>
|
|
993
|
+
```
|
|
994
|
+
|
|
989
995
|
<a name='types-rest'></a>
|
|
990
996
|
|
|
991
997
|
### Rest Types
|
|
@@ -1510,13 +1516,13 @@ TypeSystem.AllowArrayObjects = true
|
|
|
1510
1516
|
TypeSystem.AllowNaN = true
|
|
1511
1517
|
```
|
|
1512
1518
|
|
|
1513
|
-
<a name='
|
|
1519
|
+
<a name='workbench'></a>
|
|
1514
1520
|
|
|
1515
|
-
## TypeBox
|
|
1521
|
+
## TypeBox Workbench
|
|
1516
1522
|
|
|
1517
1523
|
TypeBox offers a small web based code generation tool that can be used to convert TypeScript types into TypeBox types as well as a variety of other runtime type representations.
|
|
1518
1524
|
|
|
1519
|
-
[TypeBox
|
|
1525
|
+
[TypeBox Workbench Link Here](https://sinclairzx81.github.io/typebox-workbench/)
|
|
1520
1526
|
|
|
1521
1527
|
<a name='ecosystem'></a>
|
|
1522
1528
|
|