-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
e1501e5
commit a725846
Showing
10 changed files
with
183 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* @file Type Tests - At | ||
* @module tutils/types/tests/unit-d/At | ||
*/ | ||
|
||
import type Author from '#fixtures/author.interface' | ||
import type TestSubject from '../at' | ||
import type EmptyArray from '../empty-array' | ||
import type EmptyString from '../empty-string' | ||
|
||
describe('unit-d:types/At', () => { | ||
type F = undefined | ||
|
||
it('should equal F if K is out of range', () => { | ||
// Arrange | ||
type T1 = ['a', 'b', 'c'] | ||
type T2 = 'abc' | ||
|
||
// Expect | ||
expectTypeOf<TestSubject<T1, '-4'>>().toEqualTypeOf<F>() | ||
expectTypeOf<TestSubject<T1, '3'>>().toEqualTypeOf<F>() | ||
expectTypeOf<TestSubject<T1, -4>>().toEqualTypeOf<F>() | ||
expectTypeOf<TestSubject<T1, 3>>().toEqualTypeOf<F>() | ||
expectTypeOf<TestSubject<T2, '-4'>>().toEqualTypeOf<F>() | ||
expectTypeOf<TestSubject<T2, '3'>>().toEqualTypeOf<F>() | ||
expectTypeOf<TestSubject<T2, -4>>().toEqualTypeOf<F>() | ||
expectTypeOf<TestSubject<T2, 3>>().toEqualTypeOf<F>() | ||
}) | ||
|
||
it('should equal F if T extends EmptyString', () => { | ||
expectTypeOf<TestSubject<EmptyString, 0>>().toEqualTypeOf<F>() | ||
expectTypeOf<TestSubject<Readonly<EmptyString>, 0>>().toEqualTypeOf<F>() | ||
}) | ||
|
||
it('should equal F if T extends EmptyArray', () => { | ||
expectTypeOf<TestSubject<EmptyArray, 0>>().toEqualTypeOf<F>() | ||
expectTypeOf<TestSubject<EmptyArray, 0>>().toEqualTypeOf<F>() | ||
}) | ||
|
||
it('should equal F | T[number] if T is not string literal or tuple', () => { | ||
// Arrange | ||
type T1 = Author[] | ||
type T2 = string | ||
|
||
// Expect | ||
expectTypeOf<TestSubject<T1, '0'>>().toEqualTypeOf<F | T1[number]>() | ||
expectTypeOf<TestSubject<T1, 0>>().toEqualTypeOf<F | T1[number]>() | ||
expectTypeOf<TestSubject<T2, '1'>>().toEqualTypeOf<F | T2[number]>() | ||
expectTypeOf<TestSubject<T2, 1>>().toEqualTypeOf<F | T2[number]>() | ||
}) | ||
|
||
it('should equal T.at(K)! if T is string literal or tuple', () => { | ||
// Arrange | ||
type T1 = [Author, Author] | ||
type T2 = 'def' | ||
|
||
// Expect | ||
expectTypeOf<TestSubject<T1, '1'>>().toEqualTypeOf<Author>() | ||
expectTypeOf<TestSubject<T1, '-1'>>().toEqualTypeOf<Author>() | ||
expectTypeOf<TestSubject<T1, 1>>().toEqualTypeOf<Author>() | ||
expectTypeOf<TestSubject<T1, -1>>().toEqualTypeOf<Author>() | ||
expectTypeOf<TestSubject<T2, '1'>>().toEqualTypeOf<'e'>() | ||
expectTypeOf<TestSubject<T2, '-1'>>().toEqualTypeOf<'d' | 'e' | 'f'>() | ||
expectTypeOf<TestSubject<T2, 1>>().toEqualTypeOf<'e'>() | ||
expectTypeOf<TestSubject<T2, -1>>().toEqualTypeOf<'d' | 'e' | 'f'>() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* @file Type Definitions - At | ||
* @module tutils/types/At | ||
*/ | ||
|
||
import type EmptyArray from './empty-array' | ||
import type EmptyString from './empty-string' | ||
import type IfTuple from './if-tuple' | ||
import type NumberString from './number-string' | ||
import type Numeric from './numeric' | ||
import type Split from './split' | ||
import type TupleLength from './tuple-length' | ||
|
||
/** | ||
* Constructs a union of numbers in the range `[0,Max)`. | ||
* | ||
* @internal | ||
* | ||
* @template Max - Upper bound of range (exclusive) | ||
* @template Acc - Accumulator | ||
*/ | ||
type Enumerate< | ||
Max extends number, | ||
Acc extends readonly number[] = EmptyArray | ||
> = Acc['length'] extends Max | ||
? Acc[number] | ||
: Enumerate<Max, [...Acc, Acc['length']]> | ||
|
||
/** | ||
* Constructs a union of numerics in the range `[-1 * Max, Max)`. | ||
* | ||
* @internal | ||
* | ||
* @template Max - Upper bound of range (exclusive) | ||
*/ | ||
type Range<Max extends number> = | ||
| Exclude<`-${Exclude<Enumerate<Max>, Enumerate<0>>}`, `-${0}`> | ||
| `-${Max}` | ||
| `${Exclude<Enumerate<Max>, Enumerate<0>>}` | ||
|
||
/** | ||
* Indexes `T` at `K`. | ||
* | ||
* Partially supports negative indices. | ||
* | ||
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/at | ||
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/at | ||
* | ||
* @template T - Type to evaluate | ||
* @template K - Index | ||
* @template F - Fallback value | ||
*/ | ||
type At< | ||
T extends string | readonly unknown[], | ||
K extends NumberString, | ||
F = undefined | ||
> = NonNullable<T> extends EmptyArray | EmptyString | ||
? F | ||
: K extends Numeric | number | ||
? NonNullable<T> extends string | ||
? [string] extends [NonNullable<T>] | ||
? F | T[number] | ||
: Split<NonNullable<T>, ''> extends infer B | ||
? `${K}` extends Range<TupleLength<B>> | ||
? B[K & keyof B] | ||
: F | ||
: F | ||
: IfTuple< | ||
NonNullable<T>, | ||
`${K}` extends Range<TupleLength<NonNullable<T>>> ? T[K & keyof T] : F, | ||
F | T[number] | ||
> | ||
: F | ||
|
||
export type { At as default } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters