Created
July 10, 2018 20:18
-
-
Save shadowcat-mst/293e3f5a7b4bd45c6c141fa3e80b87ce to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// intentionally incomplete dummy escape function because meh | |
function escapeXML (str) { | |
return str.toString().replace(/"/g, '"') | |
} | |
function qxml ([ firstString, ...remainingStrings ], ...interpolations) { | |
return remainingStrings.map( | |
(v, idx) => [ v, interpolations[idx] ] | |
).reduce( | |
(acc, [ trail, interp ]) => ( | |
acc | |
+ (interp instanceof Object | |
? Object.entries(interp) | |
.map(([ k, v ]) => `${k}="${escapeXML(v)}"`).join(" ") | |
: escapeXML(interp)) | |
+ trail | |
), | |
firstString | |
); | |
} | |
let bar = "bar & grill" | |
let attrs = { quux: 1, fleem: 'Hello "Bob"' }; | |
console.log(qxml`<tag ${attrs}>${bar}</tag>`); | |
// logs: <tag quux="1" fleem="Hello "Bob"">bar & grill</tag> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
YAY
renders: (newlines added for readability in the gist)