hyperformula
Version:
HyperFormula is a JavaScript engine for efficient processing of spreadsheet-like data and formulas
32 lines (31 loc) • 1.23 kB
JavaScript
;
exports.__esModule = true;
exports.NumberLiteralHelper = void 0;
/**
* @license
* Copyright (c) 2025 Handsoncode. All rights reserved.
*/
class NumberLiteralHelper {
constructor(config) {
this.config = config;
const thousandSeparator = this.config.thousandSeparator === '.' ? `\\${this.config.thousandSeparator}` : this.config.thousandSeparator;
const decimalSeparator = this.config.decimalSeparator === '.' ? `\\${this.config.decimalSeparator}` : this.config.decimalSeparator;
this.numberPattern = new RegExp(`^([+-]?((${decimalSeparator}\\d+)|(\\d+(${thousandSeparator}\\d{3,})*(${decimalSeparator}\\d*)?)))(e[+-]?\\d+)?$`);
this.allThousandSeparatorsRegex = new RegExp(`${thousandSeparator}`, 'g');
}
numericStringToMaybeNumber(input) {
if (this.numberPattern.test(input)) {
const num = this.numericStringToNumber(input);
if (isNaN(num)) {
return undefined;
}
return num;
}
return undefined;
}
numericStringToNumber(input) {
const normalized = input.replace(this.allThousandSeparatorsRegex, '').replace(this.config.decimalSeparator, '.');
return Number(normalized);
}
}
exports.NumberLiteralHelper = NumberLiteralHelper;