Last active
June 7, 2018 14:04
-
-
Save InsulaVentus/9cac4a0ff60cc2e338d340faa3b042dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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