From 4d71738d570ace6b65eaa2da12baac634d7cc999 Mon Sep 17 00:00:00 2001 From: jin Date: Sun, 28 Jan 2024 21:53:01 +0300 Subject: [PATCH] + $mol_type_int_range --- type/int/int.test.ts | 42 ++++++++++++++++++++++++------------- type/int/int.ts | 50 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 69 insertions(+), 23 deletions(-) diff --git a/type/int/int.test.ts b/type/int/int.test.ts index f746bdb0c30..99625bea56b 100644 --- a/type/int/int.test.ts +++ b/type/int/int.test.ts @@ -3,56 +3,70 @@ namespace $ { // type wrong_ints = $mol_type_int_plus< 'b40', '60b' > type plus = $mol_type_assert< - $mol_type_int_plus< 40, '60' >, + $mol_type_int_plus< 40, 60 >, 100 > type minus = $mol_type_assert< - $mol_type_int_minus< 100, '60' >, + $mol_type_int_minus< 100, 60 >, 40 > type minus_same = $mol_type_assert< - $mol_type_int_minus< 100, '100' >, + $mol_type_int_minus< 100, 100 >, 0 > type minus_overflow = $mol_type_assert_never< - $mol_type_int_minus< 60, '100' > + $mol_type_int_minus< 60, 100 > > type mult = $mol_type_assert< - $mol_type_int_mult< 40, '40' >, + $mol_type_int_mult< 40, 40 >, 1600 > type pow = $mol_type_assert< - $mol_type_int_pow< 2, '13' >, + $mol_type_int_pow< 2, 13 >, 8192 > type asc = $mol_type_assert< - $mol_type_int_ordered< 40, '60' >, + $mol_type_int_ordered< 40, 60 >, unknown > type desc = $mol_type_assert_never< - $mol_type_int_ordered< 60, '40' > + $mol_type_int_ordered< 60, 40 > > + type range_right = $mol_type_assert< + $mol_type_int_range< 3, 6 >, + | 3 | 4 | 5 + > + + type range_wrong = $mol_type_assert_never< + $mol_type_int_range< 6, 3 > + > + type calc_priorities = $mol_type_assert< - $mol_type_int_calc< '5+4*3^2-1' >, - 40 + $mol_type_int_calc< '35..5+4*3^2-1' >, + | 35 | 36 | 37 | 38 | 39 > type calc_parentheses = $mol_type_assert< - $mol_type_int_calc< '2*(2+3)' >, - 10 + $mol_type_int_calc< '2*(3+4)' >, + 14 > type calc_spaces = $mol_type_assert< - $mol_type_int_calc< '7 + 5 - 2*2 + 1' >, - 9 + $mol_type_int_calc< '7 + 5 - 2*2 + 1 .. 11' >, + | 9 | 10 > + type range_merge = $mol_type_assert< + $mol_type_int_calc< '(2..4)+(2..4)' >, + $mol_type_int_calc< '(4..7)' > + > + } diff --git a/type/int/int.ts b/type/int/int.ts index 523a7354527..c141ca6495b 100644 --- a/type/int/int.ts +++ b/type/int/int.ts @@ -3,7 +3,7 @@ namespace $ { type Parse< Str extends string, Res extends Int = Zero, - > = Str extends `${ infer Letter }${ infer Tail }` + > = Str extends `${ infer Letter extends keyof Digits }${ infer Tail }` ? Parse< Tail, Plus<[ @@ -25,7 +25,7 @@ namespace $ { Digits[ Extract< Letter, keyof Digits> ] > - type Int = Up< any, any > + type Int = unknown[] type Pair = [ Int, Int ] type Zero = [] @@ -33,8 +33,8 @@ namespace $ { type Ten = Up<10> type Down< - Value extends Up< any, any >, - > = Extract< Value, any[] >["length"] + Value extends Int, + > = Value["length"] type Up< Value extends number, @@ -80,7 +80,7 @@ namespace $ { type Minus< Arg extends Pair > = Arg[0] extends [ ... Arg[1], ... infer Res ] - ? Res + ? Extract< Res, Int > : never /** Number literal which is multiply of two another */ @@ -114,7 +114,7 @@ namespace $ { Parse<`${Right}`>, ]> > - + type Pow< Arg extends Pair, Res extends Int = One, @@ -125,19 +125,50 @@ namespace $ { Mult<[ Res, Arg[0] ]> > + /** + * Range of number literals from Lo up to Hi + * **Slow on large ranges** + */ + export type $mol_type_int_range< + Lo extends Check< Lo >, + Hi extends Check< Hi > + > = Down< + Range<[ + Parse<`${Lo}`>, + Parse<`${Hi}`> + ]> + > + + type Range< + Args extends Pair + > = keyof Args[0] extends keyof Args[1] + ? Plus<[ + Args[0], + Parse< + Exclude< + keyof Minus<[ Args[1], Args[0] ]>, + symbol | number + > + > + ]> + : never + /** Unknown when number literals is ordered */ export type $mol_type_int_ordered< Left extends Check< Left >, Right extends Check< Right >, - > = $mol_type_int_minus< Right, Left > extends never - ? never - : unknown + > = keyof Parse<`${Left}`> extends keyof Parse<`${Right}`> + ? unknown + : never type Calc< Expr extends string > = Expr extends `${ infer Left }(${ infer Inner })${ infer Right }` ? Calc< `${ Left }${ Down< Calc< Inner > > }${ Right}` > + : Expr extends `${ infer Left }..${ infer Right }` + ? Range<[ Calc< Left >, Calc< Right > ]> + : Expr extends `${ infer Left }+${ infer Right }` ? Plus<[ Calc< Left >, Calc< Right > ]> @@ -161,4 +192,5 @@ namespace $ { /** Evaluates simple expression */ export type $mol_type_int_calc< Expr extends string > = Down< Calc< Expr > > + type X = [0,0,0,0] extends [ ... infer A extends [0,0][] ] ? A : [] }