All files / utils styles.js

14.55% Statements 8/55
7.14% Branches 2/28
4.76% Functions 1/21
17.39% Lines 8/46

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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 1402x 2x 2x           2x                                       2x                                                                                                                 2x                                                                           1x     2x                          
const cssProperties = require('known-css-properties').all;
const hash = require('murmurhash-js');
const { UNITLESS_NUMBERS, SHORTHAND_EXPANSIONS } = require('./constants');
 
function expandProperty(prop) {
  return SHORTHAND_EXPANSIONS[prop] || [prop];
}
 
const BASE_FONT_SIZE_PX = 16;
 
function isCustomProperty(name) {
  return name.startsWith('--');
}
 
function normalizeValue(prop, value) {
  if (isCustomProperty(prop)) return value;
 
  if (typeof value === 'number') {
    if (prop === 'fontSize') return `${value / BASE_FONT_SIZE_PX}rem`;
    if (!UNITLESS_NUMBERS.includes(prop)) return `${value}px`;
  }
 
  if (Array.isArray(value)) return value.slice().join(' ');
 
  return value;
}
 
// Class can't start with number
const CLASS_PREFIX = 'c';
 
function getClass(...args) {
  return CLASS_PREFIX + hash(JSON.stringify(args)).toString(36);
}
 
function camelToHyphen(string) {
  if (isCustomProperty(string)) return string;
  return string.replace(/[A-Z]/g, c => `-${c.toLowerCase()}`);
}
 
function getDeclaration({ name, value, atRules, pseudoSelectors }) {
  const cls = getClass({ name, value, atRules, pseudoSelectors });
 
  return (
    atRules.map(rule => rule + '{').join('') +
    '.' +
    cls +
    pseudoSelectors.join('') +
    '{' +
    camelToHyphen(name) +
    ':' +
    normalizeValue(name, value) +
    '}' +
    atRules.map(() => '}').join('')
  );
}
 
function normalizeTime(time) {
  if (time === 'from') return '0%';
  if (time === 'to') return '100%';
  return time;
}
 
function stringifyKeyframe(time, frame) {
  if (!Object.keys(frame).length) return '';
 
  const props = Object.entries(frame).map(([key, value]) => {
    return `${camelToHyphen(key)}:${normalizeValue(key, value)}`;
  });
 
  return `${normalizeTime(time)}{${props.join(';')}}`;
}
 
function stringifyKeyframes(rules) {
  return Object.entries(rules)
    .map(([time, frame]) => stringifyKeyframe(time, frame))
    .join('');
}
 
function getKeyframes(rules) {
  const rulesString = stringifyKeyframes(rules);
  const name = getClass(rulesString);
  const declaration = `@keyframes ${name}{${rulesString}}`;
  return { name, declaration };
}
 
const LEGACY_PSEUDO_ELEMENTS = [
  ':before',
  ':after',
  ':first-letter',
  ':first-line'
];
 
function normalizePseudoElements(string) {
  if (LEGACY_PSEUDO_ELEMENTS.includes(string)) {
    return ':' + string;
  }
 
  return string;
}
 
function minifyProperty(name) {
  const hyphenName = camelToHyphen(name);
  if (cssProperties.includes(hyphenName)) {
    return cssProperties.indexOf(hyphenName).toString(36);
  }
 
  return hash(hyphenName).toString(36);
}
 
// Values can be primitives or arrays, nested styles are plain objects
function isNestedStyles(item) {
  return typeof item === 'object' && !Array.isArray(item);
}
 
function isAtRule(string) {
  return string.startsWith('@');
}
 
function isPseudoSelector(string) {
  return string.startsWith(':');
}
 
function isAtRuleObject(name) {
  return name === '@media' || name === '@supports';
}
 
module.exports = {
  expandProperty,
  getClass,
  getDeclaration,
  getKeyframes,
  normalizePseudoElements,
  minifyProperty,
  isNestedStyles,
  normalizeValue,
  isAtRule,
  isPseudoSelector,
  isAtRuleObject
};