All files / style9/src/helpers get-style-object-value.js

100% Statements 20/20
100% Branches 6/6
100% Functions 7/7
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 448x 8x 8x 8x     130x   130x 142x 142x     130x       130x 142x   142x 28x     114x 132x 129x   114x             108x       83x     8x  
const { expandProperty } = require('inline-style-expand-shorthand');
const { evaluateNodePath } = require('../utils/ast');
const { mapObjectValues } = require('../utils/helpers');
const { isNestedStyles, normalizeValue } = require('../utils/styles');
 
function mapObject(object, cb) {
  const expanded = {};
 
  for (const key in object) {
    const value = object[key];
    Object.assign(expanded, cb([key, value]));
  }
 
  return expanded;
}
 
function expandStyleProperties(styles) {
  return mapObject(styles, ([key, value]) => {
    const current = { [key]: value };
 
    if (isNestedStyles(value)) {
      return expandProperties(current);
    }
 
    const expandedProps = Object.entries(expandProperty(key, value) || current)
      .filter(([prop]) => !(prop in styles && prop !== key))
      .map(([prop, propValue]) => [prop, normalizeValue(prop, propValue)]);
 
    return Object.fromEntries(expandedProps);
  });
}
 
// Recursively expands shorthand properties
// Alternating levels of objects are treated as having CSS properties
function expandProperties(styleContainer) {
  return mapObjectValues(styleContainer, expandStyleProperties);
}
 
function getStyleObjectValue(nodePath) {
  return expandProperties(evaluateNodePath(nodePath));
}
 
module.exports = getStyleObjectValue;