ninetails
10/31/2016 - 7:45 PM

ES6 XT.js

ES6 XT.js

export defaul function xt(tmpl, p = document.createDocumentFragment()) {
  if (!tmpl.length) {
    return p;
  }

  const t = tmpl.slice();
  const o = t.shift();

  if (o instanceof Array) {
    p.appendChild(xt(o, document.createElement(o.shift())));
  } else if (typeof o === 'object') {
    Object.keys(o).forEach(k => p.setAttribute(k, o[k]));
  } else {
    p.appendChild(document.createTextNode(o));
  }

  return xt(t, p);
}
console.log(
  xt([
    ['div', {'data-attr1': 23},
      ['a', {href: 'http://example.com'}, 'Example text',
        ['span', ' (additional span)']
      ]
    ],
    ['span', 'another']
  ])
);