fill

源码

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

说明

使用 value 值来填充(替换) array. 从 start 位置开始, 到 end 位置结束(但不包含 end 位置).

用法

fill.ts
fill.test.ts
fill.ts
import { fill } from "./ddzy/utility/array/fill";
// ? 空数组
const s1: number[] = [];
const p1 = fill<number>(s1, 100);
console.log(p1);
console.log('---------------------');
// ? 非空数组
const s2: number[] = [1, 2, 3, 4, 5];
const p2 = fill<number>(s2, 200, 2, 8);
console.log(p2);
console.log('-----------------------');
// ? 原地操作
const s3: any[] = [false, 0, '', NaN, true, function () { }];
const p3 = fill<any>(s3, 'duanzhaoyang', 0);
console.log(p3);
console.log(p3 === s3);
fill.test.ts
import { fill } from "../../utility/array/fill";
describe('fill tests...', () => {
test('method fill should return an empty array when receive an empty array', () => {
const received: number[] = [];
const expected = 0;
const result = fill<number>(received, 100);
expect(result.length).toBe(expected);
});
test('method fill should return the correct array when receive an array composed of number', () => {
const received: number[] = [1, 2, 3, 4, 5];
const expected = [1, 2, 200, 200, 200, 200, 200, 200];
const result = fill<number>(received, 200, 2);
result.forEach((v, i) => {
expect(v).toBe(expected[i]);
});
});
test('method fill will modify the origin array', () => {
const received: any[] = [false, 0, '', NaN, true, function () { }];
const expected = received;
const result = fill(received, 'duanzhaoyang');
expect(result).toBe(expected);
expect(result).toBe(received);
});
});