Skip to content

Commit

Permalink
+ $mol_type_int_range
Browse files Browse the repository at this point in the history
  • Loading branch information
jin committed Jan 28, 2024
1 parent 3162bc2 commit 4d71738
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 23 deletions.
42 changes: 28 additions & 14 deletions type/int/int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)' >
>

}
50 changes: 41 additions & 9 deletions type/int/int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<[
Expand All @@ -25,16 +25,16 @@ namespace $ {
Digits[ Extract< Letter, keyof Digits> ]
>

type Int = Up< any, any >
type Int = unknown[]
type Pair = [ Int, Int ]

type Zero = []
type One = [0]
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,
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -114,7 +114,7 @@ namespace $ {
Parse<`${Right}`>,
]>
>

type Pow<
Arg extends Pair,
Res extends Int = One,
Expand All @@ -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 > ]>

Expand All @@ -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 : []
}

0 comments on commit 4d71738

Please sign in to comment.