All files index.js

100% Statements 17/17
100% Branches 4/4
100% Functions 3/3
100% Lines 16/16
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      1x   5x   1x 6x 6x 6x 6x 1x   5x 5x 53x 3x   50x 50x 50x   5x        
import _ from 'lodash';
import widthsMap from './widthsMap';
 
const settingsDefaults = { font: 'Arial', size: 100 };
 
const toAscii = string => _.deburr(string);
 
const getWidth = (string, settings) => {
  const sett = { ...settingsDefaults, ...settings };
  const font = sett.font.toLowerCase();
  const size = sett.size;
  if (font !== 'arial') {
    throw new Error('The only supported string is Arial only at this time.');
  }
  let totalWidth = 0;
  toAscii(string).split('').forEach((char) => {
    if (/[\x00-\x1F]/.test(char)) { // non-printable character
      return true;
    }
    const width = widthsMap[font][char];
    totalWidth += width;
    return true;
  });
  return totalWidth * (size / 100);
};
 
export default getWidth;