typebox 1.0.74 → 1.0.75

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.
@@ -1,5 +1,4 @@
1
1
  import { type TSchema } from '../../types/schema.mjs';
2
- import { type TIntersect } from '../../types/intersect.mjs';
3
2
  import { type TUnion } from '../../types/union.mjs';
4
3
  import { type TObject } from '../../types/object.mjs';
5
4
  import { type TTuple } from '../../types/tuple.mjs';
@@ -7,18 +6,29 @@ import { type TComposite } from './composite.mjs';
7
6
  import { type TNarrow } from './narrow.mjs';
8
7
  import { type TEvaluateType } from './evaluate.mjs';
9
8
  import { type TEvaluateIntersect } from './evaluate.mjs';
10
- type TCanDistribute<Type extends TSchema> = Type extends TObject | TTuple ? true : false;
11
- type TDistributeNormalize<Type extends TSchema> = Type extends TIntersect<infer Types extends TSchema[]> ? TEvaluateIntersect<Types> : Type;
12
- type TDistributeOperation<Left extends TSchema, Right extends TSchema, NormalLeft extends TSchema = TDistributeNormalize<Left>, NormalRight extends TSchema = TDistributeNormalize<Right>, IsObjectLeft extends boolean = TCanDistribute<NormalLeft>, IsObjectRight extends boolean = TCanDistribute<NormalRight>, Result extends TSchema = ([
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;
20
+ 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
+ IsUnionOperand
22
+ ] extends [true] ? TEvaluateIntersect<[EvaluatedLeft, EvaluatedRight]> : [
13
23
  IsObjectLeft,
14
24
  IsObjectRight
15
- ] extends [true, true] ? TComposite<TEvaluateType<NormalLeft>, NormalRight> : [
25
+ ] extends [true, true] ? TComposite<EvaluatedLeft, EvaluatedRight> : [
16
26
  IsObjectLeft,
17
27
  IsObjectRight
18
- ] extends [true, false] ? TEvaluateType<NormalLeft> : [
28
+ ] extends [true, false] ? EvaluatedLeft : [
19
29
  IsObjectLeft,
20
30
  IsObjectRight
21
- ] extends [false, true] ? NormalRight : TNarrow<TEvaluateType<NormalLeft>, NormalRight>)> = Result;
31
+ ] extends [false, true] ? EvaluatedRight : TNarrow<EvaluatedLeft, EvaluatedRight>)> = Result;
22
32
  type TDistributeType<Type extends TSchema, Distribution extends TSchema[], Result extends TSchema[] = []> = (Distribution extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TDistributeType<Type, Right, [...Result, TDistributeOperation<Type, Left>]> : Result extends [] ? [Type] : Result);
23
33
  type TDistributeUnion<Types extends TSchema[], Distribution extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TDistributeUnion<Right, Distribution, [...Result, ...TDistribute<[Left], Distribution>]> : Result);
24
34
  export type TDistribute<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? Left extends TUnion<infer UnionTypes extends TSchema[]> ? TDistribute<Right, TDistributeUnion<UnionTypes, Result>> : TDistribute<Right, TDistributeType<Left, Result>> : Result);
@@ -2,7 +2,6 @@
2
2
  // deno-fmt-ignore-file
3
3
  import { Guard } from '../../../guard/index.mjs';
4
4
  import { IsSchema } from '../../types/schema.mjs';
5
- import { IsIntersect } from '../../types/intersect.mjs';
6
5
  import { IsUnion } from '../../types/union.mjs';
7
6
  import { IsObject } from '../../types/object.mjs';
8
7
  import { IsTuple } from '../../types/tuple.mjs';
@@ -10,23 +9,28 @@ import { Composite } from './composite.mjs';
10
9
  import { Narrow } from './narrow.mjs';
11
10
  import { EvaluateType } from './evaluate.mjs';
12
11
  import { EvaluateIntersect } from './evaluate.mjs';
13
- function CanDistribute(type) {
12
+ function IsObjectLike(type) {
14
13
  return IsObject(type) || IsTuple(type);
15
14
  }
16
- function DistributeNormalize(type) {
17
- return (IsIntersect(type)
18
- ? EvaluateIntersect(type.allOf)
19
- : type);
15
+ function IsUnionOperand(left, right) {
16
+ const isUnionLeft = IsUnion(left);
17
+ const isUnionRight = IsUnion(right);
18
+ return (isUnionLeft && isUnionRight ? true :
19
+ !isUnionLeft && isUnionRight ? true :
20
+ isUnionLeft && !isUnionRight ? true :
21
+ false);
20
22
  }
21
23
  function DistributeOperation(left, right) {
22
- const normalLeft = DistributeNormalize(left);
23
- const normalRight = DistributeNormalize(right);
24
- const isObjectLeft = CanDistribute(normalLeft);
25
- const IsObjectRight = CanDistribute(normalRight);
26
- const result = (isObjectLeft && IsObjectRight ? Composite(EvaluateType(normalLeft), normalRight) :
27
- isObjectLeft && !IsObjectRight ? EvaluateType(normalLeft) :
28
- !isObjectLeft && IsObjectRight ? normalRight :
29
- Narrow(EvaluateType(normalLeft), normalRight));
24
+ const evaluatedLeft = EvaluateType(left);
25
+ const evaluatedRight = EvaluateType(right);
26
+ const isUnionOperand = IsUnionOperand(evaluatedLeft, evaluatedRight);
27
+ const isObjectLeft = IsObjectLike(evaluatedLeft);
28
+ const IsObjectRight = IsObjectLike(evaluatedRight);
29
+ const result = (isUnionOperand ? EvaluateIntersect([evaluatedLeft, evaluatedRight]) :
30
+ isObjectLeft && IsObjectRight ? Composite(evaluatedLeft, evaluatedRight) :
31
+ isObjectLeft && !IsObjectRight ? evaluatedLeft :
32
+ !isObjectLeft && IsObjectRight ? evaluatedRight :
33
+ Narrow(evaluatedLeft, evaluatedRight));
30
34
  return result;
31
35
  }
32
36
  function DistributeType(type, types, result = []) {
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.74",
4
+ "version": "1.0.75",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"