All files / postcss attr-to-template.js

100% Statements 16/16
75% Branches 3/4
100% Functions 2/2
100% Lines 16/16
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 262x 2x 2x   2x   2x   2x 5x 5x   9x 5x 5x 5x 5x 5x       5x     2x  
const valueParser = require('postcss-value-parser');
const _ = require('lodash/fp');
const postfixAttrValue = require('./postfix-attr-value');
 
const isAttrFunction = _.matches({ type: 'function', value: 'attr' });
 
const extractWordValues = _.flow([_.filter({ type: 'word' }), _.map('value')]);
 
const attrToTemplate = value => {
  const attributes = [];
  const template = valueParser(value)
    .walk(node => {
      if (isAttrFunction(node)) {
        const [name, type, defaultValue] = extractWordValues(node.nodes);
        const attrValue = `{ ${ name } ${ defaultValue ? `= "${ defaultValue }"` : '' }}`;
        node.type = 'word';
        node.value = postfixAttrValue(attrValue, type);
        attributes.push(name);
      }
    })
    .toString();
  return { template, attributes };
};
 
module.exports = attrToTemplate;