-
-
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
13 changed files
with
176 additions
and
89 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": patch | ||
--- | ||
|
||
Separate `Filter` and `PadStart` for array and tuple |
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 @@ | ||
export type Filter<A extends unknown[], Criteria> = A[0] extends Criteria | ||
? A | ||
: Criteria extends A[0] | ||
? Array<Criteria> | ||
: 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { CanAssign } from '../index.js' | ||
import type { CreateTuple } from '../tuple/create_tuple.js' | ||
import type { UnionOfValues } from './union_of_values.js' | ||
|
||
export type PadStart< | ||
A extends unknown[], | ||
MaxLength extends number, | ||
PadWith = unknown | ||
> = MaxLength extends 0 | ||
? A | ||
: CanAssign<PadWith, UnionOfValues<A>> extends true | ||
? A | ||
: PadStart<[...CreateTuple<MaxLength, PadWith>, ...A], MaxLength, PadWith> |
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 |
---|---|---|
@@ -1,27 +1,15 @@ | ||
import type { Filter as FilterTuple } from '../tuple/tuple_plus.filter.js' | ||
import type { Filter as FilterArray } from './array_plus.filter.js' | ||
|
||
/** | ||
* filter the array or tuple `A`, keeping entries satisfying `Criteria`. | ||
*/ | ||
export type Filter<A extends Array<any>, Criteria> = number extends A['length'] | ||
? // array | ||
A[0] extends Criteria | ||
? A | ||
: Criteria extends A[0] | ||
? Array<Criteria> | ||
: never[] | ||
: // tuple | ||
A['length'] extends 0 | ||
? never | ||
: A extends [infer Head, ...infer Tail] | ||
? Tail['length'] extends 0 | ||
? Head extends Criteria | ||
? [Head] | ||
: never[] | ||
: Head extends Criteria | ||
? [Head, ...Filter<Tail, Criteria>] | ||
: Filter<Tail, Criteria> | ||
: never | ||
export type Filter<A extends unknown[], Criteria> = number extends A['length'] | ||
? FilterArray<A, Criteria> | ||
: FilterTuple<A, Criteria> | ||
|
||
|
||
/** | ||
* keeps entries satisfying `Criteria` in array or tuple `A`. | ||
*/ | ||
export type KeepMatch<A extends Array<any>, Criteria> = Filter<A, Criteria> | ||
export type KeepMatch<A extends unknown[], Criteria> = Filter<A, Criteria> |
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,29 @@ | ||
/** | ||
* Filter entries matching `Criteria` in tuple `T`. | ||
* | ||
* ⚗️ *transform* | ||
* | ||
* @example | ||
* ```ts | ||
* type R = Filter<[1, 2, '3'], number> // [1, 2] | ||
* type R = Filter<[1, 2, '3'], true> // [] | ||
* ``` | ||
*/ | ||
export type Filter<T extends unknown[], Criteria> = T['length'] extends 0 | ||
? [] | ||
: ( | ||
T extends [infer Head, ...infer Tail] | ||
? ( | ||
Tail['length'] extends 0 | ||
? ( | ||
Head extends Criteria | ||
? [Head] | ||
: []) | ||
: ( | ||
Head extends Criteria | ||
? [Head, ...Filter<Tail, Criteria>] | ||
: Filter<Tail, Criteria> | ||
) | ||
) | ||
: 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,15 +1,14 @@ | ||
import { it } from '@jest/globals' | ||
import { testType } from '../index.js' | ||
import type { PadStart } from './tuple_plus.js' | ||
import { testType, type TuplePlus } from '../index.js' | ||
|
||
it('pads with unknown', () => { | ||
testType.equal<PadStart<[1, 2, 3], 5>, [unknown, unknown, 1, 2, 3]>(true) | ||
testType.equal<TuplePlus.PadStart<[1, 2, 3], 5>, [unknown, unknown, 1, 2, 3]>(true) | ||
}) | ||
|
||
it('returns source type if total length is less than source length', () => { | ||
testType.equal<PadStart<[1, 2, 3], 2>, [1, 2, 3]>(true) | ||
testType.equal<TuplePlus.PadStart<[1, 2, 3], 2>, [1, 2, 3]>(true) | ||
}) | ||
|
||
it('can specify what to pad with', () => { | ||
testType.equal<PadStart<[1, 2, 3], 5, 0>, [0, 0, 1, 2, 3]>(true) | ||
testType.equal<TuplePlus.PadStart<[1, 2, 3], 5, 0>, [0, 0, 1, 2, 3]>(true) | ||
}) |
Oops, something went wrong.