yini-parser
Version:
Readable configuration without YAML foot-guns or JSON noise. The official Node.js parser for YINI config format — An INI-inspired configuration format with clear nesting, explicit types, and predictable parsing.
84 lines • 3.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const number_1 = require("../utils/number");
const print_1 = require("../utils/print");
/**
* @property {string | undefined} [tag]
* This parameter is for debugging only. Its
* contents may change at any time and should not
* be relied upon for any significant purpose.
*/
const parseNumberLiteral = (txt) => {
(0, print_1.debugPrint)('-> Entered parseNumberLiteral(..), txt: ' + txt);
if (/^[+-]?(?:\d+\.\d*|\d*\.?\d+)e[+-]?\d+$/i.test(txt)) {
// Exp. numbers
(0, print_1.debugPrint)('* Identified as an exp number');
return {
tag: 'From exp number, Number-Float',
// value: parseInt(txt.replace('#', '0x'), 16),
value: parseFloat(txt),
};
}
// --- Hexadecimal ---------
if (/^[+-]?(0[xX]|#)/.test(txt)) {
// Prefix: 0x, 0X, #
(0, print_1.debugPrint)('* Identified as a hex number');
(0, print_1.debugPrint)('parsed out HEX: ' + txt.replace(/0[xX]|#/, ''));
return {
tag: 'From hex number, Number-Integer',
// value: parseInt(txt.replace('#', '0x'), 16),
value: parseInt(txt.replace(/0[xX]|#/, ''), 16),
};
}
// --- Binary ---------
if (/^[+-]?(0[bB]|%)/.test(txt)) {
// Prefix: 0b, 0B, %
(0, print_1.debugPrint)('* Identified as a bin number');
(0, print_1.debugPrint)('parsed out BIN: ' + txt.replace(/0[bB]|%/, ''));
return {
tag: 'From bin number, Number-Integer',
value: parseInt(txt.replace(/0[bB]|%/, ''), 2),
};
}
// --- Octal ---------
if (/^[+-]?0[oO]/.test(txt)) {
// Prefix: 0o, 0O
(0, print_1.debugPrint)('* Identified as a oct number');
(0, print_1.debugPrint)('parsed out OCT: ' + txt.replace(/0[oO]/, ''));
return {
tag: 'From oct number, Number-Integer',
value: parseInt(txt.replace(/0[oO]/, ''), 8),
};
}
// --- Duodecimal ---------
if (/^[+-]?0[zZ]/.test(txt)) {
// Prefix: 0z, 0Z, x = A = 10, e = B = 11.
(0, print_1.debugPrint)('* Identified as a duodecimal number');
(0, print_1.debugPrint)('parsed out DOZ: ' + txt.replace(/0[zZ]/, ''));
txt = txt.replace(/[xX]/g, 'A');
txt = txt.replace(/[eE]/g, 'B');
(0, print_1.debugPrint)('Converter to AB form: ' + txt.replace(/0[zZ]/, ''));
return {
tag: 'From doz (duodecimal) number, Number-Integer',
value: parseInt(txt.replace(/0[zZ]/, ''), 12),
};
}
// In a regex literal the dot must be escaped (\.) to match a literal '.'
if (/\./.test(txt)) {
(0, print_1.debugPrint)('* Identified as a float number');
return {
tag: 'From float number, Number-Float',
value: parseFloat(txt),
};
}
// TODO: Depending, on mode, below continue or break on error
//console.error('Error: Failed to parse number value: ' + txt)
if (!(0, number_1.isValidJSNumber)(txt)) {
(0, print_1.debugPrint)('* Identified as invalid number');
return { tag: 'From invalid number/value', value: undefined };
}
(0, print_1.debugPrint)('* Identified as a int number');
return { tag: 'From int number, Number-Integer', value: parseInt(txt) };
};
exports.default = parseNumberLiteral;
//# sourceMappingURL=parseNumber.js.map