Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
Created May 30, 2016 14:29
Show Gist options
  • Save michaelficarra/70ce798feb25fdc91508f387190053a1 to your computer and use it in GitHub Desktop.
Save michaelficarra/70ce798feb25fdc91508f387190053a1 to your computer and use it in GitHub Desktop.
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