@sinclair/typebox 0.30.0 → 0.30.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.30.0",
3
+ "version": "0.30.2",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -84,6 +84,7 @@ License MIT
84
84
  - [Recursive](#types-recursive)
85
85
  - [Conditional](#types-conditional)
86
86
  - [Template Literal](#types-template-literal)
87
+ - [Intrinsic String](#types-intrinsic-string)
87
88
  - [Indexed](#types-indexed)
88
89
  - [Negated](#types-negated)
89
90
  - [Rest](#types-rest)
@@ -486,6 +487,30 @@ The following table lists the Standard TypeBox types. These types are fully comp
486
487
  │ ]) │ │ } │
487
488
  │ │ │ │
488
489
  ├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
490
+ │ const T = Type.Uncapitalize( │ type T = Uncapitalize< │ const T = { │
491
+ │ Type.Literal('Hello') │ 'Hello' │ type: 'string', │
492
+ │ ) │ > │ const: 'hello' │
493
+ │ │ │ } │
494
+ │ │ │ │
495
+ ├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
496
+ │ const T = Type.Capitalize( │ type T = Capitalize< │ const T = { │
497
+ │ Type.Literal('hello') │ 'hello' │ type: 'string', │
498
+ │ ) │ > │ const: 'Hello' │
499
+ │ │ │ } │
500
+ │ │ │ │
501
+ ├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
502
+ │ const T = Type.Uppercase( │ type T = Uppercase< │ const T = { │
503
+ │ Type.Literal('hello') │ 'hello' │ type: 'string', │
504
+ │ ) │ > │ const: 'HELLO' │
505
+ │ │ │ } │
506
+ │ │ │ │
507
+ ├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
508
+ │ const T = Type.Lowercase( │ type T = Lowercase< │ const T = { │
509
+ │ Type.Literal('HELLO') │ 'HELLO' │ type: 'string', │
510
+ │ ) │ > │ const: 'hello' │
511
+ │ │ │ } │
512
+ │ │ │ │
513
+ ├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
489
514
  │ const T = Type.Object({ │ type T = { │ const R = { │
490
515
  │ x: Type.Number(), │ x: number, │ $ref: 'T' │
491
516
  │ y: Type.Number() │ y: number │ } │
@@ -851,6 +876,34 @@ const R = Type.Record(T, Type.String()) // const R = {
851
876
  // }
852
877
  ```
853
878
 
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
+
854
907
  <a name='types-indexed'></a>
855
908
 
856
909
  ### Indexed Access Types
@@ -1586,11 +1639,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
1586
1639
  ┌──────────────────────┬────────────┬────────────┬─────────────┐
1587
1640
  │ (index) │ Compiled │ Minified │ Compression │
1588
1641
  ├──────────────────────┼────────────┼────────────┼─────────────┤
1589
- │ typebox/compiler │ '129.4 kb' │ ' 58.6 kb' │ '2.21 x' │
1590
- │ typebox/errors │ '111.6 kb' │ ' 50.1 kb' │ '2.23 x' │
1591
- │ typebox/system │ ' 76.5 kb' │ ' 31.7 kb' │ '2.41 x' │
1592
- │ typebox/value │ '180.7 kb' │ ' 79.3 kb' │ '2.28 x' │
1593
- │ typebox │ ' 75.4 kb' │ ' 31.3 kb' │ '2.41 x' │
1642
+ │ typebox/compiler │ '131.4 kb' │ ' 59.4 kb' │ '2.21 x' │
1643
+ │ typebox/errors │ '113.6 kb' │ ' 50.9 kb' │ '2.23 x' │
1644
+ │ typebox/system │ ' 78.5 kb' │ ' 32.5 kb' │ '2.42 x' │
1645
+ │ typebox/value │ '182.8 kb' │ ' 80.0 kb' │ '2.28 x' │
1646
+ │ typebox │ ' 77.4 kb' │ ' 32.0 kb' │ '2.42 x' │
1594
1647
  └──────────────────────┴────────────┴────────────┴─────────────┘
1595
1648
  ```
1596
1649
 
package/typebox.d.ts CHANGED
@@ -198,6 +198,11 @@ export type TIndexType<T extends TSchema, K extends TPropertyKey> = T extends TR
198
198
  export type TIndexRestMany<T extends TSchema, K extends TPropertyKey[]> = K extends [infer L, ...infer R] ? [TIndexType<T, Assert<L, TPropertyKey>>, ...TIndexRestMany<T, Assert<R, TPropertyKey[]>>] : [
199
199
  ];
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
+ export type TIntrinsicMode = 'Uppercase' | 'Lowercase' | 'Capitalize' | 'Uncapitalize';
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 : T;
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
+ 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;
201
206
  export interface TInteger extends TSchema, NumericOptions<number> {
202
207
  [Kind]: 'Integer';
203
208
  static: number;
@@ -626,6 +631,10 @@ export declare namespace TypeClone {
626
631
  export declare namespace IndexedAccessor {
627
632
  function Resolve(schema: TSchema, keys: TPropertyKey[], options?: SchemaOptions): TSchema;
628
633
  }
634
+ export declare namespace Intrinsic {
635
+ /** Applies an intrinsic string manipulation to the given type. */
636
+ function Map<T extends TSchema, M extends TIntrinsicMode>(schema: T, mode: M): TIntrinsic<T, M>;
637
+ }
629
638
  export declare namespace ObjectMap {
630
639
  function Map<T = TSchema>(schema: TSchema, callback: (object: TObject) => TObject, options: SchemaOptions): T;
631
640
  }
@@ -705,8 +714,8 @@ export declare class StandardTypeBuilder extends TypeBuilder {
705
714
  Array<T extends TSchema>(schema: T, options?: ArrayOptions): TArray<T>;
706
715
  /** `[Standard]` Creates a Boolean type */
707
716
  Boolean(options?: SchemaOptions): TBoolean;
708
- /** `[Standard]` Capitalize a LiteralString type */
709
- Capitalize<T extends TLiteral<string>>(schema: T, options?: SchemaOptions): TLiteral<Capitalize<T['const']>>;
717
+ /** `[Standard]` Intrinsic function to Capitalize LiteralString types */
718
+ Capitalize<T extends TSchema>(schema: T, options?: SchemaOptions): TIntrinsic<T, 'Capitalize'>;
710
719
  /** `[Standard]` Creates a Composite object type */
711
720
  Composite<T extends TObject[]>(objects: [...T], options?: ObjectOptions): TComposite<T>;
712
721
  /** `[Standard]` Creates a Enum type */
@@ -743,8 +752,8 @@ export declare class StandardTypeBuilder extends TypeBuilder {
743
752
  KeyOf<T extends TSchema>(schema: T, options?: SchemaOptions): TKeyOf<T>;
744
753
  /** `[Standard]` Creates a Literal type */
745
754
  Literal<T extends TLiteralValue>(value: T, options?: SchemaOptions): TLiteral<T>;
746
- /** `[Standard]` Lowercase a LiteralString type */
747
- Lowercase<T extends TLiteral<string>>(schema: T, options?: SchemaOptions): TLiteral<Lowercase<T['const']>>;
755
+ /** `[Standard]` Intrinsic function to Lowercase LiteralString types */
756
+ Lowercase<T extends TSchema>(schema: T, options?: SchemaOptions): TIntrinsic<T, 'Lowercase'>;
748
757
  /** `[Standard]` Creates a Never type */
749
758
  Never(options?: SchemaOptions): TNever;
750
759
  /** `[Standard]` Creates a Not type */
@@ -805,8 +814,8 @@ export declare class StandardTypeBuilder extends TypeBuilder {
805
814
  TemplateLiteral<T extends TTemplateLiteralKind[]>(kinds: [...T], options?: SchemaOptions): TTemplateLiteral<T>;
806
815
  /** `[Standard]` Creates a Tuple type */
807
816
  Tuple<T extends TSchema[]>(items: [...T], options?: SchemaOptions): TTuple<T>;
808
- /** `[Standard]` Uncapitalize a LiteralString type */
809
- Uncapitalize<T extends TLiteral<string>>(schema: T, options?: SchemaOptions): TLiteral<Uncapitalize<T['const']>>;
817
+ /** `[Standard]` Intrinsic function to Uncapitalize LiteralString types */
818
+ Uncapitalize<T extends TSchema>(schema: T, options?: SchemaOptions): TIntrinsic<T, 'Uncapitalize'>;
810
819
  /** `[Standard]` Creates a Union type */
811
820
  Union(anyOf: [], options?: SchemaOptions): TNever;
812
821
  /** `[Standard]` Creates a Union type */
@@ -819,8 +828,8 @@ export declare class StandardTypeBuilder extends TypeBuilder {
819
828
  Unknown(options?: SchemaOptions): TUnknown;
820
829
  /** `[Standard]` Creates a Unsafe type that will infers as the generic argument T */
821
830
  Unsafe<T>(options?: UnsafeOptions): TUnsafe<T>;
822
- /** `[Standard]` Uppercase a LiteralString type */
823
- Uppercase<T extends TLiteral<string>>(schema: T, options?: SchemaOptions): TLiteral<Uppercase<T['const']>>;
831
+ /** `[Standard]` Intrinsic function to Uppercase LiteralString types */
832
+ Uppercase<T extends TSchema>(schema: T, options?: SchemaOptions): TIntrinsic<T, 'Uppercase'>;
824
833
  }
825
834
  export declare class ExtendedTypeBuilder extends StandardTypeBuilder {
826
835
  /** `[Extended]` Creates a AsyncIterator type */
package/typebox.js CHANGED
@@ -27,7 +27,7 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.TemplateLiteralDslParser = exports.TemplateLiteralGenerator = exports.TemplateLiteralFinite = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.UnionResolver = exports.KeyArrayResolver = exports.KeyResolver = exports.ObjectMap = exports.IndexedAccessor = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.ValueGuard = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Optional = exports.Readonly = void 0;
30
+ exports.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.TemplateLiteralDslParser = exports.TemplateLiteralGenerator = exports.TemplateLiteralFinite = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.UnionResolver = exports.KeyArrayResolver = exports.KeyResolver = exports.ObjectMap = exports.Intrinsic = exports.IndexedAccessor = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.ValueGuard = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Optional = exports.Readonly = void 0;
31
31
  // --------------------------------------------------------------------------
32
32
  // Symbols
33
33
  // --------------------------------------------------------------------------
@@ -477,7 +477,14 @@ var TypeGuard;
477
477
  TypeGuard.TRef = TRef;
478
478
  /** Returns true if the given schema is TString */
479
479
  function TString(schema) {
480
- return (TKindOf(schema, 'String') && schema.type === 'string' && IsOptionalString(schema.$id) && IsOptionalNumber(schema.minLength) && IsOptionalNumber(schema.maxLength) && IsOptionalPattern(schema.pattern) && IsOptionalFormat(schema.format));
480
+ // prettier-ignore
481
+ return (TKindOf(schema, 'String') &&
482
+ schema.type === 'string' &&
483
+ IsOptionalString(schema.$id) &&
484
+ IsOptionalNumber(schema.minLength) &&
485
+ IsOptionalNumber(schema.maxLength) &&
486
+ IsOptionalPattern(schema.pattern) &&
487
+ IsOptionalFormat(schema.format));
481
488
  }
482
489
  TypeGuard.TString = TString;
483
490
  /** Returns true if the given schema is TSymbol */
@@ -1448,6 +1455,67 @@ var IndexedAccessor;
1448
1455
  IndexedAccessor.Resolve = Resolve;
1449
1456
  })(IndexedAccessor || (exports.IndexedAccessor = IndexedAccessor = {}));
1450
1457
  // --------------------------------------------------------------------------
1458
+ // Intrinsic
1459
+ // --------------------------------------------------------------------------
1460
+ var Intrinsic;
1461
+ (function (Intrinsic) {
1462
+ function Uncapitalize(value) {
1463
+ const [first, rest] = [value.slice(0, 1), value.slice(1)];
1464
+ return `${first.toLowerCase()}${rest}`;
1465
+ }
1466
+ function Capitalize(value) {
1467
+ const [first, rest] = [value.slice(0, 1), value.slice(1)];
1468
+ return `${first.toUpperCase()}${rest}`;
1469
+ }
1470
+ function Uppercase(value) {
1471
+ return value.toUpperCase();
1472
+ }
1473
+ function Lowercase(value) {
1474
+ return value.toLowerCase();
1475
+ }
1476
+ function IntrinsicTemplateLiteral(schema, mode) {
1477
+ // note: template literals require special runtime handling as they are encoded in string patterns.
1478
+ // This diverges from the mapped type which would otherwise map on the template literal kind.
1479
+ const expression = TemplateLiteralParser.ParseExact(schema.pattern);
1480
+ const finite = TemplateLiteralFinite.Check(expression);
1481
+ if (!finite)
1482
+ return { ...schema, pattern: IntrinsicLiteral(schema.pattern, mode) };
1483
+ const strings = [...TemplateLiteralGenerator.Generate(expression)];
1484
+ const literals = strings.map((value) => exports.Type.Literal(value));
1485
+ const mapped = IntrinsicRest(literals, mode);
1486
+ const union = exports.Type.Union(mapped);
1487
+ return exports.Type.TemplateLiteral([union]);
1488
+ }
1489
+ function IntrinsicLiteral(value, mode) {
1490
+ // prettier-ignore
1491
+ return typeof value === 'string' ? (mode === 'Uncapitalize' ? Uncapitalize(value) :
1492
+ mode === 'Capitalize' ? Capitalize(value) :
1493
+ mode === 'Uppercase' ? Uppercase(value) :
1494
+ mode === 'Lowercase' ? Lowercase(value) :
1495
+ value) : value.toString();
1496
+ }
1497
+ function IntrinsicRest(schema, mode) {
1498
+ if (schema.length === 0)
1499
+ return [];
1500
+ const [L, ...R] = schema;
1501
+ return [Map(L, mode), ...IntrinsicRest(R, mode)];
1502
+ }
1503
+ function Visit(schema, mode) {
1504
+ if (TypeGuard.TTemplateLiteral(schema))
1505
+ return IntrinsicTemplateLiteral(schema, mode);
1506
+ if (TypeGuard.TUnion(schema))
1507
+ return exports.Type.Union(IntrinsicRest(schema.anyOf, mode));
1508
+ if (TypeGuard.TLiteral(schema))
1509
+ return exports.Type.Literal(IntrinsicLiteral(schema.const, mode));
1510
+ return schema;
1511
+ }
1512
+ /** Applies an intrinsic string manipulation to the given type. */
1513
+ function Map(schema, mode) {
1514
+ return Visit(schema, mode);
1515
+ }
1516
+ Intrinsic.Map = Map;
1517
+ })(Intrinsic || (exports.Intrinsic = Intrinsic = {}));
1518
+ // --------------------------------------------------------------------------
1451
1519
  // ObjectMap
1452
1520
  // --------------------------------------------------------------------------
1453
1521
  var ObjectMap;
@@ -1952,10 +2020,9 @@ class StandardTypeBuilder extends TypeBuilder {
1952
2020
  Boolean(options = {}) {
1953
2021
  return this.Create({ ...options, [exports.Kind]: 'Boolean', type: 'boolean' });
1954
2022
  }
1955
- /** `[Standard]` Capitalize a LiteralString type */
2023
+ /** `[Standard]` Intrinsic function to Capitalize LiteralString types */
1956
2024
  Capitalize(schema, options = {}) {
1957
- const [first, rest] = [schema.const.slice(0, 1), schema.const.slice(1)];
1958
- return exports.Type.Literal(`${first.toUpperCase()}${rest}`, options);
2025
+ return { ...Intrinsic.Map(TypeClone.Clone(schema), 'Capitalize'), ...options };
1959
2026
  }
1960
2027
  /** `[Standard]` Creates a Composite object type */
1961
2028
  Composite(objects, options) {
@@ -2076,9 +2143,9 @@ class StandardTypeBuilder extends TypeBuilder {
2076
2143
  Literal(value, options = {}) {
2077
2144
  return this.Create({ ...options, [exports.Kind]: 'Literal', const: value, type: typeof value });
2078
2145
  }
2079
- /** `[Standard]` Lowercase a LiteralString type */
2146
+ /** `[Standard]` Intrinsic function to Lowercase LiteralString types */
2080
2147
  Lowercase(schema, options = {}) {
2081
- return exports.Type.Literal(schema.const.toLowerCase(), options);
2148
+ return { ...Intrinsic.Map(TypeClone.Clone(schema), 'Lowercase'), ...options };
2082
2149
  }
2083
2150
  /** `[Standard]` Creates a Never type */
2084
2151
  Never(options = {}) {
@@ -2249,10 +2316,9 @@ class StandardTypeBuilder extends TypeBuilder {
2249
2316
  { ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
2250
2317
  return this.Create(schema);
2251
2318
  }
2252
- /** `[Standard]` Uncapitalize a LiteralString type */
2319
+ /** `[Standard]` Intrinsic function to Uncapitalize LiteralString types */
2253
2320
  Uncapitalize(schema, options = {}) {
2254
- const [first, rest] = [schema.const.slice(0, 1), schema.const.slice(1)];
2255
- return exports.Type.Literal(`${first.toLocaleLowerCase()}${rest}`, options);
2321
+ return { ...Intrinsic.Map(TypeClone.Clone(schema), 'Uncapitalize'), ...options };
2256
2322
  }
2257
2323
  /** `[Standard]` Creates a Union type */
2258
2324
  Union(union, options = {}) {
@@ -2277,9 +2343,9 @@ class StandardTypeBuilder extends TypeBuilder {
2277
2343
  Unsafe(options = {}) {
2278
2344
  return this.Create({ ...options, [exports.Kind]: options[exports.Kind] || 'Unsafe' });
2279
2345
  }
2280
- /** `[Standard]` Uppercase a LiteralString type */
2346
+ /** `[Standard]` Intrinsic function to Uppercase LiteralString types */
2281
2347
  Uppercase(schema, options = {}) {
2282
- return exports.Type.Literal(schema.const.toUpperCase(), options);
2348
+ return { ...Intrinsic.Map(TypeClone.Clone(schema), 'Uppercase'), ...options };
2283
2349
  }
2284
2350
  }
2285
2351
  exports.StandardTypeBuilder = StandardTypeBuilder;