All files / lib/generator index.js

100% Statements 24/24
100% Branches 14/14
100% Functions 10/10
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 601x                                     1x   4x 28x     27x 44x 21x     4x 37x   8x   29x   4x   27x   1x         4x 46x   14x   33x   13x       4x    
const voidTags = [
  'area',
  'base',
  'br',
  'col',
  'command',
  'embed',
  'hr',
  'img',
  'input',
  'keygen',
  'link',
  'meta',
  'param',
  'source',
  'track',
  'wbr',
];
 
module.exports = (data, hooks) => {
 
  const tagHTML = ([tag, attributes, content]) => (
    voidTags.includes(tag) ? `<${attributes}/>` : `<${attributes}>${innerHTML(content || '')}</${tag}>`
  );
 
  const tagCompile = (tag, data) => [tag, [tag].concat(Object.keys(data)
    .filter(key => key !== 'innerHTML' && data[key])
    .map(key => `${key}="${data[key]}"`))
    .join(' '), data.innerHTML];
 
  const tagType = (tag, data) => {
    switch (Object.prototype.toString.call(hooks[tag])) {
      case '[object Function]':
        return innerHTML(hooks[tag](data));
      default:
        switch (Object.prototype.toString.call(data)) {
          case '[object Array]':
            return data.map(item => tagType(tag, item)).join('\n');
          case '[object Object]':
            return tagHTML(tagCompile(tag, data));
          default:
            return tagHTML([tag, tag, data]);
        }
    }
  };
 
  const innerHTML = (data) => {
    switch (Object.prototype.toString.call(data)) {
      case '[object Array]':
        return data.map(item => innerHTML(item)).join('\n');
      case '[object Object]':
        return Object.keys(data).map(tag => tagType(tag, data[tag])).join('\n');
      default:
        return data;
    }
  };
 
  return innerHTML(data);
};