All files / postcss append-attr.js

100% Statements 8/8
100% Branches 1/1
100% Functions 2/2
100% Lines 8/8
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 391x 1x                                 1x 2x 2x 2x     2x 2x                          
const _ = require('lodash/fp');
const attrToTemplate = require('./attr-to-template');
 
/**
 * @typedef {Object} Attr Components' CSS declarations which contain the attr() function representation
 * @property {string} prop The declaration's CSS property
 * @property {string} selector The CSS selector of the declaration's rule
 * @property {Template} template The declaration with attr() replaced to template placeholders
 * @property {string} attributes Attributes referenced in the declaration with the attr() function
 */
 
/**
 * @param {Object<Object>} components
 * @param {string} component
 * @param {string} rule
 * @param {Declaration} decl
 * @returns {Attr[]}
 */
module.exports = function appendAttr(components, component, rule, decl) {
  const { prop, value } = decl;
  decl.remove();
  return _.update(
    [component, 'attrs'],
    (attrs = []) => {
      const { template, attributes } = attrToTemplate(value);
      return [
        ...attrs,
        {
          prop: _.camelCase(prop),
          selector: rule.selector,
          template,
          attributes,
        },
      ];
    },
    components
  );
};