From 64c48787b9f78d31b6287ea35d7113bbbf00f428 Mon Sep 17 00:00:00 2001 From: Jamie Blair Date: Tue, 23 Jun 2026 18:06:39 +0100 Subject: [PATCH] feat: support the '&&' syntax --- src/safeXml.test.ts | 7 +++++++ src/safeXml.ts | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/safeXml.test.ts b/src/safeXml.test.ts index f93c86b..908bab7 100644 --- a/src/safeXml.test.ts +++ b/src/safeXml.test.ts @@ -36,4 +36,11 @@ describe("safeXml", () => { const output = safeXml`${["this", undefined, " is", null, " awesome!"]}`; expect(output).toEqual("this is awesome!"); }); + + test("valid with the common '&&' syntax", () => { + const truthyOutput = safeXml`${true && "this is awesome!"}`; + expect(truthyOutput).toEqual("this is awesome!"); + const falseyOutput = safeXml`${false && "this is awesome!"}`; + expect(falseyOutput).toEqual(""); + }); }); diff --git a/src/safeXml.ts b/src/safeXml.ts index f1c2b82..f2aa5f8 100644 --- a/src/safeXml.ts +++ b/src/safeXml.ts @@ -7,11 +7,11 @@ import { xml2js } from "xml-js"; */ export function safeXml( templateStrings: TemplateStringsArray, - ...templateValues: (undefined | null | string | number | (undefined | null | string | number)[])[] + ...templateValues: (undefined | null | false | string | number | (undefined | null | false | string | number)[])[] ) { let xml = ""; for (let i = 0; i < templateStrings.length; i++) { - if (templateValues[i] !== undefined && templateValues[i] !== null) { + if (templateValues[i] !== undefined && templateValues[i] !== null && templateValues[i] !== false) { const value = templateValues[i]; const xmlPart = Array.isArray(value) ? value.join("") : value; xml += `${templateStrings[i]!}${xmlPart}`;