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
2 changes: 1 addition & 1 deletion src/helpers/repeat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HandlebarsHelper, HelperConstructorBlock } from "./helper";

export const repeatHelper: HelperConstructorBlock = (ctx) => {

Check warning on line 3 in src/helpers/repeat.ts

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

'ctx' is defined but never used
return new HandlebarsHelper("repeat", function (rawNum, options) {
const num = Number.parseInt(rawNum);

Expand All @@ -9,7 +9,7 @@
}

let ret = "";
for (let i = 0; i < rawNum; i++) {
for (let i = 0; i < num; i++) {
ret += options.fn({ ...this, "repeat_index": i });
}
return ret;
Expand Down
15 changes: 15 additions & 0 deletions tests/helpers/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,21 @@ describe("Handlebars Helpers", () => {
expect(template({ count: -1 })).toEqual("");
});

// Regression test for #124. parseInt and the loop's own coercion
// disagree on trailing garbage: parseInt("3abc") is 3, but Number("3abc")
// is NaN, so comparing against the raw string silently produced nothing.
test("should use the parsed count when the input has trailing characters", () => {
const template = Handlebars.compile("{{#repeat count}}item{{/repeat}}");
expect(template({ count: "3abc" })).toEqual("itemitemitem");
});

// Same disagreement in the other direction: Number("3.7") is 3.7, so the
// raw comparison ran one iteration too many.
test("should use the parsed count when the input is fractional", () => {
const template = Handlebars.compile("{{#repeat count}}item{{/repeat}}");
expect(template({ count: "3.7" })).toEqual("itemitemitem");
});

test("should repeat exactly once for count of 1", () => {
const template = Handlebars.compile("{{#repeat 1}}only{{/repeat}}");
expect(template({})).toEqual("only");
Expand Down
Loading