32 lines
1.2 KiB
JavaScript
32 lines
1.2 KiB
JavaScript
"use strict";
|
|
|
|
exports.__esModule = true;
|
|
exports.NumberLiteralHelper = void 0;
|
|
/**
|
|
* @license
|
|
* Copyright (c) 2024 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; |