diff --git a/src/handlebars.test.ts b/src/handlebars.test.ts new file mode 100644 index 0000000..41b00f5 --- /dev/null +++ b/src/handlebars.test.ts @@ -0,0 +1,31 @@ +import { readFile, mkdtemp, rm } from "fs/promises"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import Handlebars from "./handlebars"; + +describe.concurrent("handlebars", () => { + it("can use an or helper function", async (ctx) => { + const template = Handlebars.compile( + "{{#or KEY_ONE KEY_TWO KEY_THREE}}helpers: true{{else}}helpers: false{{/or}}", + ); + const result = template({ + KEY_ONE: "false", + KEY_TWO: "false", + KEY_THREE: "true", + }); + + expect(result).toEqual("helpers: true"); + }); + + it("can use an or helper inverse function", async (ctx) => { + const template = Handlebars.compile( + "{{#or KEY_ONE KEY_TWO KEY_THREE}}helpers: true{{else}}helpers: false{{/or}}", + ); + const result = template({ + KEY_ONE: undefined, + KEY_TWO: undefined, + KEY_THREE: undefined, + }); + + expect(result).toEqual("helpers: false"); + }); +}); diff --git a/src/handlebars.ts b/src/handlebars.ts index 2bbd3e0..f4d4395 100644 --- a/src/handlebars.ts +++ b/src/handlebars.ts @@ -1,11 +1,11 @@ import Handlebars from "handlebars"; import isObject from "lodash/isObject"; -import { urlToHttpOptions } from "url"; Handlebars.registerHelper("or", function (context, ...params) { const options = params[params.length - 1]; + params.pop(); - for (const value in params.splice(-1)) { + for (const value of params) { if (value) { return options.fn(context); } diff --git a/src/templates.test.ts b/src/templates.test.ts index 578b243..3b2df31 100644 --- a/src/templates.test.ts +++ b/src/templates.test.ts @@ -62,19 +62,4 @@ describe.concurrent("templates", () => { `{\n "keyOne": true,\n "keyTwo": "valueTwo",\n "keyThree": "valueThree"\n}\n`, ); }); - - it("can use handlebars-helpers functions", async (ctx) => { - await templateFiles({ - ...ctx.config, - templateVariables: { - KEY_ONE: "false", - KEY_TWO: "false", - KEY_THREE: "true", - }, - }); - const path = join(ctx.config.fullPath, "helpers.txt"); - const data = await readFile(path, "utf8"); - - expect(data).toEqual("helpers: true\n"); - }); }); diff --git a/test/fixtures/templates/helpers.txt b/test/fixtures/templates/helpers.txt deleted file mode 100644 index 841bdfc..0000000 --- a/test/fixtures/templates/helpers.txt +++ /dev/null @@ -1 +0,0 @@ -{{#or KEY_ONE KEY_TWO KEY_THREE}}helpers: true{{else}}helpers: false{{/or}}