Skip to content

Instantly share code, notes, and snippets.

@LucaColonnello
Created May 25, 2021 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucaColonnello/9c0d662747ccec8fed8caa955b47b750 to your computer and use it in GitHub Desktop.
Save LucaColonnello/9c0d662747ccec8fed8caa955b47b750 to your computer and use it in GitHub Desktop.
Tiny template - using ES6 native tiny template implementation
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