-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
142 additions
and
18 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,5 @@ | ||
--- | ||
'type-plus': minor | ||
--- | ||
|
||
Add `ArrayPlus.Entries<A>`. |
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,12 @@ | ||
/** | ||
* Concats two arrays or tuples. | ||
* | ||
* alias of: `[...A, ...B]` | ||
* | ||
* @alias ArrayPlus.Concat | ||
* | ||
* ```ts | ||
* type R = Concat<[1], [2, 3]> // [1, 2, 3] | ||
* ``` | ||
*/ | ||
export type Concat<A extends unknown[], B extends unknown[]> = [...A, ...B] |
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,24 @@ | ||
import { it, test } from '@jest/globals' | ||
import { testType, type ArrayPlus } from '../index.js' | ||
|
||
test('behavior of array.entries()', () => { | ||
const array = [1, 2, '3'] | ||
const entries = array.entries() | ||
testType.equal<typeof entries, IterableIterator<[number, string | number]>>(true) | ||
}) | ||
|
||
test('behavior of tuple.entries()', () => { | ||
const tuple = [1, 2, '3'] as const | ||
const entries = tuple.entries() | ||
testType.equal<typeof entries, IterableIterator<[number, 1 | 2 | '3']>>(true) | ||
}) | ||
|
||
it('gets Array<[number, T]> for array', () => { | ||
testType.equal<ArrayPlus.Entries<string[]>, Array<[number, string]>>(true) | ||
testType.equal<ArrayPlus.Entries<Array<string | number>>, Array<[number, string | number]>>(true) | ||
}) | ||
|
||
it('returns [[0, T1], [1, T2],...[n, Tn]] for tuple', () => { | ||
testType.equal<ArrayPlus.Entries<[]>, []>(true) | ||
testType.equal<ArrayPlus.Entries<[1, 2, 3]>, [[0, 1], [1, 2], [2, 3]]>(true) | ||
}) |
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,24 @@ | ||
import type { IsTuple } from '../tuple/tuple_type.js' | ||
/** | ||
* Returns an array of key-value pairs for every entry in the array or tuple. | ||
* | ||
* Note that this is not the same as `Array.entries(A)`, | ||
* which returns an iterable interator. | ||
* | ||
* @example | ||
* ```ts | ||
* ArrayPlus.Entries<Array<string | number>> // Array<[number, string | number]> | ||
* ArrayPlus.Entries<[1, 2, 3]> // [[0, 1], [1, 2], [2, 3]] | ||
* ``` | ||
*/ | ||
export type Entries<A extends unknown[]> = IsTuple< | ||
A, | ||
Device<A, []>, | ||
A extends Array<infer T> ? Array<[number, T]> : never | ||
> | ||
|
||
type Device<A extends unknown[], R extends unknown[]> = A['length'] extends 0 | ||
? R | ||
: A extends [...infer F, infer N] | ||
? Device<F, [[F['length'], N], ...R]> | ||
: never |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export type { At, Concat } from './array.js' | ||
export type { At } from './array.at.js' | ||
export type { Concat } from './array.concat.js' | ||
export type { Entries } from './array.entries.js' | ||
export type { IndexAt, IsIndexOutOfBound } from './array_index.js' |
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