Advertisement
boky8

Virtual DOM

Oct 21st, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const el = (tag, attrs, body) => `${renderTag(tag, attrs)}${renderBody(body)}${renderClosingTag(tag)}`;
  2. const renderTag = (tag, attrs) => `<${tag}${renderAttrs(attrs)}>`;
  3. const renderAttrs = attrs => `${hasAttrs(attrs) ? ' ' : ''}${joinAttrs(attrs)}`;
  4. const hasAttrs = attrs => Object.keys(attrs || {}).length > 0;
  5. const joinAttrs = attrs => Object.entries(attrs).map(attr => attrToString(attr)).join(' ');
  6. const attrToString = ([key, value]) => `${key}="${value}"`;
  7. const renderBody = body => body.join('');
  8. const renderClosingTag = tag => isClosingTag ? `</${tag}>` : '';
  9. const isClosingTag = tag => !tagsWithoutClosing.includes(tag);
  10. const tagsWithoutClosing = ['input'];
  11.  
  12. const text = txt => txt.toString();
  13. const div = (attrs, ...body) => el('div', attrs, body);
  14. const span = (attrs, ...body) => el('span', attrs, body);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement