-
Notifications
You must be signed in to change notification settings - Fork 15
feat(@angular-ru/common): item functions type #684
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
442a08a
feat(@angular-ru/common): item functions type
Markiewic 542ece0
fixup! feat(@angular-ru/common): item functions type
Markiewic e207a28
Merge branch 'master' into feat/item-types
Markiewic cb449ac
fixup! feat(@angular-ru/common): item functions type
Markiewic c8f039a
Merge branch 'master' into feat/item-types
splincode b28a811
Merge branch 'master' into feat/item-types
splincode a5172a9
branch was auto-updated
splincode 9517362
branch was auto-updated
splincode a07777f
branch was auto-updated
splincode 47dbb95
Merge branch 'master' into feat/item-types
Markiewic 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { Nullable } from '@angular-ru/common/typings'; | ||
import { Nullable, TupleItem } from '@angular-ru/common/typings'; | ||
|
||
export function takeFirstItem<T>(array?: Nullable<T[]>): Nullable<T> { | ||
return array?.[0]; | ||
export function takeFirstItem<ArrayType extends Nullable<unknown[]>>(array: ArrayType): TupleItem<ArrayType, 0> { | ||
return (Array.isArray(array) ? array[0] : undefined) as TupleItem<ArrayType, 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { Nullable } from '@angular-ru/common/typings'; | ||
import { LastOfTuple, Nullable } from '@angular-ru/common/typings'; | ||
|
||
export function takeLastItem<T>(array?: Nullable<T[]>): Nullable<T> { | ||
return array?.[array?.length - 1]; | ||
export function takeLastItem<ArrayType extends Nullable<unknown[]>>(array: ArrayType): LastOfTuple<ArrayType> { | ||
return (Array.isArray(array) ? array[array.length - 1] : undefined) as LastOfTuple<ArrayType>; | ||
} |
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,5 +1,5 @@ | ||
import { Nullable } from '@angular-ru/common/typings'; | ||
import { Nullable, TupleItem } from '@angular-ru/common/typings'; | ||
|
||
export function takeSecondItem<T>(array?: Nullable<T[]>): Nullable<T> { | ||
return array?.[1]; | ||
export function takeSecondItem<ArrayType extends Nullable<unknown[]>>(array: ArrayType): TupleItem<ArrayType, 1> { | ||
return (Array.isArray(array) ? array[1] : undefined) as TupleItem<ArrayType, 1>; | ||
} |
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,6 +1,10 @@ | ||
import { Nullable } from '@angular-ru/common/typings'; | ||
import { Nullable, TupleItem } from '@angular-ru/common/typings'; | ||
|
||
export function takeThirdItem<T>(array?: Nullable<T[]>): Nullable<T> { | ||
const thirdItemIndex: number = 2; | ||
return array?.[thirdItemIndex]; | ||
type ThirdItemIndex = 2; | ||
const thirdItemIndex: ThirdItemIndex = 2; | ||
|
||
export function takeThirdItem<ArrayType extends Nullable<unknown[]>>( | ||
array: ArrayType | ||
): TupleItem<ArrayType, ThirdItemIndex> { | ||
return (Array.isArray(array) ? array[thirdItemIndex] : undefined) as TupleItem<ArrayType, ThirdItemIndex>; | ||
} |
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,3 @@ | ||
import { Tuple } from './tuple'; | ||
|
||
export type InfiniteTuple<ItemType, Length extends number = 1> = [...Tuple<ItemType, Length>, ...ItemType[]]; |
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,7 @@ | ||
export type LastOfTuple<ArrayType> = ArrayType extends unknown[] | ||
? ArrayType extends [] | ||
? undefined | ||
: ArrayType extends [unknown, ...unknown[]] | ||
? ArrayType[ArrayType extends [unknown, ...infer Rest] ? Rest['length'] : number] | ||
: ArrayType[number] | undefined | ||
: undefined; |
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,7 @@ | ||
import { InfiniteTuple } from './infinite-tuple'; | ||
|
||
export type TupleItem<ArrayType, Index extends number> = ArrayType extends unknown[] | ||
? ArrayType extends [unknown, ...InfiniteTuple<unknown, Index>] | ||
? ArrayType[Index] | ||
: ArrayType[Index] | undefined | ||
: undefined; |
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,3 @@ | ||
export type Tuple<ItemType, Length extends number = 1, Base extends unknown[] = []> = Base['length'] extends Length | ||
? Base | ||
: Tuple<ItemType, Length, [ItemType, ...Base]>; |
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.