Created
May 30, 2016 14:29
-
-
Save michaelficarra/70ce798feb25fdc91508f387190053a1 to your computer and use it in GitHub Desktop.
chainable template tag for joining a bunch of strings over many lines
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
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); }; | |
} | |
} |
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
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