take

说明

创建一个数组切片,从 arr 数组的起始元素开始提取 amount 个元素

源码

用法

take.test.ts
1
import take from "../../utility/array/take";
2
3
describe('take', () => {
4
test('Method `take()` should return the correct result', () => {
5
const received = [
6
{
7
arr: [1, 2, 3, 4, 5, 6, 7, 8],
8
amount: 5,
9
},
10
{
11
arr: [],
12
amount: 3,
13
},
14
{
15
arr: ['a', 'b', 'c', 'd', 'e'],
16
amount: 8,
17
},
18
{
19
arr: [[], [], [], [1, 2, 3]],
20
amount: 0,
21
},
22
];
23
const expected = [
24
[1, 2, 3, 4, 5],
25
[],
26
['a', 'b', 'c', 'd', 'e'],
27
[],
28
];
29
30
received.forEach((outerV, outerI) => {
31
const result = take<any>(outerV.arr, outerV.amount);
32
33
if (result.length > 0) {
34
result.forEach((innerV, innerI) => {
35
expect(innerV).toBe(expected[outerI][innerI]);
36
});
37
} else {
38
if (outerI % 2 !== 0) {
39
expect(result.length).toBe(expected[outerI].length);
40
}
41
}
42
});
43
});
44
});
Copied!
最近更新 1yr ago
复制链接