// Formatter for number representations import round from './math'; // get a fixed number of digits const fixedDigits = (number, numberDigits) => { const str = `${number}`; let padding = ''; for (let i = 0; i < numberDigits; i += 1) { padding += '0'; } return padding.substring(0, padding.length - str.length) + str; }; const needsSpacing = (val1, val2) => { let toBeSpaced = 0; // Add additional spacing when one value has decimal places and the other not if ((val1 < 1 && val2 >= 1) || (val1 >= 1 && val2 < 1)) { toBeSpaced = 1; } return toBeSpaced; }; // - 0 <= x < 1: 1 decimal place // - 1 <= x <= 100.000: 0 decimal places const getPrecision = (val) => { if (val < 1) { return 10; } return 1; }; // - 0 <= x < 1: 0 spaces // - 1 <= x <= 100.000: 2 spaces const getSpacing = (val) => { if (val >= 1) { return 2; } return 0; }; // - 0 <= x < 1: 1 decimal place // - 1 <= x <= 100.000: 0 decimal places const getFormattedValue = (val) => { const digits = Math.log(val) / Math.log(10); let formattedValue; if (digits < 0) { formattedValue = Math.max(round(val, 1), 0.1); } else { formattedValue = round(val, 0); } return formattedValue; }; const haveSameRepresentation = (value1, value2) => { const p1 = getFormattedValue(value1); const p2 = getFormattedValue(value2); return p1 === p2 || (p1 < 0.1 && p2 < 0.1); }; export { fixedDigits, needsSpacing, getPrecision, getSpacing, getFormattedValue, haveSameRepresentation };