filled-array
Version:
Returns an array filled with the specified input
19 lines (14 loc) • 507 B
JavaScript
export default function filledArray(fillValue, count) {
if (!(Number.isSafeInteger(count) && count >= 0)) {
throw new TypeError(`Expected \`count\` to be a non-negative integer, got \`${count}\`.`);
}
const returnValue = Array.from({length: count});
const isFunction = typeof fillValue === 'function';
if (!isFunction) {
return returnValue.fill(fillValue);
}
for (let index = 0; index < count; index++) {
returnValue[index] = fillValue(index, count, returnValue);
}
return returnValue;
}