UNPKG

@adaptabletools/adaptable-cjs

Version:

Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

43 lines (42 loc) 1.23 kB
"use strict"; /** * Converts value to a number. * Drop-in replacement for lodash/toNumber. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.default = toNumber; const reTrimStart = /^\s+/; const reTrimEnd = /\s+$/; const reIsBinary = /^0b[01]+$/i; const reIsOctal = /^0o[0-7]+$/i; const reIsBadHex = /^[-+]0x[0-9a-f]+$/i; 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; } // Objects: try valueOf const other = typeof value.valueOf === 'function' ? value.valueOf() : value; return +other; }