Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/fill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Fills elements of `array` with value from `start` up to, but not including, `end`.
* @group Array
* @since 1.0.0
* @param array The array to fill.
* @param value The value to fill `array` with.
* @param [start=0] The start position.
* @param [end=array.length] The end position.
* @example
* fill([1, 2, 3], 0); // => [0, 0, 0]
* @example
* fill([1, 2, 3], 0, 1); // => [1, 0, 0]
* @example
* fill([1, 2, 3], 0, 1, 2); // => [1, 0, 3]
*/
// eslint-disable-next-line max-params
export default function fill<Value>(array: Value[], value: Value, start?: number, end?: number): Value[] {
return array.fill(value, start, end);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export { default as drop } from "./drop.js";
export { default as dropRight } from "./drop-right.js";
export { default as endsWith } from "./ends-with.js";
export { default as eq } from "./eq.js";
export { default as fill } from "./fill.js";
export { default as first, type FirstT } from "./first.js";
export { default as get, type CalculatedPropertyT, type CalculatedPathT, type ValueAtT } from "./get.js";
export { default as head, type HeadT } from "./head.js";
Expand Down
9 changes: 9 additions & 0 deletions tests/fill.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { fill } from "@extremejs/utils";

it("should fill elements of array with value from start up to, but not including, end.", () => {
expect(fill([1, 2, 3], 0)).toEqual([0, 0, 0]);

expect(fill([1, 2, 3], 0, 1)).toEqual([1, 0, 0]);

expect(fill([1, 2, 3], 0, 1, 2)).toEqual([1, 0, 3]);
});