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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/common/array/src/first-item.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Any } from '@angular-ru/common/typings';

/**
* @deprecated: use takeFirstItem
*/
export function firstItem<T>(array?: T[] | null | undefined, fallback: Any = null): T | null {
return Array.isArray(array) && array.length ? array[0] ?? fallback : null!;
}
4 changes: 4 additions & 0 deletions packages/common/array/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export { isMultipleList } from './is-multiple-list';
export { isSingleList } from './is-single-list';
export { partition } from './partition';
export { secondItem } from './second-item';
export { takeFirstItem } from './take-first-item';
export { takeLastItem } from './take-last-item';
export { takeSecondItem } from './take-second-item';
export { takeThirdItem } from './take-third-item';
export { thirdItem } from './third-item';
export { unique } from './unique';
export { uniqueArrayOf } from './unique-array-of';
Expand Down
3 changes: 3 additions & 0 deletions packages/common/array/src/second-item.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Any } from '@angular-ru/common/typings';

/**
* @deprecated: use takeSecondItem
*/
export function secondItem<T>(array?: T[] | null | undefined, fallback: Any = null): T | null {
return Array.isArray(array) && array.length ? array[1] ?? fallback : null!;
}
5 changes: 5 additions & 0 deletions packages/common/array/src/take-first-item.ts
Original file line number Diff line number Diff line change
@@ -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.

return array?.[0];
}
5 changes: 5 additions & 0 deletions packages/common/array/src/take-last-item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Nullable } from '@angular-ru/common/typings';

export function takeLastItem<T>(array?: Nullable<T[]>): Nullable<T> {
return array?.[array?.length - 1];
}
5 changes: 5 additions & 0 deletions packages/common/array/src/take-second-item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Nullable } from '@angular-ru/common/typings';

export function takeSecondItem<T>(array?: Nullable<T[]>): Nullable<T> {
return array?.[1];
}
6 changes: 6 additions & 0 deletions packages/common/array/src/take-third-item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Nullable } from '@angular-ru/common/typings';

export function takeThirdItem<T>(array?: Nullable<T[]>): Nullable<T> {
const thirdItemIndex: number = 2;
return array?.[thirdItemIndex];
}
3 changes: 3 additions & 0 deletions packages/common/array/src/third-item.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Any } from '@angular-ru/common/typings';

/**
* @deprecated: use takeThirdItem
*/
export function thirdItem<T>(array?: T[] | null | undefined, fallback: Any = null): T | null {
const thirdItemIndex: number = 2;
return Array.isArray(array) && array.length ? array[thirdItemIndex] ?? fallback : null!;
Expand Down
32 changes: 31 additions & 1 deletion packages/common/integration/tests/array/utility-arrays.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
hasOneItem,
partition,
secondItem,
thirdItem
thirdItem,
takeFirstItem,
takeSecondItem,
takeThirdItem,
takeLastItem
} from '@angular-ru/common/array';
import { isNumber } from '@angular-ru/common/number';
import { PlainObject } from '@angular-ru/common/typings';
Expand Down Expand Up @@ -101,6 +105,12 @@ describe('[TEST]: Array utility', () => {
expect(firstItem([])).toEqual(null);
expect(firstItem([1])).toEqual(1);
expect(firstItem([1, 2])).toEqual(1);

expect(takeFirstItem()).toEqual(undefined);
expect(takeFirstItem(null)).toEqual(undefined);
expect(takeFirstItem([])).toEqual(undefined);
expect(takeFirstItem([1])).toEqual(1);
expect(takeFirstItem([1, 2])).toEqual(1);
});

it('is second item', () => {
Expand All @@ -109,6 +119,12 @@ describe('[TEST]: Array utility', () => {
expect(secondItem([])).toEqual(null);
expect(secondItem([1])).toEqual(null);
expect(secondItem([1, 2])).toEqual(2);

expect(takeSecondItem()).toEqual(undefined);
expect(takeSecondItem(null)).toEqual(undefined);
expect(takeSecondItem([])).toEqual(undefined);
expect(takeSecondItem([1])).toEqual(undefined);
expect(takeSecondItem([1, 2])).toEqual(2);
});

it('is third item', () => {
Expand All @@ -118,6 +134,20 @@ describe('[TEST]: Array utility', () => {
expect(thirdItem([1])).toEqual(null);
expect(thirdItem([1, 2])).toEqual(null);
expect(thirdItem([1, 2, 3])).toEqual(3);

expect(takeThirdItem()).toEqual(undefined);
expect(takeThirdItem(null)).toEqual(undefined);
expect(takeThirdItem([])).toEqual(undefined);
expect(takeThirdItem([1])).toEqual(undefined);
expect(takeThirdItem([1, 2, 3])).toEqual(3);
});

it('is last item', () => {
expect(takeLastItem()).toEqual(undefined);
expect(takeLastItem(null)).toEqual(undefined);
expect(takeLastItem([])).toEqual(undefined);
expect(takeLastItem([1])).toEqual(1);
expect(takeLastItem([1, 2, 3, 4])).toEqual(4);
});

it('should divide array by condition', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import { ensureRegexp, isRegexpStr } from '@angular-ru/common/regexp';
import { toStringVal } from '@angular-ru/common/string';
import { Any } from '@angular-ru/common/typings';
import { Any, Nullable } from '@angular-ru/common/typings';
import { isNotNil } from '@angular-ru/common/utils';

import { MarkedValue } from './marked-value';

type Value = string | null | undefined;

@Pipe({ name: 'markByFilter' })
export class MarkByFilterPipe implements PipeTransform {
constructor(private readonly sanitizer: DomSanitizer) {}

public transform(value: Value, filter?: Value, color: string = '#ffdd2d'): MarkedValue {
public transform(value: Nullable<string>, filter?: Nullable<string>, color: string = '#ffdd2d'): MarkedValue {
return isNotNil(filter) ? this.search(value, filter, color) : value;
}

private search(value: Value, filter?: Value, color?: string): SafeHtml {
private search(value: Nullable<string>, filter?: Nullable<string>, color?: string): SafeHtml {
const existFilter: boolean = isNotNil(value) && isNotNil(filter);
let newString: string | null | undefined = value;

Expand Down