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

27.78% Statements 5/18
0% Branches 0/4
14.29% Functions 1/7
27.78% Lines 5/18

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 458x 8x         8x                                                                   1x     8x  
const evaluateNodePath = require('../helpers/eval-node-path');
const { mapObjectValues } = require('../utils/helpers');
const {
  expandProperty,
  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]) => {
    if (isNestedStyles(value)) {
      return expandProperties({ [key]: value });
    }
 
    const expandedProps = expandProperty(key)
      .filter(prop => !(prop in styles && prop !== key))
      .map(prop => [prop, normalizeValue(prop, value)]);
 
    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;