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        57x   57x 6381x 6381x 6381x 6381x 15x 6366x 563x   6381x   57x   813x 813x 1x 812x 1x 811x 236x 575x 1x   813x   57x   814x 814x 814x   814x    
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;
};