-
-
Notifications
You must be signed in to change notification settings - Fork 655
Add IndexOf, LastIndexOf, IndicesOf, CountOf types
#1155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
8d5ce76
6c8dc3e
ad88f55
41ae7fb
4f5d1a4
c8411c7
1ae3797
7f2440f
44ea002
8e83a64
c5d85bd
1f93d14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import type {UnknownArray} from './unknown-array.d.ts'; | ||
|
|
||
| export type ArrayReverse<Array_ extends UnknownArray> = | ||
| Array_ extends [...infer Head, infer Tail] | ||
| ? [Tail, ...ArrayReverse<Head>] | ||
| : []; | ||
benzaria marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import type {UnknownArray} from './unknown-array.d.ts'; | ||
| import type {AllIndexOf} from './index-of.d.ts'; | ||
|
|
||
| export type CountOf<Array_ extends UnknownArray, Item, FromIndex extends number = 0> = | ||
| AllIndexOf<Array_, Item, FromIndex> extends infer Indexs extends number[] | ||
| ? Indexs['length'] | ||
| : 0; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import type {SplitArrayByIndex} from './array-splice.d.ts'; | ||
| import type {SubtractPositives} from './subtract.d.ts'; | ||
| import type {ArrayReverse} from './array-reverse.d.ts'; | ||
| import type {UnknownArray} from './unknown-array.d.ts'; | ||
| import type {SumPositives} from './sum.d.ts'; | ||
| import type {IsEqual} from './is-equal.d.ts'; | ||
|
|
||
| /** | ||
| Simpler version of Sum<T, 1>, without the extra logic. | ||
| */ | ||
| export type Increment<T extends number> = SumPositives<T, 1>; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you exporting it? It's not used elsewhere.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohh i forgot about it i was using it in a arithmetic types I'm working on to Improve |
||
|
|
||
| type _IndexOf< | ||
| Array_ extends UnknownArray, Item, | ||
| FromIndex extends number = 0, | ||
| Index extends number = 0, | ||
| > = ( | ||
| Array_ extends readonly [infer Head, ...infer Tail] | ||
| ? IsEqual<Head, Item> extends true | ||
| ? SumPositives<Index, FromIndex> | ||
| : _IndexOf<Tail, Item, FromIndex, Increment<Index>> | ||
| : -1 // Same as `indexOf` | ||
| ); | ||
|
|
||
| type _AllIndexOf< | ||
| Array_ extends UnknownArray, Item, | ||
| FromIndex extends number = 0, | ||
| Indexs extends number[] = [], | ||
| > = ( | ||
| IndexOf<Array_, Item, FromIndex> extends infer Index extends number | ||
| ? Index extends -1 | ||
| ? Indexs | ||
| : _AllIndexOf<Array_, Item, Increment<Index>, [...Indexs, Index]> | ||
| : never | ||
| ); | ||
|
|
||
| // TODO: Add `ToIndex` parameter | ||
| export type IndexOf< | ||
| Array_ extends UnknownArray, Item, | ||
| FromIndex extends number = 0, | ||
| > = _IndexOf<SplitArrayByIndex<Array_, FromIndex>[1], Item, FromIndex>; | ||
| // Return's never If `FromIndex > ArrayLength` | ||
|
|
||
| // TODO: Add `ToIndex` parameter | ||
| export type AllIndexOf< | ||
| Array_ extends UnknownArray, Item, | ||
| FromIndex extends number = 0, | ||
| > = _AllIndexOf<Array_, Item, FromIndex>; | ||
|
|
||
| // TODO: Add `ToIndex` parameter | ||
| export type LastIndexOf< | ||
| Array_ extends UnknownArray, Item, | ||
| FromIndex extends number = 0, | ||
| > = ( | ||
| IndexOf<ArrayReverse<Array_>, Item, FromIndex> extends infer Index extends number | ||
| ? Index extends -1 | ||
| ? -1 | ||
| : SubtractPositives<Array_['length'], Increment<Index>> | ||
| : never | ||
| ); | ||
Uh oh!
There was an error while loading. Please reload this page.