Skip to content

Commit 922600a

Browse files
authored
feat(@angular-ru/common): support nullable for array utils (#682)
* feat(@angular-ru/common): support nullable values when update array * feat(@angular-ru/common): support nullable for array utils
1 parent ff3b95f commit 922600a

File tree

10 files changed

+68
-6
lines changed

10 files changed

+68
-6
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Any } from '@angular-ru/common/typings';
22

3+
/**
4+
* @deprecated: use takeFirstItem
5+
*/
36
export function firstItem<T>(array?: T[] | null | undefined, fallback: Any = null): T | null {
47
return Array.isArray(array) && array.length ? array[0] ?? fallback : null!;
58
}

packages/common/array/src/public_api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export { isMultipleList } from './is-multiple-list';
1212
export { isSingleList } from './is-single-list';
1313
export { partition } from './partition';
1414
export { secondItem } from './second-item';
15+
export { takeFirstItem } from './take-first-item';
16+
export { takeLastItem } from './take-last-item';
17+
export { takeSecondItem } from './take-second-item';
18+
export { takeThirdItem } from './take-third-item';
1519
export { thirdItem } from './third-item';
1620
export { unique } from './unique';
1721
export { uniqueArrayOf } from './unique-array-of';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Any } from '@angular-ru/common/typings';
22

3+
/**
4+
* @deprecated: use takeSecondItem
5+
*/
36
export function secondItem<T>(array?: T[] | null | undefined, fallback: Any = null): T | null {
47
return Array.isArray(array) && array.length ? array[1] ?? fallback : null!;
58
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Nullable } from '@angular-ru/common/typings';
2+
3+
export function takeFirstItem<T>(array?: Nullable<T[]>): Nullable<T> {
4+
return array?.[0];
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Nullable } from '@angular-ru/common/typings';
2+
3+
export function takeLastItem<T>(array?: Nullable<T[]>): Nullable<T> {
4+
return array?.[array?.length - 1];
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Nullable } from '@angular-ru/common/typings';
2+
3+
export function takeSecondItem<T>(array?: Nullable<T[]>): Nullable<T> {
4+
return array?.[1];
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Nullable } from '@angular-ru/common/typings';
2+
3+
export function takeThirdItem<T>(array?: Nullable<T[]>): Nullable<T> {
4+
const thirdItemIndex: number = 2;
5+
return array?.[thirdItemIndex];
6+
}

packages/common/array/src/third-item.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Any } from '@angular-ru/common/typings';
22

3+
/**
4+
* @deprecated: use takeThirdItem
5+
*/
36
export function thirdItem<T>(array?: T[] | null | undefined, fallback: Any = null): T | null {
47
const thirdItemIndex: number = 2;
58
return Array.isArray(array) && array.length ? array[thirdItemIndex] ?? fallback : null!;

packages/common/integration/tests/array/utility-arrays.spec.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import {
1212
hasOneItem,
1313
partition,
1414
secondItem,
15-
thirdItem
15+
thirdItem,
16+
takeFirstItem,
17+
takeSecondItem,
18+
takeThirdItem,
19+
takeLastItem
1620
} from '@angular-ru/common/array';
1721
import { isNumber } from '@angular-ru/common/number';
1822
import { PlainObject } from '@angular-ru/common/typings';
@@ -101,6 +105,12 @@ describe('[TEST]: Array utility', () => {
101105
expect(firstItem([])).toEqual(null);
102106
expect(firstItem([1])).toEqual(1);
103107
expect(firstItem([1, 2])).toEqual(1);
108+
109+
expect(takeFirstItem()).toEqual(undefined);
110+
expect(takeFirstItem(null)).toEqual(undefined);
111+
expect(takeFirstItem([])).toEqual(undefined);
112+
expect(takeFirstItem([1])).toEqual(1);
113+
expect(takeFirstItem([1, 2])).toEqual(1);
104114
});
105115

106116
it('is second item', () => {
@@ -109,6 +119,12 @@ describe('[TEST]: Array utility', () => {
109119
expect(secondItem([])).toEqual(null);
110120
expect(secondItem([1])).toEqual(null);
111121
expect(secondItem([1, 2])).toEqual(2);
122+
123+
expect(takeSecondItem()).toEqual(undefined);
124+
expect(takeSecondItem(null)).toEqual(undefined);
125+
expect(takeSecondItem([])).toEqual(undefined);
126+
expect(takeSecondItem([1])).toEqual(undefined);
127+
expect(takeSecondItem([1, 2])).toEqual(2);
112128
});
113129

114130
it('is third item', () => {
@@ -118,6 +134,20 @@ describe('[TEST]: Array utility', () => {
118134
expect(thirdItem([1])).toEqual(null);
119135
expect(thirdItem([1, 2])).toEqual(null);
120136
expect(thirdItem([1, 2, 3])).toEqual(3);
137+
138+
expect(takeThirdItem()).toEqual(undefined);
139+
expect(takeThirdItem(null)).toEqual(undefined);
140+
expect(takeThirdItem([])).toEqual(undefined);
141+
expect(takeThirdItem([1])).toEqual(undefined);
142+
expect(takeThirdItem([1, 2, 3])).toEqual(3);
143+
});
144+
145+
it('is last item', () => {
146+
expect(takeLastItem()).toEqual(undefined);
147+
expect(takeLastItem(null)).toEqual(undefined);
148+
expect(takeLastItem([])).toEqual(undefined);
149+
expect(takeLastItem([1])).toEqual(1);
150+
expect(takeLastItem([1, 2, 3, 4])).toEqual(4);
121151
});
122152

123153
it('should divide array by condition', () => {

packages/common/pipes/src/mark-by-filter/mark-by-filter.pipe.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@ import { Pipe, PipeTransform } from '@angular/core';
22
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
33
import { ensureRegexp, isRegexpStr } from '@angular-ru/common/regexp';
44
import { toStringVal } from '@angular-ru/common/string';
5-
import { Any } from '@angular-ru/common/typings';
5+
import { Any, Nullable } from '@angular-ru/common/typings';
66
import { isNotNil } from '@angular-ru/common/utils';
77

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

10-
type Value = string | null | undefined;
11-
1210
@Pipe({ name: 'markByFilter' })
1311
export class MarkByFilterPipe implements PipeTransform {
1412
constructor(private readonly sanitizer: DomSanitizer) {}
1513

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

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

0 commit comments

Comments
 (0)