mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-30 18:23:22 +03:00
6356555f46
* feat: add readonly type for toFilled * Apply suggestions from code review --------- Co-authored-by: Sojin Park <raon0211@gmail.com>
1.5 KiB
1.5 KiB
toFilled
Creates a new array filled with a specified value from the start position up to, but not including, the end position.
If the start or end indices are not provided, it defaults to filling the entire array.
Negative indices can also be used, in which case they are counted from the end of the array.
Interface
function toFilled<T, U>(arr: T[], value: U): Array<T | U>;
function toFilled<T, U>(arr: T[], value: U, start: number): Array<T | U>;
function toFilled<T, U>(arr: T[], value: U, start: number, end: number): Array<T | U>;
Parameters
arr
(T[]
): The array to base the new array on.value
(U
): The value to fill the new array with.start
(number, default = 0
): The start position. Defaults to 0.end
(number, default = array.length
): The end position. Defaults to the array's length.
Return Value
(Array<T | U>
): A new array with the specified values filled in.
Examples
const array = [1, 2, 3, 4, 5];
let result = toFilled(array, '*', 2);
console.log(result); // [1, 2, '*', '*', '*']
console.log(array); // [1, 2, 3, 4, 5]
result = toFilled(array, '*', 1, 4);
console.log(result); // [1, '*', '*', '*', 5]
console.log(array); // [1, 2, 3, 4, 5]
result = toFilled(array, '*');
console.log(result); // ['*', '*', '*', '*', '*']
console.log(array); // [1, 2, 3, 4, 5]
result = toFilled(array, '*', -4, -1);
console.log(result); // [1, '*', '*', '*', 5]
console.log(array); // [1, 2, 3, 4, 5]