Created
May 25, 2021 09:24
-
-
Save LucaColonnello/9c0d662747ccec8fed8caa955b47b750 to your computer and use it in GitHub Desktop.
Tiny template - using ES6 native tiny template implementation
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
const propSelector = (prop, ctx) => (typeof prop === 'function' ? prop(ctx) : (prop ?? '')); | |
const tinyTpl = (parts, ...props) => | |
(ctx) => parts.map((v, i) => v.concat(propSelector(props[i], ctx))).join(''); | |
const get = (prop) => (ctx) => ctx[prop]; | |
const template = tinyTpl`hello ${get('name')} ${'yay'}!` | |
console.log(template({ name: 'Luca' })) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment