Skip to content

Commit 0192f34

Browse files
webstrandthetarnav
authored andcommitted
fix: repeat does not use fallback on initial length 0
1 parent b3d3815 commit 0192f34

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

packages/range/src/repeat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function repeat<T>(
2424
options: { fallback?: Accessor<T> } = {},
2525
): Accessor<T[]> {
2626
let prev: readonly T[] = [];
27-
let prevLen = 0;
27+
let prevLen: number | undefined;
2828
const disposers: (() => void)[] = [];
2929
onCleanup(() => {
3030
for (let index = 0; index < disposers.length; index++) {
@@ -75,7 +75,7 @@ export function repeat<T>(
7575
const next = prev.slice(0, len);
7676

7777
// Create new elements as needed
78-
for (let index = prevLen; index < len; index++) {
78+
for (let index = prevLen ?? 0; index < len; index++) {
7979
next[index] = createRoot(dispose => {
8080
disposers[index] = dispose;
8181
return mapFn(index);

packages/range/test/repeat.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ describe("repeat", () => {
8282
setLength(3);
8383
expect(mapped(), "mapped after dispose").toEqual(["fb"]);
8484
}));
85+
86+
it("uses fallback when length is initially 0", () =>
87+
createRoot(disposer => {
88+
const map = repeat(
89+
() => 0,
90+
i => i,
91+
{ fallback: () => NaN },
92+
);
93+
expect(map()).toEqual([NaN]);
94+
disposer();
95+
}));
8596
});
8697

8798
describe("<Repeat/>", () => {

0 commit comments

Comments
 (0)