Skip to content

Commit

Permalink
chore: remove some usage of branch selector
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Jan 12, 2025
1 parent 180fc1c commit c3fd98a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 75 deletions.
4 changes: 2 additions & 2 deletions apps/website/src/content/docs/api/type-branching.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ type R = IsNever<any, { $any: 'any', $else: 'else' }> // 'any' or `else`?
Since properties in object type are not ordered,
i.e. `{ $any: 'any', $else: 'else' }` is the same as `{ $else: 'else', $any: 'any' }`,
there needs to be a way to resolve the branch.
there needs to be a way to define and resolve the branch in a particular order.
Inside `$ResolveBranch` is the type used within `IsNever` to handle this:
`$ResolveBranch` is the type used within `IsNever` to handle this:
```ts
// only emphasize the relevant parts
Expand Down
46 changes: 18 additions & 28 deletions packages/type-plus/src/array/array_plus.index_at.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { IsNever } from '../never/is_never.js'
import type { IsNumber } from '../number/is_number.js'
import type { IsInteger } from '../numeric/is_integer.js'
import type { IsNegative } from '../numeric/is_negative.js'
import type { $Else, $SelectionBranch, $Then } from '../type_plus/branch/$selection.js'

/**
* 🦴 *utilities*
Expand All @@ -29,13 +28,10 @@ export type IndexAt<
Fail = never,
Upper = A['length'],
Lower = 0,
> = IsNever<A, $SelectionBranch> extends infer R
? R extends $Then
? Fail
: R extends $Else
? IndexAt._<A, N, Fail, Upper, Lower>
: never
: never
> = IsNever<A, {
$then: Fail,
$else: IndexAt._<A, N, Fail, Upper, Lower>,
}>

export namespace IndexAt {
export type _<A extends readonly unknown[], N extends number, Fail = never, Upper = A['length'], Lower = 0> = IsEqual<
Expand All @@ -45,31 +41,25 @@ export namespace IndexAt {
IsInteger<
N,
{
$then: IsNumber<A['length'], IsNumber.$Branch<{ exact: true }>> extends infer R
? // A: array
R extends $Then
? N
: // A: tuple
R extends $Else
? IsNegative<
N,
{
$then: GreaterThan<Abs<N>, A['length']> extends true ? Lower : Subtract<A['length'], Abs<N>>
$else: GreaterThan<A['length'], N> extends true ? N : Upper
}
>
: never
: never
$then: IsNumber<A['length'], { exact: true,
$then: N,
$else:IsNegative<
N,
{
$then: GreaterThan<Abs<N>, A['length']> extends true ? Lower : Subtract<A['length'], Abs<N>>
$else: GreaterThan<A['length'], N> extends true ? N : Upper
}
>
}>
// N: number or float
$else: IsAny<
N,
{
$then: number
$else: IsNumber<N, IsNumber.$Branch<{ exact: true }>> extends infer R
? R extends $Then
? N
: never
: never
$else: IsNumber<N, { exact: true,
$then: N,
$else: never
}>
}
>
}
Expand Down
22 changes: 9 additions & 13 deletions packages/type-plus/src/array/last.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { IsNever } from '../never/is_never.js'
import type { $Never } from '../never/never.js'
import type { $Else, $SelectionBranch, $Then } from '../type_plus/branch/$selection.js'

/**
* 🦴 *utilities*
Expand All @@ -23,18 +22,15 @@ import type { $Else, $SelectionBranch, $Then } from '../type_plus/branch/$select
*/
export type Last<T extends readonly unknown[], Options extends Last.Options = Last.DefaultOptions> = IsNever<
T,
$SelectionBranch
> extends infer R
? R extends $Then
? Options['$never']
: R extends $Else
? T['length'] extends 0
? Options['caseEmptyTuple']
: T extends readonly [...unknown[], infer R]
? R
: T[0]
: never
: never
{
$then: Options['$never'],
$else: T['length'] extends 0
? Options['caseEmptyTuple']
: T extends readonly [...unknown[], infer R]
? R
: T[0]
}
>

export namespace Last {
export interface Options extends $Never.$Options {
Expand Down
44 changes: 17 additions & 27 deletions packages/type-plus/src/numeric/is_integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,25 @@ export type IsInteger<T, $O extends IsInteger.$Options = {}> = IsNumber<
T,
{
distributive: $O['distributive']
$then: $Then
$else: $Else
}
> extends infer R
? R extends $Then
? number extends T
? $ResolveBranch<number, $O, [$Then]> | $ResolveBranch<T, $O, [$Else]>
: T extends number & infer U
? `${T}` extends `${number}.${number}`
? $ResolveBranch<T, $O, [$Else]>
: [T, U] extends [U, T]
? $ResolveBranch<T, $O, [$Then]>
: $ResolveBranch<number, $O, [$Then]> | $ResolveBranch<T, $O, [$Else]>
: never
: R extends $Else
? IsBigint<
T,
{
distributive: $O['distributive']
$then: $Then
$else: $Else
}
> extends infer R
? R extends $Then
$then: number extends T
? $ResolveBranch<number, $O, [$Then]> | $ResolveBranch<T, $O, [$Else]>
: T extends number & infer U
? `${T}` extends `${number}.${number}`
? $ResolveBranch<T, $O, [$Else]>
: [T, U] extends [U, T]
? $ResolveBranch<T, $O, [$Then]>
: $ResolveBranch<T, $O, [$Else]>
: never
: $ResolveBranch<number, $O, [$Then]> | $ResolveBranch<T, $O, [$Else]>
: never
: never
$else: IsBigint<
T,
{
distributive: $O['distributive']
$then: $ResolveBranch<T, $O, [$Then]>
$else: $ResolveBranch<T, $O, [$Else]>
}
>
}
>
export namespace IsInteger {
export type $Options = $Equality.$Options
export type $Branch<$O extends $Options = {}> = $Equality.$Branch<$O>
Expand Down
9 changes: 4 additions & 5 deletions packages/type-plus/src/numeric/is_not_negative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ export type IsNotNegative<T, $O extends IsNotNegative.$Options = {}> = IsBigint<
> extends infer R
? R extends $Then
? IsNotNegative._Negative<T, bigint, $O>
: IsNumber<Exclude<T, bigint>, { distributive: $O['distributive']; $then: $Then; $else: $Else }> extends infer R
? R extends $Then
? IsNotNegative._Negative<T, number, $O>
: $ResolveBranch<Exclude<T, number | bigint>, $O, [$Then]>
: never
: IsNumber<Exclude<T, bigint>, { distributive: $O['distributive'];
$then: IsNotNegative._Negative<T, number, $O>
$else: $ResolveBranch<Exclude<T, number | bigint>, $O, [$Then]>
}>
: never

export namespace IsNotNegative {
Expand Down

0 comments on commit c3fd98a

Please sign in to comment.