typebox 1.0.75 → 1.0.76

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.
@@ -7,16 +7,7 @@ import { type TNarrow } from './narrow.mjs';
7
7
  import { type TEvaluateType } from './evaluate.mjs';
8
8
  import { type TEvaluateIntersect } from './evaluate.mjs';
9
9
  type TIsObjectLike<Type extends TSchema> = Type extends TObject | TTuple ? true : false;
10
- type TIsUnionOperand<Left extends TSchema, Right extends TSchema, IsUnionLeft extends boolean = Left extends TUnion ? true : false, IsUnionRight extends boolean = Right extends TUnion ? true : false, Result extends boolean = ([
11
- IsUnionLeft,
12
- IsUnionRight
13
- ] extends [true, true] ? true : [
14
- IsUnionLeft,
15
- IsUnionRight
16
- ] extends [false, true] ? true : [
17
- IsUnionLeft,
18
- IsUnionRight
19
- ] extends [true, false] ? true : false)> = Result;
10
+ type TIsUnionOperand<Left extends TSchema, Right extends TSchema, IsUnionLeft extends boolean = Left extends TUnion ? true : false, IsUnionRight extends boolean = Right extends TUnion ? true : false, Result extends boolean = IsUnionLeft extends true ? true : IsUnionRight extends true ? true : false> = Result;
20
11
  type TDistributeOperation<Left extends TSchema, Right extends TSchema, EvaluatedLeft extends TSchema = TEvaluateType<Left>, EvaluatedRight extends TSchema = TEvaluateType<Right>, IsUnionOperand extends boolean = TIsUnionOperand<EvaluatedLeft, EvaluatedRight>, IsObjectLeft extends boolean = TIsObjectLike<EvaluatedLeft>, IsObjectRight extends boolean = TIsObjectLike<EvaluatedRight>, Result extends TSchema = ([
21
12
  IsUnionOperand
22
13
  ] extends [true] ? TEvaluateIntersect<[EvaluatedLeft, EvaluatedRight]> : [
@@ -15,10 +15,8 @@ function IsObjectLike(type) {
15
15
  function IsUnionOperand(left, right) {
16
16
  const isUnionLeft = IsUnion(left);
17
17
  const isUnionRight = IsUnion(right);
18
- return (isUnionLeft && isUnionRight ? true :
19
- !isUnionLeft && isUnionRight ? true :
20
- isUnionLeft && !isUnionRight ? true :
21
- false);
18
+ const result = isUnionLeft || isUnionRight;
19
+ return result;
22
20
  }
23
21
  function DistributeOperation(left, right) {
24
22
  const evaluatedLeft = EvaluateType(left);
@@ -1,7 +1,6 @@
1
- import { type TUnreachable } from '../../../system/unreachable/index.mjs';
2
1
  import { type TSchema } from '../../types/schema.mjs';
3
2
  import { type TPattern } from '../../script/parser.mjs';
4
- /** Parses a Pattern into a sequence of TemplateLiteral types */
5
- export type TParsePatternIntoTypes<Pattern extends string, Parsed extends [TSchema[], string] | [] = TPattern<Pattern>, Result extends TSchema[] = Parsed extends [infer Types extends TSchema[], string] ? Types : TUnreachable> = Result;
6
- /** Parses a Pattern into a sequence of TemplateLiteral types */
3
+ /** Parses a Pattern into a sequence of TemplateLiteral types. A result of [] indicates failure to parse. */
4
+ export type TParsePatternIntoTypes<Pattern extends string, Parsed extends [TSchema[], string] | [] = TPattern<Pattern>, Result extends TSchema[] = Parsed extends [infer Types extends TSchema[], string] ? Types : []> = Result;
5
+ /** Parses a Pattern into a sequence of TemplateLiteral types. A result of [] indicates failure to parse. */
7
6
  export declare function ParsePatternIntoTypes<Pattern extends string>(pattern: Pattern): TParsePatternIntoTypes<Pattern>;
@@ -1,13 +1,11 @@
1
1
  // deno-fmt-ignore-file
2
- import { Unreachable } from '../../../system/unreachable/index.mjs';
3
2
  import { Guard } from '../../../guard/index.mjs';
4
3
  import { Pattern } from '../../script/parser.mjs';
5
- /** Parses a Pattern into a sequence of TemplateLiteral types */
4
+ /** Parses a Pattern into a sequence of TemplateLiteral types. A result of [] indicates failure to parse. */
6
5
  export function ParsePatternIntoTypes(pattern) {
7
6
  const parsed = Pattern(pattern);
8
7
  const result = Guard.IsEqual(parsed.length, 2)
9
8
  ? parsed[0]
10
- : Unreachable(); // []
9
+ : []; // Failed to Parse
11
10
  return result;
12
11
  }
13
- // deno-coverage-ignore-stop
@@ -1,8 +1,9 @@
1
1
  import { TUnreachable } from '../../../system/unreachable/index.mjs';
2
+ import { type TLiteral, type TLiteralValue } from '../../types/literal.mjs';
2
3
  import { type TSchema } from '../../types/schema.mjs';
3
- import { type TUnion } from '../../types/union.mjs';
4
4
  import { type TString } from '../../types/string.mjs';
5
- import { type TLiteral, type TLiteralValue } from '../../types/literal.mjs';
5
+ import { type TTemplateLiteral } from '../../types/template-literal.mjs';
6
+ import { type TUnion } from '../../types/union.mjs';
6
7
  import { type TParsePatternIntoTypes } from '../patterns/pattern.mjs';
7
8
  import { type TTemplateLiteralFinite } from './finite.mjs';
8
9
  type TFromLiteralPush<Variants extends string[], Value extends TLiteralValue, Result extends string[] = []> = Variants extends [infer Left extends string, ...infer Right extends string[]] ? TFromLiteralPush<Right, Value, [...Result, `${Left}${Value}`]> : Result;
@@ -14,7 +15,7 @@ type TVariantsToLiterals<Variants extends string[], Result extends TSchema[] = [
14
15
  type TDecodeTypesAsUnion<Types extends TSchema[], Variants extends string[] = TDecodeFromSpan<[], Types>, Literals extends TSchema[] = TVariantsToLiterals<Variants>, Result extends TSchema = TUnion<Literals>> = Result;
15
16
  type TDecodeTypes<Types extends TSchema[], Result extends TSchema = (Types extends [] ? TUnreachable : Types extends [infer Type extends TLiteral] ? Type : TDecodeTypesAsUnion<Types>)> = Result;
16
17
  /** Decodes a TemplateLiteral into a Type. If the TemplateLiteral yields a non-finite set, the return value is TString */
17
- export type TTemplateLiteralDecode<Pattern extends string, Types extends TSchema[] = TParsePatternIntoTypes<Pattern>, Finite extends boolean = TTemplateLiteralFinite<Types>, Result extends TSchema = (Finite extends true ? TDecodeTypes<Types> : TString)> = Result;
18
+ export type TTemplateLiteralDecode<Pattern extends string, Types extends TSchema[] = TParsePatternIntoTypes<Pattern>, Result extends TSchema = (Types extends [] ? TString : TTemplateLiteralFinite<Types> extends true ? TDecodeTypes<Types> : TTemplateLiteral<Pattern>)> = Result;
18
19
  /** Decodes a TemplateLiteral into a Type. If the TemplateLiteral yields a non-finite set, the return value is TString */
19
20
  export declare function TemplateLiteralDecode<Pattern extends string>(pattern: Pattern): TTemplateLiteralDecode<Pattern>;
20
21
  export {};
@@ -1,12 +1,13 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Unreachable } from '../../../system/unreachable/index.mjs';
3
3
  import { Guard } from '../../../guard/index.mjs';
4
+ import { Literal, IsLiteral } from '../../types/literal.mjs';
4
5
  import { IsSchema } from '../../types/schema.mjs';
5
- import { Union, IsUnion } from '../../types/union.mjs';
6
6
  import { String } from '../../types/string.mjs';
7
- import { Literal, IsLiteral } from '../../types/literal.mjs';
7
+ import { Union, IsUnion } from '../../types/union.mjs';
8
8
  import { ParsePatternIntoTypes } from '../patterns/pattern.mjs';
9
9
  import { TemplateLiteralFinite } from './finite.mjs';
10
+ import { TemplateLiteralCreate } from './create.mjs';
10
11
  function FromLiteralPush(variants, value, result = []) {
11
12
  const [left, ...right] = variants;
12
13
  return (Guard.IsString(left) ? FromLiteralPush(right, value, [...result, `${left}${value}`]) : result);
@@ -65,9 +66,10 @@ function DecodeTypes(types) {
65
66
  /** Decodes a TemplateLiteral into a Type. If the TemplateLiteral yields a non-finite set, the return value is TString */
66
67
  export function TemplateLiteralDecode(pattern) {
67
68
  const types = ParsePatternIntoTypes(pattern);
68
- const finite = TemplateLiteralFinite(types);
69
- const result = finite
70
- ? DecodeTypes(types)
71
- : String();
69
+ const result = Guard.IsEqual(types.length, 0) // Failed to Parse
70
+ ? String() // ... Pattern cannot be typed, so discard
71
+ : TemplateLiteralFinite(types)
72
+ ? DecodeTypes(types)
73
+ : TemplateLiteralCreate(pattern);
72
74
  return result;
73
75
  }
@@ -7,6 +7,7 @@ import { type TString } from './string.mjs';
7
7
  import { type TDeferred } from './deferred.mjs';
8
8
  import { type TInstantiate } from '../engine/instantiate.mjs';
9
9
  import { type TTemplateLiteralStatic } from '../engine/template-literal/index.mjs';
10
+ import { type TTemplateLiteralDecode } from '../engine/template-literal/decode.mjs';
10
11
  type StaticPropertyKey<Key extends string, Result extends PropertyKey = (Key extends TStringKey ? string : Key extends TIntegerKey ? number : Key extends TNumberKey ? number : Key extends `^${string}$` ? TTemplateLiteralStatic<Key> : string)> = Result;
11
12
  export type StaticRecord<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Key extends string, Value extends TSchema, StaticKey extends PropertyKey = StaticPropertyKey<Key>, StaticValue extends unknown = StaticType<Stack, Direction, Context, This, Value>> = Record<StaticKey, StaticValue>;
12
13
  export type TStringKey = typeof StringKey;
@@ -37,7 +38,7 @@ export type TRecordPattern<Type extends TRecord, Result extends string = Extract
37
38
  /** Returns the raw string pattern used for the Record key */
38
39
  export declare function RecordPattern<Type extends TRecord>(type: Type): TRecordPattern<Type>;
39
40
  /** Returns the Record key as a TypeBox type */
40
- export type TRecordKey<Type extends TRecord, Pattern extends string = TRecordPattern<Type>, Result extends TSchema = (Pattern extends typeof IntegerKey ? TInteger : Pattern extends typeof NumberKey ? TNumber : TString)> = Result;
41
+ export type TRecordKey<Type extends TRecord, Pattern extends string = TRecordPattern<Type>, Result extends TSchema = (Pattern extends typeof StringKey ? TString : Pattern extends typeof IntegerKey ? TInteger : Pattern extends typeof NumberKey ? TNumber : TTemplateLiteralDecode<Pattern>)> = Result;
41
42
  /** Returns the Record key as a TypeBox type */
42
43
  export declare function RecordKey<Type extends TRecord>(type: Type): TRecordKey<Type>;
43
44
  export type TRecordValue<Type extends TRecord, Result extends TSchema = Type['patternProperties'][TRecordPattern<Type>]> = Result;
@@ -7,6 +7,7 @@ import { Number, NumberPattern } from './number.mjs';
7
7
  import { String, StringPattern } from './string.mjs';
8
8
  import { Deferred } from './deferred.mjs';
9
9
  import { Instantiate } from '../engine/instantiate.mjs';
10
+ import { TemplateLiteralDecode } from '../engine/template-literal/decode.mjs';
10
11
  import { CreateRecord } from '../engine/record/record-create.mjs';
11
12
  export const IntegerKey = `^${IntegerPattern}$`;
12
13
  export const NumberKey = `^${NumberPattern}$`;
@@ -39,9 +40,10 @@ export function RecordPattern(type) {
39
40
  /** Returns the Record key as a TypeBox type */
40
41
  export function RecordKey(type) {
41
42
  const pattern = RecordPattern(type);
42
- const result = (Guard.IsEqual(pattern, IntegerKey) ? Integer() :
43
- Guard.IsEqual(pattern, NumberKey) ? Number() :
44
- String());
43
+ const result = (Guard.IsEqual(pattern, StringKey) ? String() :
44
+ Guard.IsEqual(pattern, IntegerKey) ? Integer() :
45
+ Guard.IsEqual(pattern, NumberKey) ? Number() :
46
+ TemplateLiteralDecode(pattern));
45
47
  return result;
46
48
  }
47
49
  export function RecordValue(type) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typebox",
3
3
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
4
- "version": "1.0.75",
4
+ "version": "1.0.76",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"