Skip to content

Commit

Permalink
add length checks and only send non empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Apr 5, 2024
1 parent e1a6412 commit 7defefa
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ const htmlGenerator = function* (literals, ...expressions) {
const isRaw =
literal.length > 0 && literal.charCodeAt(literal.length - 1) === 33;

yield isRaw ? literal.slice(0, -1) : literal;
if (literal.length) {
yield isRaw ? literal.slice(0, -1) : literal;
}

for (const value of expressions[index]) {
expression = typeof value === "string" ? value : `${value ?? ""}`;

yield isRaw ? value : value.replace(escapeRegExp, escapeFunction);
}

++index;

continue;
}

Expand All @@ -94,12 +97,18 @@ const htmlGenerator = function* (literals, ...expressions) {
expression = expression.replace(escapeRegExp, escapeFunction);
}

yield literal + expression;
literal += expression;

if (literal.length) {
yield literal;
}

++index;
}

yield literals.raw[index];
if (literals.raw[index].length) {
yield literals.raw[index];
}
};

export { html, htmlGenerator };

0 comments on commit 7defefa

Please # to comment.