digit-to-words-nepali
Version:
A comprehensive TypeScript library for converting numbers to words in English and Nepali languages. Supports numbers up to 10^39 (Adanta Singhar), currency formatting, decimal handling, and BigInt. Zero dependencies, fully tested.
133 lines (132 loc) • 3.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isValidNumber = isValidNumber;
exports.splitNumber = splitNumber;
exports.roundDecimal = roundDecimal;
exports.validateDecimal = validateDecimal;
exports.normalizeDecimal = normalizeDecimal;
exports.validateCustomMappings = validateCustomMappings;
exports.getScaleValue = getScaleValue;
const scaleMappings_1 = require("../mappings/scaleMappings");
function isValidNumber(num) {
try {
if (typeof num === 'bigint')
return num >= 0n;
if (typeof num === 'number') {
return !isNaN(num) && isFinite(num) && num >= 0;
}
const str = num.toString().trim();
if (!str)
return false;
// Handle special cases
if (str === 'NaN' || str === 'Infinity' || str === '-Infinity') {
return false;
}
// Handle decimal numbers
const [intPart, decPart] = str.split('.');
if (!intPart || intPart.startsWith('-') || !/^\d+$/.test(intPart)) {
return false;
}
if (decPart !== undefined && !/^\d+$/.test(decPart)) {
return false;
}
// Validate the size
try {
BigInt(intPart);
return true;
}
catch {
return false;
}
}
catch {
return false;
}
}
function splitNumber(num) {
if (typeof num === 'bigint')
return { integer: num };
const str = num.toString();
const [intPart, decPart] = str.split('.');
const integer = BigInt(intPart);
if (!decPart)
return { integer };
const decimalStr = decPart.padEnd(3, '0').slice(0, 3);
const decimalNum = Number(`0.${decimalStr}`);
const rounded = Math.round(decimalNum * 100);
if (rounded === 100) {
return { integer: integer + 1n };
}
return {
integer,
decimal: rounded.toString().padStart(2, '0')
};
}
function roundDecimal(decimal) {
const num = parseFloat(`0.${decimal}`);
const rounded = Math.round(num * 100);
return rounded.toString().padStart(2, '0');
}
function validateDecimal(decimal) {
if (!decimal)
return true;
const num = parseInt(decimal);
return (!isNaN(num) &&
num >= 0 &&
num <= 99 &&
decimal.length <= 2 &&
/^\d{1,2}$/.test(decimal));
}
function normalizeDecimal(decimal) {
if (!decimal)
return undefined;
const num = parseInt(decimal);
if (num === 0)
return undefined;
return num.toString().padStart(2, '0');
}
function isValidLanguageMapping(mapping) {
if (!mapping || typeof mapping !== 'object')
return false;
return ('en' in mapping &&
'ne' in mapping &&
typeof mapping.en === 'string' &&
typeof mapping.ne === 'string');
}
function isValidCustomUnits(units) {
if (!units || typeof units !== 'object')
return false;
return Object.entries(units).every(([key, value]) => {
const num = Number(key);
return (!isNaN(num) &&
num >= 0 &&
num <= 99 &&
isValidLanguageMapping(value));
});
}
function isValidCustomScales(scales) {
if (!scales || typeof scales !== 'object')
return false;
const validScales = Object.values(scaleMappings_1.SCALE_VALUES).map(Number);
return Object.entries(scales).every(([key, value]) => {
const num = Number(key);
return (validScales.includes(num) &&
isValidLanguageMapping(value));
});
}
function validateCustomMappings(config) {
try {
if (config.units && !isValidCustomUnits(config.units))
return false;
if (config.scales && !isValidCustomScales(config.scales))
return false;
return true;
}
catch {
return false;
}
}
function getScaleValue(scale) {
const num = typeof scale === 'string' ? parseInt(scale) : scale;
return BigInt(num);
}