dropRight

源码

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

说明

从源数组的右侧开始, 舍弃指定个数的值

用法

dropRight.ts
dropRight.test.ts
import { dropRight } from "./ddzy/utility/array/dropRight";
// ? 空数组
const s1: number[] = [];
const p1 = dropRight<number>(s1);
console.log(p1);
// ? 数字数组
const s2: number[] = [1, 2, 3, 4, 5];
const p2 = dropRight<number>(s2, 2);
console.log(p2);
// ? 不改变源数组
const s3: number[] = [1, 2, 3, 4, 5, 6];
const p3 = dropRight<number>(s3);
console.log(p3);
p3.push(100);
console.log(s3);
import { dropRight } from "../../utility/array/dropRight";
describe('dropRight tests...', () => {
test('dropRight should return an empty array when receive an empty array', () => {
const received: number[] = [];
const expected = [];
const result = dropRight<number>(received);
expect(result.length).toBe(expected.length);
});
test('dropRight should return the processed array correctly', () => {
const received: number[] = [1, 2, 3, 4, 5];
const expected = [1, 2, 3];
const result = dropRight<number>(received, 2);
result.forEach((v, i) => {
expect(v).toBe(expected[i]);
});
});
test('dropRight should not change the origin array', () => {
const received: number[] = [1, 2, 3, 4, 5, 6];
const result = dropRight(received);
result.push(100, 200);
expect(result.length).toBe(7);
expect(received.length).toBe(6);
});
});