compact

说明

过滤指定数组中的假值(0''falsenullundefinedNaN)

源码

用法

compact.test.ts
1
import { compact } from "../../utility/array/compact";
2
3
describe('compact', () => {
4
test('compact should return an empty array when receive an empty array', () => {
5
const received: any[] = [];
6
const expected = 0;
7
8
const result = compact<any>(received);
9
10
expect(result.length).toBe(0);
11
});
12
13
test('compact should return the array only contains truthy value', () => {
14
const received: any[] = [true, false, 0, '', undefined, NaN, null, 22];
15
const expected = [true, 22];
16
17
const result = compact<any>(received);
18
19
for (const [i, v] of result.entries()) {
20
expect(v).toBe(expected[i]);
21
}
22
});
23
24
test('compact should return a new array', () => {
25
const received: number[] = [0, 1, 2, 3, 4];
26
const expected: number[] = [1, 2, 3, 4];
27
28
const result = compact<number>(received);
29
30
received.push(5);
31
32
for (const [i, v] of result.entries()) {
33
expect(v).toBe(expected[i]);
34
}
35
});
36
})
Copied!
最近更新 1yr ago
复制链接