All files / utils/styledHelpers/utils scaleFont.js

100% Statements 26/26
100% Branches 19/19
100% Functions 3/3
100% Lines 26/26

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        56x   56x 1882x 1882x 1882x 1882x 15x 1867x 550x   1882x   56x   633x 633x 1x 632x 1x 631x 69x 562x 1x   633x   56x   634x 634x 634x   634x    
import { getGlobalStyles } from 'src/global-styles';
 
const {
  fontSize: fontSizeSchema,
} = getGlobalStyles();
 
export const scaleFactor = props => {
  const { sizeVariants: { sm: schemaSm, lg: schemaLg } } = fontSizeSchema;
  let value = null;
  const { sm, lg } = props;
  if (lg) {
    value = schemaLg;
  } else if (sm) {
    value = schemaSm;
  }
  return value;
};
const getUnit = (size, defaultUnit) => {
  // Get the unit of measurement
  let unit = defaultUnit;
  if (size.toString().indexOf('%') !== -1) {
    unit = '%';
  } else if (size.toString().indexOf('px') !== -1) {
    unit = 'px';
  } else if (size.toString().indexOf('rem') !== -1) {
    unit = 'rem';
  } else if (size.toString().indexOf('em') !== -1) {
    unit = 'em';
  }
  return unit;
};
export const scaleFont = (size, factor = 1, defaultSize = 1, defaultUnit = 'rem') => {
  // Convert the font size and scale factor to floats for calculation
  const floatSize = parseFloat(size) || defaultSize;
  const floatScaleFactor = parseFloat(factor);
  const unit = size ? getUnit(size, defaultUnit) : defaultUnit;
  // Calculate the final font size
  return (floatSize * floatScaleFactor).toString() + unit;
};