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
7 changes: 7 additions & 0 deletions src/safeXml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ describe("safeXml", () => {
const output = safeXml`<name>${["this", undefined, " is", null, " awesome!"]}</name>`;
expect(output).toEqual("<name>this is awesome!</name>");
});

test("valid with the common '&&' syntax", () => {
const truthyOutput = safeXml`<name>${true && "this is awesome!"}</name>`;
expect(truthyOutput).toEqual("<name>this is awesome!</name>");
const falseyOutput = safeXml`<name>${false && "this is awesome!"}</name>`;
expect(falseyOutput).toEqual("<name></name>");
});
});
4 changes: 2 additions & 2 deletions src/safeXml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
Loading