VDL Plugin SDK

toFilled

Import from @varavel/vdl-plugin-sdk/utils/arrays.

Utility functions re-exported from es-toolkit (MIT License). See https://github.com/toss/es-toolkit for more details.

Call Signature

function toFilled<T, U>(arr, value): (T | U)[];

Creates a new array filled with the specified value from the start position up to, but not including, the end position. This function does not mutate the original array.

Type Parameters

Type ParameterDescription
TThe type of elements in the original array.
UThe type of the value to fill the new array with.

Parameters

ParameterTypeDescription
arrreadonly T[]The array to base the new array on.
valueUThe value to fill the new array with.

Returns

(T | U)[]

The new array with the filled values.

Example

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]

Call Signature

function toFilled<T, U>(
   arr, 
   value, 
   start?
): (T | U)[];

Creates a new array filled with the specified value from the start position up to, but not including, the end position. This function does not mutate the original array.

Type Parameters

Type ParameterDescription
TThe type of elements in the original array.
UThe type of the value to fill the new array with.

Parameters

ParameterTypeDescription
arrreadonly T[]The array to base the new array on.
valueUThe value to fill the new array with.
start?numberThe start position. Defaults to 0.

Returns

(T | U)[]

The new array with the filled values.

Example

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]

Call Signature

function toFilled<T, U>(
   arr, 
   value, 
   start?, 
   end?
): (T | U)[];

Creates a new array filled with the specified value from the start position up to, but not including, the end position. This function does not mutate the original array.

Type Parameters

Type ParameterDescription
TThe type of elements in the original array.
UThe type of the value to fill the new array with.

Parameters

ParameterTypeDescription
arrreadonly T[]The array to base the new array on.
valueUThe value to fill the new array with.
start?numberThe start position. Defaults to 0.
end?numberThe end position. Defaults to the array's length.

Returns

(T | U)[]

The new array with the filled values.

Example

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]