take

说明

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

源码

https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/array/take

用法

take.test.ts
import take from "../../utility/array/take";
describe('take', () => {
test('Method `take()` should return the correct result', () => {
const received = [
{
arr: [1, 2, 3, 4, 5, 6, 7, 8],
amount: 5,
},
{
arr: [],
amount: 3,
},
{
arr: ['a', 'b', 'c', 'd', 'e'],
amount: 8,
},
{
arr: [[], [], [], [1, 2, 3]],
amount: 0,
},
];
const expected = [
[1, 2, 3, 4, 5],
[],
['a', 'b', 'c', 'd', 'e'],
[],
];
received.forEach((outerV, outerI) => {
const result = take<any>(outerV.arr, outerV.amount);
if (result.length > 0) {
result.forEach((innerV, innerI) => {
expect(innerV).toBe(expected[outerI][innerI]);
});
} else {
if (outerI % 2 !== 0) {
expect(result.length).toBe(expected[outerI].length);
}
}
});
});
});