Last active
January 24, 2017 09:13
-
-
Save electerious/a3ede10f166eef26a53e 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
html = (literalSections, ...substs) => { | |
// Use raw literal sections: We don’t want | |
// backslashes (\n etc.) to be interpreted | |
let raw = literalSections.raw, | |
let result = '' | |
substs.forEach((subst, i) => { | |
// Retrieve the literal section preceding | |
// the current substitution | |
let lit = raw[i] | |
// If the substitution is preceded by a dollar sign, | |
// we escape special characters in it | |
if (lit.slice(-1)==='$') { | |
subst = escapeHTML(subst) | |
lit = lit.slice(0, -1) | |
} | |
result += lit | |
result += subst | |
}) | |
// Take care of last literal section | |
// (Never fails, because an empty template string | |
// produces one literal section, an empty string) | |
result += raw[raw.length-1] | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requirements:
Example:
Output: