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

100% Statements 18/18
100% Branches 4/4
100% Functions 7/7
100% Lines 18/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 45 46 478x     8x         8x     116x   116x 127x 127x     116x       116x 127x 26x     101x 119x 116x   101x             98x       73x     8x  
const { evaluateNodePath } = require('./utils/ast');
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;