dropRightWhile

说明

创建一个切片数组, 去除源数组中从特定的callback返回假值开始到尾部的部分

源码

用法

dropRightWhile.test.ts
1
import { dropRightWhile } from "../../utility/array/dropRightWhile";
2
3
describe('dropRightWhile tests...', () => {
4
5
test('dropRightWhile should return an empty array when receve an empty array', () => {
6
const received: number[] = [];
7
const expected = 0;
8
9
const result = dropRightWhile<number>(received, function (v) {
10
return !!v;
11
});
12
13
expect(result.length).toBe(expected);
14
});
15
16
test('dropRightWhile should return the filtered array when receive an array being composed of number', () => {
17
const received: number[] = [23, 34, -1, -5, 54, 22, 0];
18
const expected: number[] = [23, 34];
19
20
const result = dropRightWhile(received, function (v) {
21
return v < 0;
22
})
23
24
for (const [i, v] of result.entries()) {
25
expect(v).toBe(expected[i]);
26
}
27
});
28
29
test('dropRightWhile should return the filtered array when receive an array being composed of object', () => {
30
interface IReceivedParams {
31
name: string;
32
age: number;
33
};
34
const received: IReceivedParams[] = [
35
{ name: 'duan', age: 20 },
36
{ name: 'zhao', age: 30 },
37
{ name: 'yang', age: 40 },
38
];
39
const expected = [received[0]];
40
41
42
const result = dropRightWhile<IReceivedParams>(received, function (v) {
43
return v.age === 30;
44
});
45
46
for (const [i, v] of result.entries()) {
47
expect(v).toBe(expected[i]);
48
}
49
});
50
51
});
Copied!
最近更新 1yr ago
复制链接