Created
July 15, 2015 12:38
-
-
Save davidnormo/6767256c798b6adb3b3d to your computer and use it in GitHub Desktop.
es6-tagged-template-strings.js
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
let asAttrs = (strings, value) => { | |
let attrs = []; | |
for (let prop in value) { | |
let val = value[prop]; | |
if (typeof val === 'boolean'){ | |
attrs.push(`${prop}` + (val ? '' : '=false')); | |
} else { | |
attrs.push(`${prop}="${val}"`); | |
} | |
} | |
return attrs.join(' '); | |
} | |
let props = { | |
'data-one': 'one', | |
b: true, | |
c: false | |
}; | |
let tag = 'div'; | |
let attrs = `<${tag} ` + asAttrs`${props}` + `>Hello world!</${tag}>`; | |
console.log(attrs); // '<div data-one="one" b c=false>Hello world!</div>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment