All files / style9/src utils.js

100% Statements 54/54
100% Branches 26/26
100% Functions 20/20
100% Lines 45/45

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 1358x 8x 8x     101x     8x     327x       219x   211x 58x 57x     209x   208x       8x     193x       108x 104x       86x   86x 5x                 5x         11x 9x 7x       13x   11x 17x     11x       10x 13x         10x 10x 10x 10x     8x               48x 16x     32x       5x 5x 2x     3x         250x       210x       196x     8x                        
const { UNITLESS_NUMBERS, SHORTHAND_EXPANSIONS } = require('./constants');
const hash = require('murmurhash-js');
const cssProperties = require('known-css-properties').all;
 
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(':');
}
 
module.exports = {
  expandProperty,
  getClass,
  getDeclaration,
  getKeyframes,
  normalizePseudoElements,
  minifyProperty,
  isNestedStyles,
  normalizeValue,
  isAtRule,
  isPseudoSelector,
};