@sinclair/typebox 0.30.1 → 0.30.3
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 +9 -4
- package/package.json +1 -1
- package/readme.md +36 -30
- package/typebox.d.ts +1 -1
- package/typebox.js +1 -1
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,10 @@ 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
|
-
function typeRegistryFunction(kind, value) {
|
|
599
|
-
if (!Types.TypeRegistry.Has(kind))
|
|
602
|
+
function typeRegistryFunction(kind, instance, value) {
|
|
603
|
+
if (!Types.TypeRegistry.Has(kind) || !state.instances.has(instance))
|
|
600
604
|
return false;
|
|
605
|
+
const schema = state.instances.get(instance);
|
|
601
606
|
const checkFunc = Types.TypeRegistry.Get(kind);
|
|
602
607
|
return checkFunc(schema, value);
|
|
603
608
|
}
|
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)
|
|
@@ -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
|
package/typebox.d.ts
CHANGED
|
@@ -200,7 +200,7 @@ export type TIndexRestMany<T extends TSchema, K extends TPropertyKey[]> = K exte
|
|
|
200
200
|
export type TIndex<T extends TSchema, K extends TPropertyKey[]> = T extends TRecursive<infer S> ? TIndex<S, K> : T extends TIntersect ? UnionType<Flat<TIndexRestMany<T, K>>> : T extends TUnion ? UnionType<Flat<TIndexRestMany<T, K>>> : T extends TObject ? UnionType<Flat<TIndexRestMany<T, K>>> : T extends TTuple ? UnionType<Flat<TIndexRestMany<T, K>>> : TNever;
|
|
201
201
|
export type TIntrinsicMode = 'Uppercase' | 'Lowercase' | 'Capitalize' | 'Uncapitalize';
|
|
202
202
|
export type TIntrinsicTemplateLiteral<T extends TTemplateLiteralKind[], M extends TIntrinsicMode> = M extends ('Lowercase' | 'Uppercase') ? T extends [infer L, ...infer R] ? [TIntrinsic<AssertType<L>, M>, ...TIntrinsicTemplateLiteral<AssertRest<R>, M>] : T : M extends ('Capitalize' | 'Uncapitalize') ? T extends [infer L, ...infer R] ? [TIntrinsic<AssertType<L>, M>, ...R] : T : T;
|
|
203
|
-
export type TIntrinsicLiteral<T, M extends TIntrinsicMode> = T extends string ? M extends 'Uncapitalize' ? Uncapitalize<T> : M extends 'Capitalize' ? Capitalize<T> : M extends 'Uppercase' ? Uppercase<T> : M extends 'Lowercase' ? Lowercase<T> : string :
|
|
203
|
+
export type TIntrinsicLiteral<T, M extends TIntrinsicMode> = T extends string ? M extends 'Uncapitalize' ? Uncapitalize<T> : M extends 'Capitalize' ? Capitalize<T> : M extends 'Uppercase' ? Uppercase<T> : M extends 'Lowercase' ? Lowercase<T> : string : T;
|
|
204
204
|
export type TIntrinsicRest<T extends TSchema[], M extends TIntrinsicMode> = T extends [infer L, ...infer R] ? [TIntrinsic<AssertType<L>, M>, ...TIntrinsicRest<AssertRest<R>, M>] : [];
|
|
205
205
|
export type TIntrinsic<T extends TSchema, M extends TIntrinsicMode> = T extends TTemplateLiteral<infer S> ? TTemplateLiteral<TIntrinsicTemplateLiteral<S, M>> : T extends TUnion<infer S> ? TUnion<TIntrinsicRest<S, M>> : T extends TLiteral<infer S> ? TLiteral<TIntrinsicLiteral<S, M>> : T;
|
|
206
206
|
export interface TInteger extends TSchema, NumericOptions<number> {
|
package/typebox.js
CHANGED
|
@@ -1492,7 +1492,7 @@ var Intrinsic;
|
|
|
1492
1492
|
mode === 'Capitalize' ? Capitalize(value) :
|
|
1493
1493
|
mode === 'Uppercase' ? Uppercase(value) :
|
|
1494
1494
|
mode === 'Lowercase' ? Lowercase(value) :
|
|
1495
|
-
value) :
|
|
1495
|
+
value) : value.toString();
|
|
1496
1496
|
}
|
|
1497
1497
|
function IntrinsicRest(schema, mode) {
|
|
1498
1498
|
if (schema.length === 0)
|