@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
135 lines (134 loc) • 4.17 kB
JavaScript
export function abbreviateNumber(numberToAbbreviate) {
let str = '';
if (numberToAbbreviate >= 1000000000) {
str = (numberToAbbreviate / 1000000000).toFixed(1) + 'B';
}
else if (numberToAbbreviate >= 1000000) {
str = (numberToAbbreviate / 1000000).toFixed(1) + 'M';
}
else if (numberToAbbreviate >= 1000) {
str = (numberToAbbreviate / 1000).toFixed(1) + 'K';
}
else {
str = numberToAbbreviate.toString();
}
return str;
}
export function wrapInParentheses(numberToWrap) {
return ' (' + numberToWrap + ')';
}
export function timesBy100(value) {
if (isNaN(value) || !value) {
return value;
}
const valueAsStr = value.toPrecision();
const separator = Number(1.1).toLocaleString()[1];
const indexOfSeparator = valueAsStr.indexOf(separator);
if (indexOfSeparator === -1) {
return value * 100;
}
const [integer, decimal] = valueAsStr.split(separator);
const num = [...`${integer}${decimal ?? ''}00`];
num.splice(indexOfSeparator + 2, 0, separator);
return Number(num.join(''));
}
export function divideBy100(value) {
if (isNaN(value) || !value) {
return value;
}
const isNegative = value < 0;
if (isNegative) {
value = -value;
}
const valueAsStr = value.toPrecision();
const separator = Number(1.1).toLocaleString()[1];
let indexOfSeparator = valueAsStr.indexOf(separator);
if (indexOfSeparator === -1) {
indexOfSeparator = 0;
}
let [integer = '', decimal] = valueAsStr.split(separator);
decimal = decimal || '';
integer = integer.padStart(2, '0');
const integerArr = [...integer];
const num = integerArr.slice(0, integerArr.length - 2).join('') +
separator +
integerArr.slice(-2).join('') +
decimal;
return Number(num) * (isNegative ? -1 : 1);
}
export function avoidJavascriptPrecisionIssues(value) {
let newValue = value;
if (value) {
newValue = parseFloat(newValue.toFixed(12));
}
return newValue;
}
export function clamp(value, lower, upper) {
return Math.min(Math.max(value, lower), upper);
}
export function roundNumber(numberToRound, decimalPlaces) {
switch (decimalPlaces) {
case 1:
return Math.round(numberToRound * 10) / 10;
case 2:
return Math.round(numberToRound * 100) / 100;
case 3:
return Math.round(numberToRound * 1000) / 1000;
case 4:
return Math.round(numberToRound * 10000) / 10000;
case 5:
return Math.round(numberToRound * 100000) / 100000;
case 6:
return Math.round(numberToRound * 1000000) / 1000000;
}
}
const _reTrimStart = /^\s+/;
const _reTrimEnd = /\s+$/;
const _reIsBinary = /^0b[01]+$/i;
const _reIsOctal = /^0o[0-7]+$/i;
const _reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
export function toNumber(value) {
if (typeof value === 'number') {
return value;
}
if (typeof value === 'symbol') {
return NaN;
}
if (typeof value === 'boolean') {
return value ? 1 : 0;
}
if (value == null) {
return value === undefined ? NaN : 0;
}
if (typeof value === 'string') {
const trimmed = value.replace(_reTrimStart, '').replace(_reTrimEnd, '');
if (_reIsBinary.test(trimmed)) {
return globalThis.parseInt(trimmed.slice(2), 2);
}
if (_reIsOctal.test(trimmed)) {
return globalThis.parseInt(trimmed.slice(2), 8);
}
if (_reIsBadHex.test(trimmed)) {
return NaN;
}
return +trimmed;
}
const other = typeof value.valueOf === 'function' ? value.valueOf() : value;
return +other;
}
export function parseIntFn(string, radix) {
const str = typeof string === 'string' ? string.trim() : String(string);
return globalThis.parseInt(str, radix || 10);
}
export const NumberExtensions = {
abbreviateNumber,
wrapInParentheses,
timesBy100,
divideBy100,
avoidJavascriptPrecisionIssues,
clamp,
roundNumber,
toNumber,
parseIntFn,
};
export default NumberExtensions;