-
-
Notifications
You must be signed in to change notification settings - Fork 655
Add Filter type
#1183
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
Open
benzaria
wants to merge
45
commits into
sindresorhus:main
Choose a base branch
from
benzaria:Filter
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.
+542
−3
Open
Add Filter type
#1183
Changes from 44 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
9cda493
feat: init `Filter` type
benzaria a56ed2c
feat: add extra tests, refactor type for readability, add extra condi…
benzaria c100e9c
Merge branch 'sindresorhus:main' into Filter
benzaria 80404b5
refactor: improve JSDoc links and clean up test cases for ArrayFilter
benzaria 20dffe3
Merge branch 'origin/Filter' into Filter
benzaria 640cb4f
feat: fix `IsLeadingRestElement` & add `IsTrailingRestElement`
benzaria 26e2b0c
Remake the `ArrayFilter` type and cleanup unused types
benzaria 416d35a
fix: test errors
benzaria d10b19e
Merge branch 'main' into Filter
benzaria d4b89c2
Merge branch 'main' into Filter
benzaria c00320b
reverte: changes on `IsUnion`
benzaria 691b9a1
feat: add `strict` option to `Filter`
benzaria d4fb6ae
improve: added types for `Filter`
benzaria f32aed0
test: fix test errors
benzaria 67ae5b8
improve: `ObjectFilter`
benzaria 8b13bea
Update array.d.ts
sindresorhus 3d2baa7
Update filter.d.ts
sindresorhus d8e2a57
Merge remote-tracking branch 'upstream/main' into Filter
benzaria 2eb53d0
Merge branch 'origin/Filter' into Filter
benzaria 97969ae
feat: improve `Filter` type, test and docs, add test and docs for `Ob…
benzaria 022e082
doc: add examples for `strict` option
benzaria 6498c01
Merge branch 'main' into Filter
benzaria c24f3a7
add `export {}` to `Filter`
benzaria af04729
add `export {}` to `ObjectFilter`
benzaria ed8646f
fix: export errors
benzaria 5dcaa2b
refactor: `ObjectFIlter` test
benzaria 2502eef
Update readme.md
sindresorhus 9c29c46
Update filter.d.ts
sindresorhus 92aba18
Update type.d.ts
sindresorhus b88b1e1
Update object-filter.d.ts
sindresorhus 29eb5e5
`Filter`: add preserve readonly modifier, fix JsDoc example, add some…
benzaria 7b2cd09
`FilterObject`: rename type, some refactoring
benzaria 9cb21a0
minor changes
benzaria 3c606dd
Merge branch 'origin/Filter' into Filter
benzaria a89d8bd
fix `CleanEmpty` error
benzaria b2dc2be
Merge branch 'main' into Filter
benzaria 9984532
feat: merge `FilterObject` to `Filter`
benzaria 58654b0
docs: fix JsDoc examples
benzaria 10477ba
Update filter.d.ts
sindresorhus 11a0b22
Fix grammar in FilterObject and FilterArray comments
sindresorhus 2f5541f
Update array.d.ts
sindresorhus 6a9b4b7
Update filter.d.ts
sindresorhus bd1a9bb
Fix duplicate lines in Extends type documentation
sindresorhus 3a4b48b
Update type.d.ts
sindresorhus 9d80cf1
minor comments change
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import type {IsTruthy, Extends, IsAnyOrNever} from './internal/type.d.ts'; | ||
| import type {CleanEmpty, IsArrayReadonly} from './internal/array.d.ts'; | ||
| import type {ApplyDefaultOptions} from './internal/index.d.ts'; | ||
| import type {IsOptionalKeyOf} from './is-optional-key-of.d.ts'; | ||
| import type {UnknownRecord} from './unknown-record.d.ts'; | ||
| import type {UnknownArray} from './unknown-array.d.ts'; | ||
| import type {Simplify} from './simplify.d.ts'; | ||
| import type {If} from './if.d.ts'; | ||
|
|
||
| /** | ||
| {@link Filter `Filter`} options. | ||
| */ | ||
| export type FilterOptions = { | ||
| /** | ||
| Controls the strictness of type checking when filtering. | ||
|
|
||
| - When `true`, the entire union type **must** extend the filter type. For example, `string | number extends string` returns `false`. | ||
| - When `false`, the check passes if **any** member of the union extends the filter type. For example, `string | number extends string` returns `true`. | ||
|
|
||
| @default false | ||
|
|
||
| @example | ||
| ``` | ||
| import type {Filter} from 'type-fest'; | ||
|
|
||
| type T1 = Filter<[1, 2, 3 | 4, 3?, 4?], 3>; | ||
| //=> [3 | 4, 3?] | ||
|
|
||
| type T2 = Filter<[1, 2, 3 | 4, 3?, 4?], 3, {strict: true}>; | ||
| //=> [3?] | ||
|
|
||
| type T3 = Filter<{a: 1; b: 2; c: 3 | 4; d?: 3; e?: 4}, 3>; | ||
| //=> {c: 3 | 4; d?: 3} | ||
|
|
||
| type T4 = Filter<{a: 1; b: 2; c: 3 | 4; d?: 3; e?: 4}, 3, {strict: true}>; | ||
| //=> {d?: 3} | ||
| ``` | ||
| */ | ||
| strict?: boolean; | ||
| }; | ||
|
|
||
| type DefaultFilterOptions = { | ||
| strict: false; | ||
| }; | ||
|
|
||
| /** | ||
| Returns a boolean for whether a value `T` extends the filtering type `U`. | ||
|
|
||
| If `U` is `Boolean`, it checks whether `T` is `truthy` like {@link Boolean `Boolean(T)`} does. | ||
|
|
||
| Otherwise, it uses {@link Extends `Extends<T, U, S>`} to check if `T extends U` with strict or loose mode. | ||
| */ | ||
| type FilterType<T, U, S extends boolean> = | ||
| Boolean extends U | ||
| ? IsTruthy<T> | ||
| : Extends<T, U, S>; | ||
|
|
||
| /** | ||
| Determines whether the array `V` should be kept based on the boolean type `T`. | ||
| */ | ||
| type IfFilter<T extends boolean, V extends UnknownArray> = [T] extends [true] ? V : []; | ||
|
|
||
| /** | ||
| Filters elements or properties from an array or object based on whether they extend the specified filter type. | ||
|
|
||
| If `FilterType` is `Boolean`, it filters out `falsy` values like {@link Boolean `Boolean(T)`} does. | ||
|
|
||
| Optional control for strict or loose type comparison. | ||
|
|
||
| @example | ||
| ``` | ||
| import type {Filter} from 'type-fest'; | ||
|
|
||
| type A1 = Filter<[1, 2, 3 | 4, 3?, 4?], 3>; | ||
| //=> [3 | 4, 3?] | ||
|
|
||
| type A2 = Filter<[1, 2, 3 | 4, 3?, 4?], 3, {strict: true}>; | ||
| //=> [3?] | ||
|
|
||
| type A3 = Filter<['foo1', 'bar2', 'fooo', 'foo3'], `foo${number}`>; | ||
| //=> ['foo1', 'foo3'] | ||
|
|
||
| type A4 = Filter<[1, '2', 3, 'foo', false], string | number>; | ||
| //=> [1, '2', 3, 'foo'] | ||
|
|
||
| type A5 = Filter<[true, false, boolean, 0, 1], Boolean>; | ||
| //=> [true, 1] | ||
|
|
||
| type A6 = Filter<[0, '', false, null, undefined, 'ok', 42], Boolean>; | ||
| //=> ['ok', 42] | ||
| ``` | ||
|
|
||
| @example | ||
| ``` | ||
| import type {Filter} from 'type-fest'; | ||
|
|
||
| type O1 = Filter<{a: 1; b: 2; c: 3 | 4; d?: 3; e?: 4}, 3>; | ||
| //=> {c: 3 | 4; d?: 3} | ||
|
|
||
| type O2 = Filter<{a: 1; b: 2; c: 3 | 4; d?: 3; e?: 4}, 3, {strict: true}>; | ||
| //=> {c?: 3} | ||
|
|
||
| type O3 = Filter<{a: 'foo1'; b: 'bar2'; c: 'fooo'; d: 'foo3'}, `foo${number}`>; | ||
| //=> {a: 'foo1'; d: 'foo3'} | ||
|
|
||
| type O4 = Filter<{a: 1; b: '2'; c: 3; d: 'foo'; e: false}, string | number>; | ||
| //=> {a: 1; b: '2'; c: 3; d: 'foo'} | ||
|
|
||
| type O5 = Filter<{a: true; b: false; c: boolean; d: 0; e: 1}, Boolean>; | ||
| //=> {a: true; e: 1} | ||
|
|
||
| type O6 = Filter<{a: 0; b: ''; c: false; d: null; e: undefined; f: 'ok'; g: 42}, Boolean>; | ||
| //=> {f: 'ok'; g: 42} | ||
| ``` | ||
|
|
||
| @category Array | ||
| @category Object | ||
| */ | ||
| export type Filter< | ||
| BaseType extends UnknownArray | UnknownRecord, | ||
| FilterType, | ||
| Options extends FilterOptions = {}, | ||
| > = IsAnyOrNever<BaseType> extends true ? BaseType | ||
| : ApplyDefaultOptions< | ||
| FilterOptions, | ||
| DefaultFilterOptions, | ||
| Options | ||
| > extends infer ResolvedOptions extends Required<FilterOptions> | ||
| ? CleanEmpty< | ||
| BaseType extends UnknownArray | ||
| ? FilterArray<BaseType, FilterType, ResolvedOptions> | ||
| : BaseType extends UnknownRecord | ||
| ? FilterObject<BaseType, FilterType, ResolvedOptions> | ||
| : never | ||
| > | ||
| : never; | ||
|
|
||
| /** | ||
| Iterates through the object properties and keeps them if they pass {@link FilterType `FilterType`}. | ||
| */ | ||
| type FilterObject< | ||
| Object_ extends UnknownRecord, Type, | ||
| Options extends Required<FilterOptions>, | ||
| > = Simplify<{ | ||
| [Key in keyof Object_ as | ||
| FilterType<Object_[Key], Type, Options['strict']> extends true | ||
| ? Key | ||
| : never | ||
| ]: Object_[Key] | ||
| }>; | ||
|
|
||
| /** | ||
| Iterates through the array elements and keeps them if they pass {@link FilterType `FilterType`}. | ||
| */ | ||
| type FilterArray< | ||
| Array_ extends UnknownArray, Type, | ||
| Options extends Required<FilterOptions>, | ||
| > = | ||
| _FilterArray<Array_, Type, Options> extends infer Result extends UnknownArray | ||
| ? If<IsArrayReadonly<Array_>, Readonly<Result>, Result> // Preserve readonly modifier | ||
| : never; | ||
|
|
||
| /** | ||
| Core implementation of {@link FilterArray}. | ||
| */ | ||
| type _FilterArray< | ||
| Array_ extends UnknownArray, Type, | ||
| Options extends Required<FilterOptions>, | ||
| HeadAcc extends UnknownArray = [], | ||
| TailAcc extends UnknownArray = [], | ||
| > = | ||
| keyof Array_ & `${number}` extends never // Is `Array_` a rest element or empty array | ||
| ? Array_ extends readonly [...infer Rest, infer Last] | ||
| // Add `Last` to `TailAcc` if it passes the filter | ||
| ? _FilterArray<Rest, Type, Options, HeadAcc, [ | ||
| ...IfFilter<FilterType<Last, Type, Options['strict']>, [Last]>, | ||
| ...TailAcc, | ||
| ]> | ||
| // Handle rest element (spread) or return the final result | ||
| : [ | ||
| ...HeadAcc, | ||
| ...IfFilter<FilterType<Array_[number], Type, Options['strict']>, Array_>, | ||
| ...TailAcc, | ||
| ] | ||
| : Array_ extends readonly [(infer First)?, ...infer Rest] | ||
| // Add `First` to `HeadAcc` if it passes the filter | ||
| ? _FilterArray<Rest, Type, Options, [ | ||
| ...HeadAcc, | ||
| ...IfFilter< | ||
| FilterType<First, Type, Options['strict']>, | ||
| // Preserve optional modifier | ||
| If<IsOptionalKeyOf<Array_, 0>, [First?], [First]> | ||
| >, | ||
| ], TailAcc> | ||
| : never; | ||
| export {}; | ||
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 |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import type {If} from '../if.d.ts'; | ||
| import type {IsNever} from '../is-never.d.ts'; | ||
| import type {OptionalKeysOf} from '../optional-keys-of.d.ts'; | ||
| import type {EmptyObject} from '../empty-object.d.ts'; | ||
| import type {UnknownArray} from '../unknown-array.d.ts'; | ||
| import type {OptionalKeysOf} from '../optional-keys-of.d.ts'; | ||
| import type {IsExactOptionalPropertyTypesEnabled, IfNotAnyOrNever} from './type.d.ts'; | ||
|
|
||
| /** | ||
|
|
@@ -87,6 +88,42 @@ Returns whether the given array `T` is readonly. | |
| */ | ||
| export type IsArrayReadonly<T extends UnknownArray> = If<IsNever<T>, false, T extends unknown[] ? false : true>; | ||
|
|
||
| /** | ||
| Represents an empty array, the `[]` or `readonly []` value. | ||
| */ | ||
| export type EmptyArray = readonly [] | []; // The extra `[]` is just to prevent TS from expanding the type. | ||
|
|
||
| /** | ||
| Returns a `boolean` for whether the type is an empty array, the `[]` or `readonly []` value. | ||
|
|
||
| @example | ||
| ``` | ||
| import type {IsEmptyArray} from 'type-fest'; | ||
|
|
||
| type Pass1 = IsEmptyArray<[]>; | ||
| //=> true | ||
|
|
||
| type Pass2 = IsEmptyArray<readonly []>; | ||
| //=> true | ||
|
|
||
| type Fail1 = IsEmptyArray<[0]>; | ||
| //=> false | ||
|
|
||
| type Fail2 = IsEmptyArray<[0?]>; | ||
| //=> false | ||
|
|
||
| type Fail3 = IsEmptyArray<...string[]>; | ||
|
||
| //=> false | ||
| ``` | ||
|
|
||
| @see {@link EmptyArray} | ||
| @category Array | ||
| */ | ||
| export type IsEmptyArray<T> = | ||
| IsNever<T> extends true ? false | ||
| : T extends EmptyArray ? true | ||
| : false; | ||
|
|
||
| /** | ||
| Transforms a tuple type by replacing it's rest element with a single element that has the same type as the rest element, while keeping all the non-rest elements intact. | ||
|
|
||
|
|
@@ -148,4 +185,32 @@ type _CollapseRestElement< | |
| : never // Should never happen, since `[(infer First)?, ...infer Rest]` is a top-type for arrays. | ||
| : never; // Should never happen | ||
|
|
||
| /** | ||
| Cleans any extra empty arrays/objects from a union. | ||
|
|
||
| @example | ||
| ``` | ||
| type T1 = CleanEmpty<[number] | []>; | ||
| //=> [number] | ||
|
|
||
| type T2 = CleanEmpty<[number, string?] | [never] | []>; | ||
| //=> [number, string?] | [never] | ||
|
|
||
| type T3 = CleanEmpty<{a: 'A'} | {}>; | ||
| //=> {a: 'A'} | ||
|
|
||
| type T4 = CleanEmpty<[]>; | ||
| //=> [] | ||
|
|
||
| type T5 = CleanEmpty<{}>; | ||
| //=> {} | ||
| ``` | ||
|
|
||
| @category Utilities | ||
| */ | ||
| export type CleanEmpty<T> = | ||
| Exclude<T, EmptyArray | EmptyObject> extends infer U | ||
| ? IsNever<U> extends true ? T : U | ||
| : never; | ||
|
|
||
| export {}; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.