pullAll

说明

移除数组 arr 中所有和给定值相等的元素, 原地操作

源码

用法

pullAll.test.ts
1
import { pullAll } from "../../utility/array/pullAll";
2
3
4
describe('pullAll tests...', () => {
5
test('Method pullAll should receive an array composed of number and return the filtered array', () => {
6
const received = {
7
origin: [1, 2, 3, 1, 2, 3],
8
selector: [2, 3],
9
};
10
const expected = {
11
isEqual: true,
12
result: [1, 1],
13
};
14
15
const result = pullAll<number>(received.origin, received.selector);
16
17
expect(result === received.origin).toBe(expected.isEqual);
18
19
result.forEach((v, i) => {
20
expect(v).toBe(expected.result[i]);
21
});
22
});
23
24
test('Method pullAll should receive an array composed of plain object and return the filtered array', () => {
25
interface IOriginParams {
26
name: string,
27
age: number,
28
};
29
30
const received = {
31
origin: [
32
{
33
name: 'duan',
34
age: 21,
35
},
36
{
37
name: 'zhao',
38
age: 22,
39
},
40
{
41
name: 'duan',
42
age: 21,
43
},
44
],
45
selector: [
46
{
47
name: 'duan',
48
age: 21,
49
},
50
],
51
};
52
const expected = {
53
isEqual: true,
54
result: [
55
{
56
name: 'duan',
57
age: 21,
58
},
59
{
60
name: 'zhao',
61
age: 22,
62
},
63
{
64
name: 'duan',
65
age: 21,
66
},
67
],
68
};
69
70
const result = pullAll<IOriginParams>(received.origin, received.selector);
71
72
expect(result === received.origin).toBe(expected.isEqual);
73
74
expect(result.length).toBe(expected.result.length);
75
});
76
77
test('Method pullAll should receive an array composed of mixed value and return the filtered array', () => {
78
const received = {
79
origin: [
80
0,
81
19980808,
82
'duanzhaoyang',
83
false,
84
null,
85
undefined,
86
Symbol('a'),
87
function () { },
88
{},
89
[],
90
],
91
selector: [false, undefined, 19980808, []],
92
};
93
const expected = {
94
isEqual: true,
95
result: [
96
0,
97
'duanzhaoyang',
98
null,
99
Symbol('a'),
100
function () { },
101
{},
102
[],
103
],
104
};
105
106
const result = pullAll<any>(received.origin, received.selector);
107
108
expect(result === received.origin).toBe(expected.isEqual);
109
110
expect(result.length).toBe(expected.result.length);
111
});
112
});
Copied!
最近更新 1yr ago
复制链接