-
-
Notifications
You must be signed in to change notification settings - Fork 616
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
Draft
benzaria
wants to merge
12
commits into
sindresorhus:main
Choose a base branch
from
benzaria:IndexOf-CountOf-ArrReverse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8d5ce76
Add `IndexOf`, `LastIndexOf`, `AllIndexOf`, `CountOf` and `ArrayRever…
benzaria 6c8dc3e
Merge branch 'sindresorhus:main' into IndexOf-CountOf-ArrReverse
benzaria ad88f55
Fix: Linting errors
benzaria 41ae7fb
Change `ArrayReverse` -> `Reverse`, `AllIndexOf` -> `IndicesOf` and U…
benzaria 4f5d1a4
Rename: array-reverse -> reverse, Add: Reverse test, IsLeadingSpread
benzaria c8411c7
Add: SplitOnSpread, Remove: IsLeadingSpread, Modify Reverse to use Sp…
benzaria 1ae3797
Fix: SplitOnSpread typo, Expose: ExtendsStrict
benzaria 7f2440f
Add: Split: `IndicesOf` into seperated file, Add: tests and docs
benzaria 44ea002
Public: Expose and Document
benzaria 8e83a64
Fix: minor error in tests for `Reverse`
benzaria c5d85bd
Merge branch 'main' into IndexOf-CountOf-ArrReverse
benzaria 1f93d14
Fix: conflict
benzaria File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
import type {UnknownArray} from './unknown-array.d.ts'; | ||
import type {IndicesOf} from './indices-of.d.ts'; | ||
|
||
/** | ||
Returns the count of occurrences of a value in an array, or `0` if it is not present. | ||
|
||
@example | ||
``` | ||
type T = CountOf<[1, 2, 1, 1], 1>; | ||
//=> 3 | ||
|
||
declare function getCount<const T extends unknown[], const I>(array: T, item: I): CountOf<T, I>; | ||
getCount(['a', 'b', 'a'], 'a'); | ||
//=> 2 instead of `number` | ||
``` | ||
|
||
@author benzaria | ||
@see Includes | ||
@category Array | ||
*/ | ||
export type CountOf<Array_ extends UnknownArray, Item, FromIndex extends number = 0> = | ||
IndicesOf<Array_, Item, FromIndex> extends infer Indices extends number[] | ||
? Indices['length'] | ||
: 0; |
This file contains hidden or 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,34 @@ | ||
import type {IsNever} from './is-never.d.ts'; | ||
|
||
/** | ||
A stricter version of `extends` that Checks Stricily if one type extends another without distribution. | ||
|
||
Note: this is not quite the same as `Left extends Right` because: | ||
|
||
1. Types are wrapped in a 1-tuple so that union types are not distributed - instead we consider `string | number` to _not_ extend `number`. If we used `Left extends Right` directly you would get `Extends<string | number, number>` => `false | true` => `boolean`. So it's return `true` if `[Left] extends [Right]`. | ||
|
||
2. Return's `true` if `Left` and `Right` are both `never`. | ||
|
||
@example | ||
``` | ||
type T = ExtendsStrict<number | string, string> | ||
//=> false | ||
|
||
type T = ExtendsStrict<string, number | string> | ||
//=> true | ||
|
||
type T = ExtendsStrict<string, string> | ||
//=> true | ||
|
||
type T = ExtendsStrict<never, never> | ||
//=> true | ||
``` | ||
|
||
@category Improved Built-in | ||
*/ | ||
export type ExtendsStrict<Left, Right> = | ||
IsNever<Left> extends true | ||
? IsNever<Right> | ||
: [Left] extends [Right] | ||
? true | ||
: false; |
This file contains hidden or 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 hidden or 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,79 @@ | ||
import type {SplitArrayByIndex} from './array-splice.d.ts'; | ||
import type {SubtractPositives} from './subtract.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'; | ||
import type {Reverse} from './reverse.d.ts'; | ||
|
||
/** | ||
Simpler version of Sum<T, 1>, without the extra logic. | ||
*/ | ||
export type Increment<T extends number> = SumPositives<T, 1>; | ||
|
||
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` | ||
); | ||
|
||
/** | ||
Returns the index of the first occurrence of a value in an array, or `-1` if it is not present. | ||
|
||
@example | ||
``` | ||
type T = IndexOf<readonly ['a', 'c', 'c'], 'c'>; | ||
//=> 1 | ||
|
||
type T = IndexOf<[1, 2, 3], 4>; | ||
//=> -1 | ||
|
||
type T = IndexOf<[{a: 1}, {a: 1}, {b: 1}], {a: 1}>; | ||
//=> 0 | ||
``` | ||
|
||
@author benzaria | ||
@see LastIndexOf, IndicesOf | ||
@category Array | ||
*/ | ||
// 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` | ||
|
||
/** | ||
Returns the index of the last occurrence of a value in an array, or `-1` if it is not present. | ||
|
||
@example | ||
``` | ||
type T = LastIndexOf<readonly ['a', 'c', 'c'], 'c'>; | ||
//=> 2 | ||
|
||
type T = LastIndexOf<[1, 2, 3], 4>; | ||
//=> -1 | ||
|
||
type T = LastIndexOf<[{a: 1}, {a: 1}, {b: 1}], {a: 1}>; | ||
//=> 1 | ||
``` | ||
|
||
@author benzaria | ||
@see IndexOf, IndiciesOf | ||
@category Array | ||
*/ | ||
export type LastIndexOf< | ||
Array_ extends UnknownArray, Item, | ||
FromIndex extends number = 0, | ||
> = ( | ||
IndexOf<Reverse<Array_>, Item, FromIndex> extends infer Index extends number | ||
? Index extends -1 | ||
? -1 | ||
: SubtractPositives<Array_['length'], Increment<Index>> | ||
: never | ||
); |
This file contains hidden or 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,41 @@ | ||
import type {Increment, IndexOf} from './index-of.d.ts'; | ||
import type {UnknownArray} from './unknown-array.d.ts'; | ||
|
||
/** | ||
Search for an item in a array and return it's indices. | ||
*/ | ||
type _IndicesOf< | ||
Array_ extends UnknownArray, Item, | ||
FromIndex extends number = 0, | ||
Indices extends number[] = [], | ||
> = ( | ||
IndexOf<Array_, Item, FromIndex> extends infer Index extends number | ||
? Index extends -1 | ||
? Indices | ||
: _IndicesOf<Array_, Item, Increment<Index>, [...Indices, Index]> | ||
: never | ||
); | ||
|
||
/** | ||
Returns the index of the first occurrence of a value in an array, or `-1` if it is not present. | ||
|
||
@example | ||
``` | ||
type T = IndicesOf<readonly ['a', 'c', 'c'], 'c'>; | ||
//=> [1, 2] | ||
|
||
type T = IndicesOf<[1, 2, 3], 4>; | ||
//=> [] | ||
|
||
type T = IndicesOf<[{a: 1}, {a: 1}, {b: 1}], {a: 1}>; | ||
//=> [0, 1] | ||
``` | ||
|
||
@author benzaria | ||
@see IndexOf, LastIndexOf | ||
@category Array | ||
*/ | ||
export type IndicesOf< | ||
Array_ extends UnknownArray, Item, | ||
FromIndex extends number = 0, | ||
> = _IndicesOf<Array_, Item, FromIndex>; |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you exporting it? It's not used elsewhere.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
Sum
,Substract
with a range of 10999 number, with also Multiplication, Division. do u consider adding does types or it just to much ?