typebox 1.3.7 → 1.3.9

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.
@@ -33,6 +33,9 @@ export declare function IsGreaterEqualThan(left: string, right: string): string;
33
33
  export declare function IsMinLength(value: string, length: string): string;
34
34
  export declare function IsMaxLength(value: string, length: string): string;
35
35
  export declare function Every(value: string, offset: string, params: [value: string, index: string], expression: string): string;
36
+ export declare function Some(value: string, params: [value: string, index: string], expression: string): string;
37
+ export declare function SomeAll(value: string, params: [value: string, index: string], expression: string): string;
38
+ export declare function Counted(value: string, params: [value: string, index: string], expression: string): string;
36
39
  export declare function Entries(value: string): string;
37
40
  export declare function Keys(value: string): string;
38
41
  export declare function HasPropertyKey(value: string, key: string): string;
@@ -50,5 +53,4 @@ export declare function If(condition: string, then: string): string;
50
53
  export declare function Return(expression: string): string;
51
54
  export declare function ReduceAnd(operands: string[]): string;
52
55
  export declare function ReduceOr(operands: string[]): string;
53
- export declare function PrefixIncrement(expression: string): string;
54
56
  export declare function MultipleOf(dividend: string, divisor: string): string;
@@ -110,6 +110,15 @@ export function Every(value, offset, params, expression) {
110
110
  ? `${value}.every((${params[0]}, ${params[1]}) => ${expression})`
111
111
  : `((value, callback) => { for(let index = ${offset}; index < value.length; index++) if (!callback(value[index], index)) return false; return true })(${value}, (${params[0]}, ${params[1]}) => ${expression})`;
112
112
  }
113
+ export function Some(value, params, expression) {
114
+ return `${value}.some((${params[0]}, ${params[1]}) => ${expression})`;
115
+ }
116
+ export function SomeAll(value, params, expression) {
117
+ return `((value, callback) => { let result = false; for(let index = 0; index < value.length; index++) if (callback(value[index], index)) result = true; return result })(${value}, (${params[0]}, ${params[1]}) => ${expression})`;
118
+ }
119
+ export function Counted(value, params, expression) {
120
+ return `${value}.reduce((result, ${params[0]}, ${params[1]}) => (${expression}) ? ++result : result, 0)`;
121
+ }
113
122
  // --------------------------------------------------------------------------
114
123
  // Objects
115
124
  // --------------------------------------------------------------------------
@@ -145,6 +154,8 @@ export function Member(left, right) {
145
154
  return `${left}${IsIdentifier(right) ? `.${right}` : `[${Constant(right)}]`}`;
146
155
  }
147
156
  export function Constant(value) {
157
+ if (!G.IsValueLike(value))
158
+ throw Error('Unsupported Constant');
148
159
  return G.IsString(value) ? JSON.stringify(value) : `${value}`;
149
160
  }
150
161
  export function Ternary(condition, true_, false_) {
@@ -172,15 +183,11 @@ export function ReduceAnd(operands) {
172
183
  return G.IsEqual(operands.length, 0) ? 'true' : operands.reduce((left, right) => And(left, right));
173
184
  }
174
185
  export function ReduceOr(operands) {
175
- // deno-coverage-ignore - we never observe 0 operands
176
186
  return G.IsEqual(operands.length, 0) ? 'false' : operands.reduce((left, right) => Or(left, right));
177
187
  }
178
188
  // --------------------------------------------------------------------------
179
- // Arithmetic
189
+ // MultipleOf
180
190
  // --------------------------------------------------------------------------
181
- export function PrefixIncrement(expression) {
182
- return `++${expression}`;
183
- }
184
191
  export function MultipleOf(dividend, divisor) {
185
192
  return `Guard.IsMultipleOf(${dividend}, ${divisor})`;
186
193
  }
@@ -39,10 +39,16 @@ export declare function GraphemeCount(value: string): number;
39
39
  export declare function IsMaxLength(value: string, length: number): boolean;
40
40
  /** Returns true if the string has at least the given number of graphemes */
41
41
  export declare function IsMinLength(value: string, length: number): boolean;
42
- /** Returns true if all elements from offset satisfy the callback, short-circuiting on the first failure */
42
+ /** Returns true if every element from offset satisfies the callback, short-circuiting on the first failure */
43
43
  export declare function Every<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
44
- /** Returns true if all elements from offset satisfy the callback, visiting every element regardless of failure */
44
+ /** Returns true if every element from offset satisfies the callback, using exhaustive enumeration */
45
45
  export declare function EveryAll<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
46
+ /** Returns true if some element satisfies the callback, short-circuiting on the first success */
47
+ export declare function Some<T>(value: T[], callback: (value: T, index: number) => boolean): boolean;
48
+ /** Returns true if some element satisfies the callback, using exhaustive enumeration */
49
+ export declare function SomeAll<T>(value: T[], callback: (value: T, index: number) => boolean): boolean;
50
+ /** Returns the count of elements that satisfy the callback */
51
+ export declare function Counted(value: unknown[], callback: (value: unknown, index: number) => boolean): number;
46
52
  /** Shifts the left-most element from an array and dispatches to the true arm, or the false arm if empty */
47
53
  export declare function ShiftLeft<T, True extends (left: T, right: T[]) => unknown, False extends () => unknown>(array: T[], true_: True, false_: False): ReturnType<True> | ReturnType<False>;
48
54
  /** Returns true if the PropertyKey is Unsafe (ref: prototype-pollution). */
@@ -138,7 +138,7 @@ export function IsMinLength(value, length) {
138
138
  // --------------------------------------------------------------------------
139
139
  // Array
140
140
  // --------------------------------------------------------------------------
141
- /** Returns true if all elements from offset satisfy the callback, short-circuiting on the first failure */
141
+ /** Returns true if every element from offset satisfies the callback, short-circuiting on the first failure */
142
142
  export function Every(value, offset, callback) {
143
143
  for (let index = offset; index < value.length; index++) {
144
144
  if (!callback(value[index], index))
@@ -146,7 +146,7 @@ export function Every(value, offset, callback) {
146
146
  }
147
147
  return true;
148
148
  }
149
- /** Returns true if all elements from offset satisfy the callback, visiting every element regardless of failure */
149
+ /** Returns true if every element from offset satisfies the callback, using exhaustive enumeration */
150
150
  export function EveryAll(value, offset, callback) {
151
151
  let result = true;
152
152
  for (let index = offset; index < value.length; index++) {
@@ -155,6 +155,27 @@ export function EveryAll(value, offset, callback) {
155
155
  }
156
156
  return result;
157
157
  }
158
+ /** Returns true if some element satisfies the callback, short-circuiting on the first success */
159
+ export function Some(value, callback) {
160
+ for (let index = 0; index < value.length; index++) {
161
+ if (callback(value[index], index))
162
+ return true;
163
+ }
164
+ return false;
165
+ }
166
+ /** Returns true if some element satisfies the callback, using exhaustive enumeration */
167
+ export function SomeAll(value, callback) {
168
+ let result = false;
169
+ for (let index = 0; index < value.length; index++) {
170
+ if (callback(value[index], index))
171
+ result = true;
172
+ }
173
+ return result;
174
+ }
175
+ /** Returns the count of elements that satisfy the callback */
176
+ export function Counted(value, callback) {
177
+ return value.reduce((result, value, index) => callback(value, index) ? ++result : result, 0);
178
+ }
158
179
  /** Shifts the left-most element from an array and dispatches to the true arm, or the false arm if empty */
159
180
  export function ShiftLeft(array, true_, false_) {
160
181
  return (IsEqual(array.length, 0) ? false_() : true_(array[0], array.slice(1)));
@@ -7,10 +7,10 @@ import { Guard as G, EmitGuard as E } from '../../guard/index.mjs';
7
7
  function HasUnevaluatedFromObject(value) {
8
8
  return (Schema.IsUnevaluatedItems(value)
9
9
  || Schema.IsUnevaluatedProperties(value)
10
- || G.Keys(value).some(key => HasUnevaluatedFromUnknown(value[key])));
10
+ || G.Some(G.Keys(value), key => HasUnevaluatedFromUnknown(value[key])));
11
11
  }
12
12
  function HasUnevaluatedFromArray(value) {
13
- return value.some(value => HasUnevaluatedFromUnknown(value));
13
+ return G.Some(value, value => HasUnevaluatedFromUnknown(value));
14
14
  }
15
15
  function HasUnevaluatedFromUnknown(value) {
16
16
  return (G.IsArray(value) ? HasUnevaluatedFromArray(value) :
@@ -18,7 +18,7 @@ function HasUnevaluatedFromUnknown(value) {
18
18
  false);
19
19
  }
20
20
  export function HasUnevaluated(context, schema) {
21
- return HasUnevaluatedFromUnknown(schema) || G.Keys(context).some(key => HasUnevaluatedFromUnknown(context[key]));
21
+ return HasUnevaluatedFromUnknown(schema) || G.Some(G.Keys(context), key => HasUnevaluatedFromUnknown(context[key]));
22
22
  }
23
23
  // ------------------------------------------------------------------
24
24
  // BuildContext
@@ -1,5 +1,4 @@
1
1
  // deno-fmt-ignore-file
2
- import { Hashing } from '../../system/hashing/index.mjs';
3
2
  import { EmitGuard as E } from '../../guard/index.mjs';
4
3
  import { BuildSchema } from './schema.mjs';
5
4
  const index = [0];
@@ -9,7 +8,7 @@ const funcs = new Map();
9
8
  // CreateName
10
9
  // ------------------------------------------------------------------
11
10
  function NextName() {
12
- return Hashing.Hash(index[0]++);
11
+ return `${index[0]++}`;
13
12
  }
14
13
  function CreateName(schema, href) {
15
14
  if (!names.has(schema))
@@ -12,15 +12,25 @@ function IsValid(schema) {
12
12
  // ------------------------------------------------------------------
13
13
  // Build
14
14
  // ------------------------------------------------------------------
15
- export function BuildAdditionalItems(stack, context, schema, value) {
16
- if (!IsValid(schema))
17
- return E.Constant(true);
15
+ function BuildAdditionalItemsStandard(stack, context, schema, value) {
18
16
  const [item, index] = [Unique(), Unique()];
19
17
  const isSchema = BuildSchemaPushStack(stack, context, schema.additionalItems, item);
20
18
  const isLength = E.IsLessThan(index, E.Constant(schema.items.length));
21
19
  const addIndex = context.AddIndex(index);
22
- const guarded = context.UseUnevaluated() ? E.Or(isLength, E.And(isSchema, addIndex)) : E.Or(isLength, isSchema);
23
- return E.Call(E.Member(value, 'every'), [E.ArrowFunction([item, index], guarded)]);
20
+ return E.Every(value, E.Constant(0), [item, index], E.Or(isLength, E.And(isSchema, addIndex)));
21
+ }
22
+ function BuildAdditionalItemsFast(stack, context, schema, value) {
23
+ const [item, index] = [Unique(), Unique()];
24
+ const isSchema = BuildSchemaPushStack(stack, context, schema.additionalItems, item);
25
+ const isLength = E.IsLessThan(index, E.Constant(schema.items.length));
26
+ return E.Every(value, E.Constant(0), [item, index], E.Or(isLength, isSchema));
27
+ }
28
+ export function BuildAdditionalItems(stack, context, schema, value) {
29
+ if (!IsValid(schema))
30
+ return E.Constant(true);
31
+ return context.UseUnevaluated()
32
+ ? BuildAdditionalItemsStandard(stack, context, schema, value)
33
+ : BuildAdditionalItemsFast(stack, context, schema, value);
24
34
  }
25
35
  // ------------------------------------------------------------------
26
36
  // Check
@@ -28,7 +38,7 @@ export function BuildAdditionalItems(stack, context, schema, value) {
28
38
  export function CheckAdditionalItems(stack, context, schema, value) {
29
39
  if (!IsValid(schema))
30
40
  return true;
31
- const isAdditionalItems = value.every((item, index) => {
41
+ const isAdditionalItems = G.Every(value, 0, (item, index) => {
32
42
  return G.IsLessThan(index, schema.items.length)
33
43
  || (CheckSchemaPushStack(stack, context, schema.additionalItems, item) && context.AddIndex(index));
34
44
  });
@@ -40,7 +50,7 @@ export function CheckAdditionalItems(stack, context, schema, value) {
40
50
  export function ErrorAdditionalItems(stack, context, schemaPath, instancePath, schema, value) {
41
51
  if (!IsValid(schema))
42
52
  return true;
43
- const isAdditionalItems = value.every((item, index) => {
53
+ const isAdditionalItems = G.Every(value, 0, (item, index) => {
44
54
  const nextSchemaPath = `${schemaPath}/additionalItems`;
45
55
  const nextInstancePath = `${instancePath}/${index}`;
46
56
  return G.IsLessThan(index, schema.items.length) ||
@@ -12,13 +12,24 @@ function IsValid(schema) {
12
12
  // ------------------------------------------------------------------
13
13
  // Build
14
14
  // ------------------------------------------------------------------
15
+ function BuildContainsStandard(stack, context, schema, value) {
16
+ const [item, index] = [Unique(), Unique()];
17
+ const isLength = E.Not(E.IsEqual(E.Member(value, 'length'), E.Constant(0)));
18
+ const isSome = E.SomeAll(value, [item, index], E.And(BuildSchema(stack, context, schema.contains, item), context.AddIndex(index)));
19
+ return E.And(isLength, isSome);
20
+ }
21
+ function BuildContainsFast(stack, context, schema, value) {
22
+ const [item] = [Unique()];
23
+ const isLength = E.Not(E.IsEqual(E.Member(value, 'length'), E.Constant(0)));
24
+ const isSome = E.Some(value, [item, '_'], BuildSchema(stack, context, schema.contains, item));
25
+ return E.And(isLength, isSome);
26
+ }
15
27
  export function BuildContains(stack, context, schema, value) {
16
28
  if (!IsValid(schema))
17
29
  return E.Constant(true);
18
- const item = Unique();
19
- const isLength = E.Not(E.IsEqual(E.Member(value, 'length'), E.Constant(0)));
20
- const isSome = E.Call(E.Member(value, 'some'), [E.ArrowFunction([item], BuildSchema(stack, context, schema.contains, item))]);
21
- return E.And(isLength, isSome);
30
+ return context.UseUnevaluated()
31
+ ? BuildContainsStandard(stack, context, schema, value)
32
+ : BuildContainsFast(stack, context, schema, value);
22
33
  }
23
34
  // ------------------------------------------------------------------
24
35
  // Check
@@ -26,8 +37,9 @@ export function BuildContains(stack, context, schema, value) {
26
37
  export function CheckContains(stack, context, schema, value) {
27
38
  if (!IsValid(schema))
28
39
  return true;
29
- return !G.IsEqual(value.length, 0) &&
30
- value.some((item) => CheckSchema(stack, context, schema.contains, item));
40
+ return !G.IsEqual(value.length, 0) && G.SomeAll(value, (item, index) => {
41
+ return CheckSchema(stack, context, schema.contains, item) && context.AddIndex(index);
42
+ });
31
43
  }
32
44
  // ------------------------------------------------------------------
33
45
  // Error
@@ -16,7 +16,7 @@ export function BuildEnum(_stack, _context, schema, value) {
16
16
  // Check
17
17
  // ------------------------------------------------------------------
18
18
  export function CheckEnum(_stack, _context, schema, value) {
19
- return schema.enum.some(option => G.IsValueLike(option)
19
+ return G.Some(schema.enum, option => G.IsValueLike(option)
20
20
  ? G.IsEqual(value, option)
21
21
  : G.IsDeepEqual(value, option));
22
22
  }
@@ -5,15 +5,26 @@ import { BuildSchemaPushStack, CheckSchemaPushStack, ErrorSchemaPushStack } from
5
5
  // ------------------------------------------------------------------
6
6
  // ItemsSized
7
7
  // ------------------------------------------------------------------
8
- function BuildItemsSized(stack, context, schema, value) {
8
+ function BuildItemsSizedStandard(stack, context, schema, value) {
9
9
  return E.ReduceAnd(schema.items.map((schema, index) => {
10
10
  const isLength = E.IsLessEqualThan(E.Member(value, 'length'), E.Constant(index));
11
11
  const isSchema = BuildSchemaPushStack(stack, context, schema, `${value}[${index}]`);
12
12
  const addIndex = context.AddIndex(E.Constant(index));
13
- const guarded = context.UseUnevaluated() ? E.And(isSchema, addIndex) : isSchema;
14
- return E.Or(isLength, guarded);
13
+ return E.Or(isLength, E.And(isSchema, addIndex));
15
14
  }));
16
15
  }
16
+ function BuildItemsSizedFast(stack, context, schema, value) {
17
+ return E.ReduceAnd(schema.items.map((schema, index) => {
18
+ const isLength = E.IsLessEqualThan(E.Member(value, 'length'), E.Constant(index));
19
+ const isSchema = BuildSchemaPushStack(stack, context, schema, `${value}[${index}]`);
20
+ return E.Or(isLength, isSchema);
21
+ }));
22
+ }
23
+ function BuildItemsSized(stack, context, schema, value) {
24
+ return context.UseUnevaluated()
25
+ ? BuildItemsSizedStandard(stack, context, schema, value)
26
+ : BuildItemsSizedFast(stack, context, schema, value);
27
+ }
17
28
  function CheckItemsSized(stack, context, schema, value) {
18
29
  return G.Every(schema.items, 0, (schema, index) => {
19
30
  return G.IsLessEqualThan(value.length, index)
@@ -31,12 +42,21 @@ function ErrorItemsSized(stack, context, schemaPath, instancePath, schema, value
31
42
  // ------------------------------------------------------------------
32
43
  // ItemsUnsized
33
44
  // ------------------------------------------------------------------
34
- function BuildItemsUnsized(stack, context, schema, value) {
45
+ function BuildItemsUnsizedStandard(stack, context, schema, value) {
35
46
  const offset = Schema.IsPrefixItems(schema) ? schema.prefixItems.length : 0;
36
47
  const isSchema = BuildSchemaPushStack(stack, context, schema.items, 'element');
37
48
  const addIndex = context.AddIndex('index');
38
- const guarded = context.UseUnevaluated() ? E.And(isSchema, addIndex) : isSchema;
39
- return E.Every(value, E.Constant(offset), ['element', 'index'], guarded);
49
+ return E.Every(value, E.Constant(offset), ['element', 'index'], E.And(isSchema, addIndex));
50
+ }
51
+ function BuildItemsUnsizedFast(stack, context, schema, value) {
52
+ const offset = Schema.IsPrefixItems(schema) ? schema.prefixItems.length : 0;
53
+ const isSchema = BuildSchemaPushStack(stack, context, schema.items, 'element');
54
+ return E.Every(value, E.Constant(offset), ['element', 'index'], isSchema);
55
+ }
56
+ function BuildItemsUnsized(stack, context, schema, value) {
57
+ return context.UseUnevaluated()
58
+ ? BuildItemsUnsizedStandard(stack, context, schema, value)
59
+ : BuildItemsUnsizedFast(stack, context, schema, value);
40
60
  }
41
61
  function CheckItemsUnsized(stack, context, schema, value) {
42
62
  const offset = Schema.IsPrefixItems(schema) ? schema.prefixItems.length : 0;
@@ -15,8 +15,8 @@ function IsValid(schema) {
15
15
  export function BuildMaxContains(stack, context, schema, value) {
16
16
  if (!IsValid(schema))
17
17
  return E.Constant(true);
18
- const [result, item] = [Unique(), Unique()];
19
- const count = E.Call(E.Member(value, 'reduce'), [E.ArrowFunction([result, item], E.Ternary(BuildSchema(stack, context, schema.contains, item), E.PrefixIncrement(result), result)), E.Constant(0)]);
18
+ const [item] = [Unique()];
19
+ const count = E.Counted(value, [item, '_'], BuildSchema(stack, context, schema.contains, item));
20
20
  return E.IsLessEqualThan(count, E.Constant(schema.maxContains));
21
21
  }
22
22
  // ------------------------------------------------------------------
@@ -25,7 +25,7 @@ export function BuildMaxContains(stack, context, schema, value) {
25
25
  export function CheckMaxContains(stack, context, schema, value) {
26
26
  if (!IsValid(schema))
27
27
  return true;
28
- const count = value.reduce((result, item) => CheckSchema(stack, context, schema.contains, item) ? ++result : result, 0);
28
+ const count = G.Counted(value, (item) => CheckSchema(stack, context, schema.contains, item));
29
29
  return G.IsLessEqualThan(count, schema.maxContains);
30
30
  }
31
31
  // ------------------------------------------------------------------
@@ -12,12 +12,22 @@ function IsValid(schema) {
12
12
  // ------------------------------------------------------------------
13
13
  // Build
14
14
  // ------------------------------------------------------------------
15
+ function BuildMinContainsStandard(stack, context, schema, value) {
16
+ const [item, index] = [Unique(), Unique()];
17
+ const count = E.Counted(value, [item, index], E.And(BuildSchema(stack, context, schema.contains, item), context.AddIndex(index)));
18
+ return E.IsGreaterEqualThan(count, E.Constant(schema.minContains));
19
+ }
20
+ function BuildMinContainsFast(stack, context, schema, value) {
21
+ const [item] = [Unique()];
22
+ const count = E.Counted(value, [item, '_'], BuildSchema(stack, context, schema.contains, item));
23
+ return E.IsGreaterEqualThan(count, E.Constant(schema.minContains));
24
+ }
15
25
  export function BuildMinContains(stack, context, schema, value) {
16
26
  if (!IsValid(schema))
17
27
  return E.Constant(true);
18
- const [result, item] = [Unique(), Unique()];
19
- const count = E.Call(E.Member(value, 'reduce'), [E.ArrowFunction([result, item], E.Ternary(BuildSchema(stack, context, schema.contains, item), E.PrefixIncrement(result), result)), E.Constant(0)]);
20
- return E.IsGreaterEqualThan(count, E.Constant(schema.minContains));
28
+ return context.UseUnevaluated()
29
+ ? BuildMinContainsStandard(stack, context, schema, value)
30
+ : BuildMinContainsFast(stack, context, schema, value);
21
31
  }
22
32
  // ------------------------------------------------------------------
23
33
  // Check
@@ -25,7 +35,7 @@ export function BuildMinContains(stack, context, schema, value) {
25
35
  export function CheckMinContains(stack, context, schema, value) {
26
36
  if (!IsValid(schema))
27
37
  return true;
28
- const count = value.reduce((result, item) => CheckSchema(stack, context, schema.contains, item) ? ++result : result, 0);
38
+ const count = G.Counted(value, (item, index) => CheckSchema(stack, context, schema.contains, item) && context.AddIndex(index));
29
39
  return G.IsGreaterEqualThan(count, schema.minContains);
30
40
  }
31
41
  // ------------------------------------------------------------------
@@ -6,14 +6,16 @@ import { BuildSchema, CheckSchema } from './schema.mjs';
6
6
  // ------------------------------------------------------------------
7
7
  // Build
8
8
  // ------------------------------------------------------------------
9
- function BuildNotUnevaluated(stack, context, schema, value) {
9
+ function BuildNotStandard(stack, context, schema, value) {
10
10
  return Reducer(stack, context, [schema.not], value, E.Not(E.IsEqual(E.Member('results', 'length'), E.Constant(1))));
11
11
  }
12
12
  function BuildNotFast(stack, context, schema, value) {
13
13
  return E.Not(BuildSchema(stack, context, schema.not, value));
14
14
  }
15
15
  export function BuildNot(stack, context, schema, value) {
16
- return context.UseUnevaluated() ? BuildNotUnevaluated(stack, context, schema, value) : BuildNotFast(stack, context, schema, value);
16
+ return context.UseUnevaluated()
17
+ ? BuildNotStandard(stack, context, schema, value)
18
+ : BuildNotFast(stack, context, schema, value);
17
19
  }
18
20
  // ------------------------------------------------------------------
19
21
  // Check
@@ -3,22 +3,23 @@ import { CheckContext, AccumulatedErrorContext } from './_context.mjs';
3
3
  import { Reducer } from './_reducer.mjs';
4
4
  import { EmitGuard as E, Guard as G } from '../../guard/index.mjs';
5
5
  import { BuildSchema, CheckSchema, ErrorSchema } from './schema.mjs';
6
+ import { Unique } from './_unique.mjs';
6
7
  // ------------------------------------------------------------------
7
8
  // Build
8
9
  // ------------------------------------------------------------------
9
- function BuildOneOfUnevaluated(stack, context, schema, value) {
10
+ function BuildOneOfStandard(stack, context, schema, value) {
10
11
  return Reducer(stack, context, schema.oneOf, value, E.IsEqual(E.Member('results', 'length'), E.Constant(1)));
11
12
  }
12
13
  function BuildOneOfFast(stack, context, schema, value) {
14
+ const [result] = [Unique()];
13
15
  const results = E.ArrayLiteral(schema.oneOf.map((schema) => BuildSchema(stack, context, schema, value)));
14
- const count = E.Call(E.Member(results, 'reduce'), [
15
- E.ArrowFunction(['count', 'result'], E.Ternary(E.IsEqual('result', E.Constant(true)), E.PrefixIncrement('count'), 'count')),
16
- E.Constant(0),
17
- ]);
16
+ const count = E.Counted(results, [result, '_'], E.IsEqual(result, E.Constant(true)));
18
17
  return E.IsEqual(count, E.Constant(1));
19
18
  }
20
19
  export function BuildOneOf(stack, context, schema, value) {
21
- return context.UseUnevaluated() ? BuildOneOfUnevaluated(stack, context, schema, value) : BuildOneOfFast(stack, context, schema, value);
20
+ return context.UseUnevaluated()
21
+ ? BuildOneOfStandard(stack, context, schema, value)
22
+ : BuildOneOfFast(stack, context, schema, value);
22
23
  }
23
24
  // ------------------------------------------------------------------
24
25
  // Check
@@ -5,7 +5,7 @@ import { CheckContext, AccumulatedErrorContext } from './_context.mjs';
5
5
  import { EmitGuard as E } from '../../guard/index.mjs';
6
6
  import { CheckSchema, ErrorSchema } from './schema.mjs';
7
7
  // ------------------------------------------------------------------
8
- // BuildRefStandard
8
+ // BuildRef
9
9
  // ------------------------------------------------------------------
10
10
  function BuildRefStandard(stack, context, target, value) {
11
11
  const interior = E.ArrowFunction(['context', 'value'], Functions.CreateFunction(stack, context, target, 'value'));
@@ -17,15 +17,9 @@ function BuildRefStandard(stack, context, target, value) {
17
17
  ]));
18
18
  return E.Call(exterior, ['context', value]);
19
19
  }
20
- // ------------------------------------------------------------------
21
- // BuildRefStandard
22
- // ------------------------------------------------------------------
23
20
  function BuildRefFast(stack, context, target, value) {
24
21
  return Functions.CreateFunction(stack, context, target, value);
25
22
  }
26
- // ------------------------------------------------------------------
27
- // BuildRef
28
- // ------------------------------------------------------------------
29
23
  export function BuildRef(stack, context, schema, value) {
30
24
  const target = stack.Ref(schema) ?? false;
31
25
  return context.UseUnevaluated()
@@ -48,7 +48,7 @@ function BuildTypeNames(stack, context, typenames, value) {
48
48
  return E.ReduceOr(typenames.map(type => BuildTypeName(stack, context, type, value)));
49
49
  }
50
50
  function CheckTypeNames(stack, context, types, schema, value) {
51
- return types.some(type => CheckTypeName(stack, context, type, schema, value));
51
+ return G.Some(types, type => CheckTypeName(stack, context, type, schema, value));
52
52
  }
53
53
  // ------------------------------------------------------------------
54
54
  // Type
@@ -65,8 +65,8 @@ type TIntrinsicOrCall<Target extends string, Parameters extends T.TSchema[]> = (
65
65
  Target,
66
66
  Parameters
67
67
  ] extends ['Uppercase', [infer Type extends T.TSchema]] ? S.TUppercaseDeferred<Type> : T.TCallConstruct<T.TRef<Target>, Parameters>);
68
- type TDelimitedDecode<Input extends ([unknown, unknown] | unknown)[], Result extends unknown[] = []> = (Input extends [infer Left, ...infer Right] ? Left extends [infer Item, infer _] ? TDelimitedDecode<Right, [...Result, Item]> : TDelimitedDecode<Right, [...Result, Left]> : Result);
69
- type TDelimited<Input extends [unknown, unknown]> = Input extends [infer Left extends unknown[], infer Right extends unknown[]] ? TDelimitedDecode<[...Left, ...Right]> : [];
68
+ type TDelimitedDecode<Input extends [unknown, unknown][], Result extends unknown[] = []> = (Input extends [infer Left extends [unknown, unknown], ...infer Right extends [unknown, unknown][]] ? TDelimitedDecode<Right, [...Result, Left[1]]> : Result);
69
+ type TDelimited<Input extends [unknown, unknown, unknown] | []> = (Input extends [infer Left extends unknown, infer Right extends [unknown, unknown][], infer _ extends unknown[]] ? [Left, ...TDelimitedDecode<Right>] : []);
70
70
  export type TGenericParameterExtendsEqualsMapping<Input extends [unknown, unknown, unknown, unknown, unknown]> = (Input extends [infer Name extends string, 'extends', infer Extends extends T.TSchema, '=', infer Equals extends T.TSchema] ? T.TParameter<Name, Extends, Equals> : never);
71
71
  export declare function GenericParameterExtendsEqualsMapping(input: [unknown, unknown, unknown, unknown, unknown]): unknown;
72
72
  export type TGenericParameterExtendsMapping<Input extends [unknown, unknown, unknown]> = (Input extends [infer Name extends string, 'extends', infer Extends extends T.TSchema] ? T.TParameter<Name, Extends, Extends> : never);
@@ -77,12 +77,12 @@ export type TGenericParameterIdentifierMapping<Input extends string, Result exte
77
77
  export declare function GenericParameterIdentifierMapping(input: string): unknown;
78
78
  export type TGenericParameterMapping<Input extends unknown> = (Input);
79
79
  export declare function GenericParameterMapping(input: unknown): unknown;
80
- export type TGenericParameterListMapping<Input extends [unknown, unknown]> = (TDelimited<Input>);
81
- export declare function GenericParameterListMapping(input: [unknown, unknown]): unknown;
80
+ export type TGenericParameterListMapping<Input extends [unknown, unknown, unknown] | []> = (TDelimited<Input>);
81
+ export declare function GenericParameterListMapping(input: [unknown, unknown, unknown] | []): unknown;
82
82
  export type TGenericParametersMapping<Input extends [unknown, unknown, unknown]> = (Input extends ['<', infer Parameters extends T.TParameter[], '>'] ? Parameters : never);
83
83
  export declare function GenericParametersMapping(input: [unknown, unknown, unknown]): unknown;
84
- export type TGenericCallArgumentListMapping<Input extends [unknown, unknown]> = (TDelimited<Input>);
85
- export declare function GenericCallArgumentListMapping(input: [unknown, unknown]): unknown;
84
+ export type TGenericCallArgumentListMapping<Input extends [unknown, unknown, unknown] | []> = (TDelimited<Input>);
85
+ export declare function GenericCallArgumentListMapping(input: [unknown, unknown, unknown] | []): unknown;
86
86
  export type TGenericCallArgumentsMapping<Input extends [unknown, unknown, unknown]> = (Input extends ['<', infer Arguments extends T.TSchema[], '>'] ? Arguments : never);
87
87
  export declare function GenericCallArgumentsMapping(input: [unknown, unknown, unknown]): unknown;
88
88
  export type TGenericCallMapping<Input extends [unknown, unknown], Result = Input extends [infer Ref extends string, infer Arguments extends T.TSchema[]] ? TIntrinsicOrCall<Ref, Arguments> : never> = Result;
@@ -202,8 +202,8 @@ export type TPropertyMapping<Input extends [unknown, unknown, unknown, unknown,
202
202
  export declare function PropertyMapping(input: [unknown, unknown, unknown, unknown, unknown]): unknown;
203
203
  export type TPropertyDelimiterMapping<Input extends [unknown, unknown] | [unknown]> = (Input);
204
204
  export declare function PropertyDelimiterMapping(input: [unknown, unknown] | [unknown]): unknown;
205
- export type TPropertyListMapping<Input extends [unknown, unknown]> = (TDelimited<Input>);
206
- export declare function PropertyListMapping(input: [unknown, unknown]): unknown;
205
+ export type TPropertyListMapping<Input extends [unknown, unknown, unknown] | []> = (TDelimited<Input>);
206
+ export declare function PropertyListMapping(input: [unknown, unknown, unknown] | []): unknown;
207
207
  type TPropertiesReduce<PropertiesList extends T.TProperties[], Result extends [properties: T.TProperties, patternProperties: T.TProperties] = [{}, {}]> = (PropertiesList extends [infer Left extends T.TProperties, ...infer Right extends T.TProperties[]] ? (Left extends {
208
208
  [_ in T.TIntegerKey]: T.TSchema;
209
209
  } ? TPropertiesReduce<Right, [Result[0], Memory.TAssign<Result[1], Left>]> : Left extends {
@@ -219,18 +219,21 @@ export type T_Object_Mapping<Input extends unknown> = (Input extends [infer Prop
219
219
  export declare function _Object_Mapping(input: unknown): unknown;
220
220
  export type TElementNamedMapping<Input extends [unknown, unknown, unknown, unknown, unknown] | [unknown, unknown, unknown, unknown] | [unknown, unknown, unknown]> = (Input extends [string, '?', ':', 'readonly', infer Type extends T.TSchema] ? S.TAddReadonlyDeferred<S.TAddOptionalDeferred<Type>> : Input extends [string, /**/ ':', 'readonly', infer Type extends T.TSchema] ? S.TAddReadonlyDeferred<Type> : Input extends [string, '?', ':', /* */ infer Type extends T.TSchema] ? S.TAddOptionalDeferred<Type> : Input extends [string, /**/ ':', /* */ infer Type extends T.TSchema] ? Type : never);
221
221
  export declare function ElementNamedMapping(input: [unknown, unknown, unknown, unknown, unknown] | [unknown, unknown, unknown, unknown] | [unknown, unknown, unknown]): unknown;
222
- export type TElementReadonlyOptionalMapping<Input extends [unknown, unknown, unknown]> = (Input extends ['readonly', infer Type extends T.TSchema, '?'] ? S.TAddReadonlyDeferred<S.TAddOptionalDeferred<Type>> : never);
223
- export declare function ElementReadonlyOptionalMapping(input: [unknown, unknown, unknown]): unknown;
224
- export type TElementReadonlyMapping<Input extends [unknown, unknown]> = (Input extends ['readonly', infer Type extends T.TSchema] ? S.TAddReadonlyDeferred<Type> : never);
225
- export declare function ElementReadonlyMapping(input: [unknown, unknown]): unknown;
226
- export type TElementOptionalMapping<Input extends [unknown, unknown]> = (Input extends [infer Type extends T.TSchema, '?'] ? S.TAddOptionalDeferred<Type> : never);
227
- export declare function ElementOptionalMapping(input: [unknown, unknown]): unknown;
228
- export type TElementBaseMapping<Input extends unknown> = (Input);
229
- export declare function ElementBaseMapping(input: unknown): unknown;
222
+ export type TElementBaseMapping<Input extends unknown | [unknown, unknown, unknown]> = (Input extends [infer IsReadonly extends boolean, infer Type extends T.TSchema, infer IsOptional extends boolean] ? ([
223
+ IsReadonly,
224
+ IsOptional
225
+ ] extends [true, true] ? S.TAddReadonlyDeferred<S.TAddOptionalDeferred<Type>> : [
226
+ IsReadonly,
227
+ IsOptional
228
+ ] extends [true, false] ? S.TAddReadonlyDeferred<Type> : [
229
+ IsReadonly,
230
+ IsOptional
231
+ ] extends [false, true] ? S.TAddOptionalDeferred<Type> : Type) : Input);
232
+ export declare function ElementBaseMapping(input: unknown | [unknown, unknown, unknown]): unknown;
230
233
  export type TElementMapping<Input extends [unknown, unknown] | [unknown]> = (Input extends ['...', infer Type extends T.TSchema] ? T.TRest<Type> : Input extends [infer Type extends T.TSchema] ? Type : never);
231
234
  export declare function ElementMapping(input: [unknown, unknown] | [unknown]): unknown;
232
- export type TElementListMapping<Input extends [unknown, unknown]> = (TDelimited<Input>);
233
- export declare function ElementListMapping(input: [unknown, unknown]): unknown;
235
+ export type TElementListMapping<Input extends [unknown, unknown, unknown] | []> = (TDelimited<Input>);
236
+ export declare function ElementListMapping(input: [unknown, unknown, unknown] | []): unknown;
234
237
  export type T_Tuple_Mapping<Input extends [unknown, unknown, unknown]> = (Input extends ['[', infer Types extends T.TSchema[], ']'] ? T.TTuple<Types> : never);
235
238
  export declare function _Tuple_Mapping(input: [unknown, unknown, unknown]): unknown;
236
239
  export type TParameterReadonlyOptionalMapping<Input extends [unknown, unknown, unknown, unknown, unknown]> = (Input extends [string, '?', ':', 'readonly', infer Type extends T.TSchema] ? S.TAddReadonlyDeferred<S.TAddOptionalDeferred<Type>> : never);
@@ -245,8 +248,8 @@ export type TParameterBaseMapping<Input extends unknown> = (Input);
245
248
  export declare function ParameterBaseMapping(input: unknown): unknown;
246
249
  export type TParameterMapping<Input extends [unknown, unknown] | [unknown]> = (Input extends ['...', infer Type extends T.TSchema] ? T.TRest<Type> : Input extends [infer Type extends T.TSchema] ? Type : never);
247
250
  export declare function ParameterMapping(input: [unknown, unknown] | [unknown]): unknown;
248
- export type TParameterListMapping<Input extends [unknown, unknown]> = (TDelimited<Input>);
249
- export declare function ParameterListMapping(input: [unknown, unknown]): unknown;
251
+ export type TParameterListMapping<Input extends [unknown, unknown, unknown] | []> = (TDelimited<Input>);
252
+ export declare function ParameterListMapping(input: [unknown, unknown, unknown] | []): unknown;
250
253
  export type T_Function_Mapping<Input extends [unknown, unknown, unknown, unknown, unknown]> = (Input extends ['(', infer ParameterList extends T.TSchema[], ')', '=>', infer ReturnType extends T.TSchema] ? T.TFunction<ParameterList, ReturnType> : never);
251
254
  export declare function _Function_Mapping(input: [unknown, unknown, unknown, unknown, unknown]): unknown;
252
255
  export type T_Constructor_Mapping<Input extends [unknown, unknown, unknown, unknown, unknown, unknown]> = (Input extends ['new', '(', infer ParameterList extends T.TSchema[], ')', '=>', infer InstanceType extends T.TSchema] ? T.TConstructor<ParameterList, InstanceType> : never);
@@ -280,15 +283,15 @@ export type TWithPropertyMapping<Input extends [unknown, unknown, unknown]> = (I
280
283
  [_ in Key]: Value;
281
284
  } : never);
282
285
  export declare function WithPropertyMapping(input: [unknown, unknown, unknown]): unknown;
283
- export type TWithPropertyListMapping<Input extends [unknown, unknown]> = (TDelimited<Input>);
284
- export declare function WithPropertyListMapping(input: [unknown, unknown]): unknown;
286
+ export type TWithPropertyListMapping<Input extends [unknown, unknown, unknown] | []> = (TDelimited<Input>);
287
+ export declare function WithPropertyListMapping(input: [unknown, unknown, unknown] | []): unknown;
285
288
  type TWithObjectMappingReduce<PropertyList extends Record<PropertyKey, unknown>[], Result extends Record<PropertyKey, unknown> = {}> = (PropertyList extends [infer Left extends Record<PropertyKey, unknown>, ...infer Right extends Record<PropertyKey, unknown>[]] ? TWithObjectMappingReduce<Right, Memory.TAssign<Result, Left>> : {
286
289
  [Key in keyof Result]: Result[Key];
287
290
  });
288
291
  export type TWithObjectMapping<Input extends [unknown, unknown, unknown]> = (Input extends ['{', infer PropertyList extends Record<PropertyKey, unknown>[], '}'] ? TWithObjectMappingReduce<PropertyList> : {});
289
292
  export declare function WithObjectMapping(input: [unknown, unknown, unknown]): unknown;
290
- export type TWithElementListMapping<Input extends [unknown, unknown]> = (TDelimited<Input>);
291
- export declare function WithElementListMapping(input: [unknown, unknown]): unknown;
293
+ export type TWithElementListMapping<Input extends [unknown, unknown, unknown] | []> = (TDelimited<Input>);
294
+ export declare function WithElementListMapping(input: [unknown, unknown, unknown] | []): unknown;
292
295
  export type TWithArrayMapping<Input extends [unknown, unknown, unknown]> = (Input extends ['[', infer Elements extends unknown[], ']'] ? Elements : never);
293
296
  export declare function WithArrayMapping(input: [unknown, unknown, unknown]): unknown;
294
297
  export type TWithValueMapping<Input extends unknown> = (Input);
@@ -318,8 +321,8 @@ export type TPatternBodyMapping<Input extends unknown> = (Input);
318
321
  export declare function PatternBodyMapping(input: unknown): unknown;
319
322
  export type TPatternMapping<Input extends [unknown, unknown, unknown]> = (Input extends ['^', infer Body extends T.TSchema[], '$'] ? Body : never);
320
323
  export declare function PatternMapping(input: [unknown, unknown, unknown]): unknown;
321
- export type TInterfaceDeclarationHeritageListMapping<Input extends [unknown, unknown]> = (TDelimited<Input>);
322
- export declare function InterfaceDeclarationHeritageListMapping(input: [unknown, unknown]): unknown;
324
+ export type TInterfaceDeclarationHeritageListMapping<Input extends [unknown, unknown, unknown] | []> = (TDelimited<Input>);
325
+ export declare function InterfaceDeclarationHeritageListMapping(input: [unknown, unknown, unknown] | []): unknown;
323
326
  export type TInterfaceDeclarationHeritageMapping<Input extends [unknown, unknown] | []> = (Input extends ['extends', infer Heritage extends T.TSchema[]] ? Heritage : []);
324
327
  export declare function InterfaceDeclarationHeritageMapping(input: [unknown, unknown] | []): unknown;
325
328
  export type TInterfaceDeclarationGenericMapping<Input extends [unknown, unknown, unknown, unknown, unknown]> = (Input extends ['interface', infer Name extends string, infer Parameters extends T.TParameter[], infer Heritage extends T.TSchema[], infer Properties extends [T.TProperties, T.TProperties]] ? {
@@ -342,11 +345,11 @@ export type TExportKeywordMapping<Input extends [unknown] | []> = (null);
342
345
  export declare function ExportKeywordMapping(input: [unknown] | []): unknown;
343
346
  export type TModuleDeclarationDelimiterMapping<Input extends [unknown, unknown] | [unknown]> = (Input);
344
347
  export declare function ModuleDeclarationDelimiterMapping(input: [unknown, unknown] | [unknown]): unknown;
345
- export type TModuleDeclarationListMapping<Input extends [unknown, unknown]> = (TPropertiesReduce<TDelimited<Input>>);
346
- export declare function ModuleDeclarationListMapping(input: [unknown, unknown]): unknown;
348
+ export type TModuleDeclarationListMapping<Input extends [unknown, unknown, unknown] | []> = (TDelimited<Input>);
349
+ export declare function ModuleDeclarationListMapping(input: [unknown, unknown, unknown] | []): unknown;
347
350
  export type TModuleDeclarationMapping<Input extends [unknown, unknown, unknown]> = (Input extends [null, infer ModuleDeclaration extends T.TProperties, null] ? ModuleDeclaration : never);
348
351
  export declare function ModuleDeclarationMapping(input: [unknown, unknown, unknown]): unknown;
349
- export type TModuleMapping<Input extends [unknown, unknown]> = (Input extends [infer ModuleDeclaration extends T.TProperties, infer ModuleDeclarationList extends [T.TProperties, T.TProperties]] ? S.TModuleDeferred<Memory.TAssign<ModuleDeclaration, ModuleDeclarationList[0]>> : never);
352
+ export type TModuleMapping<Input extends [unknown, unknown]> = (Input extends [infer ModuleDeclaration extends T.TProperties, infer ModuleDeclarationList extends T.TProperties[]] ? S.TModuleDeferred<Memory.TAssign<ModuleDeclaration, TPropertiesReduce<ModuleDeclarationList>[0]>> : never);
350
353
  export declare function ModuleMapping(input: [unknown, unknown]): unknown;
351
354
  export type TScriptMapping<Input extends unknown> = (Input);
352
355
  export declare function ScriptMapping(input: unknown): unknown;