Skip to content

feat(@angular-ru/common): support nullable for array utils #682

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 5 commits into from
Jun 18, 2021

Conversation

splincode
Copy link
Member

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

[ ] Yes
[ ] No

Other information

@splincode splincode requested a review from Markiewic as a code owner June 18, 2021 12:33
@splincode splincode changed the title Refactor/nullable feat(@angular-ru/common): support nullable for array utils Jun 18, 2021
@splincode splincode merged commit 922600a into master Jun 18, 2021
@splincode splincode deleted the refactor/nullable branch June 18, 2021 13:04
@@ -0,0 +1,5 @@
import { Nullable } from '@angular-ru/common/typings';

export function takeFirstItem<T>(array?: Nullable<T[]>): Nullable<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generates tuples with given length. [type] and [type, ...type[]].

type Tuple<T, N extends number = 1, R extends unknown[] = []> = R['length'] extends N ? R : Tuple<T, N, [T, ...R]>;
type InfiniteTuple<T, N extends number = 1> = [...Tuple<T, N>, ...T[]];

Returns type of Indexs item of array or fallbacks to undefined. if there is no certainty that the element is presented, append undefined.

type ItemOrUndefined<ArrayType, Index extends number> = ArrayType extends unknown[]
    ? ArrayType extends [unknown, ...InfiniteTuple<unknown, Index>]
        ? ArrayType[0]
        : ArrayType[0] | undefined
    : undefined;

Now let's use it in our function return type:

export function takeFirstItem<ArrayType extends Nullable<unknown[]>>(array: ArrayType): ItemOrUndefined<ArrayType, 0> {
    return (Array.isArray(array) ? array[0] : undefined) as ItemOrUndefined<ArrayType, 0>;
}

And another functions:

export function takeSecondItem<ArrayType extends Nullable<unknown[]>>(array: ArrayType): ItemOrUndefined<ArrayType, 1> {
    return (Array.isArray(array) ? array[1] : undefined) as ItemOrUndefined<ArrayType, 1>;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

splincode added a commit that referenced this pull request Jul 7, 2021
* feat(@angular-ru/common): support nullable values when update array

* feat(@angular-ru/common): support nullable for array utils
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants