Skip to content

Instantly share code, notes, and snippets.

@InsulaVentus
Last active June 7, 2018 14:04
Show Gist options
  • Save InsulaVentus/9cac4a0ff60cc2e338d340faa3b042dc to your computer and use it in GitHub Desktop.
Save InsulaVentus/9cac4a0ff60cc2e338d340faa3b042dc to your computer and use it in GitHub Desktop.
import escape from "escape-html";
export const escapeExpression = (strings: TemplateStringsArray, ...unsafeExpressions: string[]) => {
const escaped: string[] = [];
let unsafeExpressionIndex = 0;
let templateStringIndex = 0;
for (let i = 0; i < strings.length + unsafeExpressions.length; i++) {
if (i % 2 === 0) {
escaped.push(strings[templateStringIndex]);
templateStringIndex++;
} else {
escaped.push(escape(unsafeExpressions[unsafeExpressionIndex]));
unsafeExpressionIndex++;
}
}
return escaped.join("");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment