Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
chainable template tag for joining a bunch of strings over many lines
function append(separator) {
return typeof separator === "string" ? appender(separator, "") : appender("", "").apply(this, arguments);
}
function appender(separator, s) {
return function tag(literalParts, ...computedParts) {
s += literalParts[0];
for (let i = 1; i < literalParts.length; ++i) {
s += computedParts[i - 1] + literalParts[i];
}
return function(x) { return x == null ? s : appender(separator, s + separator).apply(this, arguments); };
}
}
var combined = append
`aaa`
`bbb`
`ccc`();
// "aaabbbccc"
var lines = append("\n")
`first line`
`second line`
`third line`();
// "first line\nsecond line\nthird line"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.