UNPKG

@codeshumon/number-to-words

Version:

Convert numbers to spoken words with accurate currency formatting for over 100 countries

1,249 lines (1,245 loc) 26.4 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { numberToCurrencyWords: () => numberToCurrencyWords, numberToWords: () => numberToWords }); module.exports = __toCommonJS(index_exports); // src/data/currenciesData.ts var CURRENCY_CONFIG = { US: { major: "Dollar", majorPlural: "Dollars", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, GB: { major: "Pound", majorPlural: "Pounds", minor: "Pence", minorPlural: "Pence", conjunction: "and", suffix: "Only" }, CA: { major: "Dollar", majorPlural: "Dollars", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, AU: { major: "Dollar", majorPlural: "Dollars", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, AE: { major: "Dirham", majorPlural: "Dirhams", minor: "Fils", minorPlural: "Fils", conjunction: "and", suffix: "Only" }, AF: { major: "Afghani", majorPlural: "Afghanis", minor: "Pul", minorPlural: "Puls", conjunction: "and", suffix: "Only" }, AL: { major: "Lek", majorPlural: "Leke", minor: "Qindark\xEB", minorPlural: "Qindarka", conjunction: "and", suffix: "Only" }, DZ: { major: "Dinar", majorPlural: "Dinars", minor: "Santeem", minorPlural: "Santeems", conjunction: "and", suffix: "Only" }, AO: { major: "Kwanza", majorPlural: "Kwanzas", minor: "C\xEAntimo", minorPlural: "C\xEAntimos", conjunction: "and", suffix: "Only" }, AR: { major: "Peso", majorPlural: "Pesos", minor: "Centavo", minorPlural: "Centavos", conjunction: "and", suffix: "Only" }, AM: { major: "Dram", majorPlural: "Drams", minor: "Luma", minorPlural: "Luma", conjunction: "and", suffix: "Only" }, AT: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, AZ: { major: "Manat", majorPlural: "Manats", minor: "Q\u0259pik", minorPlural: "Q\u0259piks", conjunction: "and", suffix: "Only" }, BH: { major: "Dinar", majorPlural: "Dinars", minor: "Fils", minorPlural: "Fils", conjunction: "and", suffix: "Only" }, BD: { major: "Taka", majorPlural: "Taka", minor: "Poisha", minorPlural: "Poisha", conjunction: "and", suffix: "Only" }, BY: { major: "Ruble", majorPlural: "Rubles", minor: "Kopeck", minorPlural: "Kopecks", conjunction: "and", suffix: "Only" }, BE: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, BJ: { major: "CFA Franc", majorPlural: "CFA Francs", minor: "Centime", minorPlural: "Centimes", conjunction: "and", suffix: "Only" }, BO: { major: "Boliviano", majorPlural: "Bolivianos", minor: "Centavo", minorPlural: "Centavos", conjunction: "and", suffix: "Only" }, BA: { major: "Mark", majorPlural: "Marks", minor: "Fening", minorPlural: "Fening", conjunction: "and", suffix: "Only" }, BR: { major: "Real", majorPlural: "Reais", minor: "Centavo", minorPlural: "Centavos", conjunction: "and", suffix: "Only" }, BG: { major: "Lev", majorPlural: "Leva", minor: "Stotinka", minorPlural: "Stotinki", conjunction: "and", suffix: "Only" }, BF: { major: "CFA Franc", majorPlural: "CFA Francs", minor: "Centime", minorPlural: "Centimes", conjunction: "and", suffix: "Only" }, KH: { major: "Riel", majorPlural: "Riels", minor: "Sen", minorPlural: "Sen", conjunction: "and", suffix: "Only" }, CM: { major: "CFA Franc", majorPlural: "CFA Francs", minor: "Centime", minorPlural: "Centimes", conjunction: "and", suffix: "Only" }, CL: { major: "Peso", majorPlural: "Pesos", minor: "Centavo", minorPlural: "Centavos", conjunction: "and", suffix: "Only" }, CN: { major: "Yuan", majorPlural: "Yuan", minor: "Fen", minorPlural: "Fen", conjunction: "and", suffix: "Only" }, CO: { major: "Peso", majorPlural: "Pesos", minor: "Centavo", minorPlural: "Centavos", conjunction: "and", suffix: "Only" }, CR: { major: "Col\xF3n", majorPlural: "Col\xF3nes", minor: "C\xE9ntimo", minorPlural: "C\xE9ntimos", conjunction: "and", suffix: "Only" }, HR: { major: "Kuna", majorPlural: "Kuna", minor: "Lipa", minorPlural: "Lipa", conjunction: "and", suffix: "Only" }, CU: { major: "Peso", majorPlural: "Pesos", minor: "Centavo", minorPlural: "Centavos", conjunction: "and", suffix: "Only" }, CZ: { major: "Koruna", majorPlural: "Koruny", minor: "Hal\xE9\u0159", minorPlural: "Hal\xE9\u0159e", conjunction: "and", suffix: "Only" }, CD: { major: "Franc", majorPlural: "Francs", minor: "Centime", minorPlural: "Centimes", conjunction: "and", suffix: "Only" }, DK: { major: "Krone", majorPlural: "Kroner", minor: "\xD8re", minorPlural: "\xD8re", conjunction: "and", suffix: "Only" }, EC: { major: "Dollar", majorPlural: "Dollars", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, EG: { major: "Pound", majorPlural: "Pounds", minor: "Piastre", minorPlural: "Piastres", conjunction: "and", suffix: "Only" }, SV: { major: "Dollar", majorPlural: "Dollars", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, EE: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, ET: { major: "Birr", majorPlural: "Birr", minor: "Santim", minorPlural: "Santim", conjunction: "and", suffix: "Only" }, FI: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, FR: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, GA: { major: "CFA Franc", majorPlural: "CFA Francs", minor: "Centime", minorPlural: "Centimes", conjunction: "and", suffix: "Only" }, GE: { major: "Lari", majorPlural: "Lari", minor: "Tetri", minorPlural: "Tetri", conjunction: "and", suffix: "Only" }, DE: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, GH: { major: "Cedi", majorPlural: "Cedis", minor: "Pesewa", minorPlural: "Pesewas", conjunction: "and", suffix: "Only" }, GR: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, GT: { major: "Quetzal", majorPlural: "Quetzales", minor: "Centavo", minorPlural: "Centavos", conjunction: "and", suffix: "Only" }, GN: { major: "Franc", majorPlural: "Francs", minor: "Centime", minorPlural: "Centimes", conjunction: "and", suffix: "Only" }, HT: { major: "Gourde", majorPlural: "Gourdes", minor: "Centime", minorPlural: "Centimes", conjunction: "and", suffix: "Only" }, HN: { major: "Lempira", majorPlural: "Lempiras", minor: "Centavo", minorPlural: "Centavos", conjunction: "and", suffix: "Only" }, HK: { major: "Dollar", majorPlural: "Dollars", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, HU: { major: "Forint", majorPlural: "Forints", minor: "Fill\xE9r", minorPlural: "Fill\xE9r", conjunction: "and", suffix: "Only" }, IN: { major: "Rupee", majorPlural: "Rupees", minor: "Paisa", minorPlural: "Paise", conjunction: "and", suffix: "Only" }, ID: { major: "Rupiah", majorPlural: "Rupiah", minor: "Sen", minorPlural: "Sen", conjunction: "and", suffix: "Only" }, IR: { major: "Rial", majorPlural: "Rials", minor: "Dinar", minorPlural: "Dinars", conjunction: "and", suffix: "Only" }, IQ: { major: "Dinar", majorPlural: "Dinars", minor: "Fils", minorPlural: "Fils", conjunction: "and", suffix: "Only" }, IE: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, IL: { major: "Shekel", majorPlural: "Shekels", minor: "Agora", minorPlural: "Agorot", conjunction: "and", suffix: "Only" }, IT: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, CI: { major: "CFA Franc", majorPlural: "CFA Francs", minor: "Centime", minorPlural: "Centimes", conjunction: "and", suffix: "Only" }, JP: { major: "Yen", majorPlural: "Yen", minor: "Sen", minorPlural: "Sen", conjunction: "and", suffix: "Only" }, JO: { major: "Dinar", majorPlural: "Dinars", minor: "Piastre", minorPlural: "Piastres", conjunction: "and", suffix: "Only" }, KZ: { major: "Tenge", majorPlural: "Tenge", minor: "Tiyn", minorPlural: "Tiyn", conjunction: "and", suffix: "Only" }, KE: { major: "Shilling", majorPlural: "Shillings", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, KW: { major: "Dinar", majorPlural: "Dinars", minor: "Fils", minorPlural: "Fils", conjunction: "and", suffix: "Only" }, KG: { major: "Som", majorPlural: "Soms", minor: "Tyiyn", minorPlural: "Tyiyn", conjunction: "and", suffix: "Only" }, LA: { major: "Kip", majorPlural: "Kips", minor: "Att", minorPlural: "Att", conjunction: "and", suffix: "Only" }, LV: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, LB: { major: "Pound", majorPlural: "Pounds", minor: "Piastre", minorPlural: "Piastres", conjunction: "and", suffix: "Only" }, LY: { major: "Dinar", majorPlural: "Dinars", minor: "Dirham", minorPlural: "Dirhams", conjunction: "and", suffix: "Only" }, LT: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, MG: { major: "Ariary", majorPlural: "Ariary", minor: "Iraimbilanja", minorPlural: "Iraimbilanja", conjunction: "and", suffix: "Only" }, MW: { major: "Kwacha", majorPlural: "Kwachas", minor: "Tambala", minorPlural: "Tambala", conjunction: "and", suffix: "Only" }, MY: { major: "Ringgit", majorPlural: "Ringgit", minor: "Sen", minorPlural: "Sen", conjunction: "and", suffix: "Only" }, ML: { major: "CFA Franc", majorPlural: "CFA Francs", minor: "Centime", minorPlural: "Centimes", conjunction: "and", suffix: "Only" }, MX: { major: "Peso", majorPlural: "Pesos", minor: "Centavo", minorPlural: "Centavos", conjunction: "and", suffix: "Only" }, MD: { major: "Leu", majorPlural: "Lei", minor: "Ban", minorPlural: "Bani", conjunction: "and", suffix: "Only" }, MA: { major: "Dirham", majorPlural: "Dirhams", minor: "Centime", minorPlural: "Centimes", conjunction: "and", suffix: "Only" }, MZ: { major: "Metical", majorPlural: "Meticais", minor: "Centavo", minorPlural: "Centavos", conjunction: "and", suffix: "Only" }, MM: { major: "Kyat", majorPlural: "Kyats", minor: "Pya", minorPlural: "Pya", conjunction: "and", suffix: "Only" }, NA: { major: "Dollar", majorPlural: "Dollars", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, NP: { major: "Rupee", majorPlural: "Rupees", minor: "Paisa", minorPlural: "Paise", conjunction: "and", suffix: "Only" }, NL: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, NZ: { major: "Dollar", majorPlural: "Dollars", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, NI: { major: "C\xF3rdoba", majorPlural: "C\xF3rdobas", minor: "Centavo", minorPlural: "Centavos", conjunction: "and", suffix: "Only" }, NE: { major: "CFA Franc", majorPlural: "CFA Francs", minor: "Centime", minorPlural: "Centimes", conjunction: "and", suffix: "Only" }, NG: { major: "Naira", majorPlural: "Naira", minor: "Kobo", minorPlural: "Kobo", conjunction: "and", suffix: "Only" }, MK: { major: "Denar", majorPlural: "Denari", minor: "Deni", minorPlural: "Deni", conjunction: "and", suffix: "Only" }, NO: { major: "Krone", majorPlural: "Kroner", minor: "\xD8re", minorPlural: "\xD8re", conjunction: "and", suffix: "Only" }, OM: { major: "Rial", majorPlural: "Rials", minor: "Baisa", minorPlural: "Baisa", conjunction: "and", suffix: "Only" }, PK: { major: "Rupee", majorPlural: "Rupees", minor: "Paisa", minorPlural: "Paise", conjunction: "and", suffix: "Only" }, PS: { major: "Shekel", majorPlural: "Shekels", minor: "Agora", minorPlural: "Agorot", conjunction: "and", suffix: "Only" }, PA: { major: "Balboa", majorPlural: "Balboas", minor: "Cent\xE9simo", minorPlural: "Cent\xE9simos", conjunction: "and", suffix: "Only" }, PY: { major: "Guaran\xED", majorPlural: "Guaran\xEDes", minor: "C\xE9ntimo", minorPlural: "C\xE9ntimos", conjunction: "and", suffix: "Only" }, PE: { major: "Sol", majorPlural: "Soles", minor: "C\xE9ntimo", minorPlural: "C\xE9ntimos", conjunction: "and", suffix: "Only" }, PH: { major: "Peso", majorPlural: "Pesos", minor: "Centavo", minorPlural: "Centavos", conjunction: "and", suffix: "Only" }, PL: { major: "Z\u0142oty", majorPlural: "Z\u0142ote", minor: "Grosz", minorPlural: "Grosze", conjunction: "and", suffix: "Only" }, PT: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, QA: { major: "Riyal", majorPlural: "Riyals", minor: "Dirham", minorPlural: "Dirhams", conjunction: "and", suffix: "Only" }, RO: { major: "Leu", majorPlural: "Lei", minor: "Ban", minorPlural: "Bani", conjunction: "and", suffix: "Only" }, RU: { major: "Ruble", majorPlural: "Rubles", minor: "Kopeck", minorPlural: "Kopecks", conjunction: "and", suffix: "Only" }, RW: { major: "Franc", majorPlural: "Francs", minor: "Centime", minorPlural: "Centimes", conjunction: "and", suffix: "Only" }, SA: { major: "Riyal", majorPlural: "Riyals", minor: "Halala", minorPlural: "Halalas", conjunction: "and", suffix: "Only" }, SN: { major: "CFA Franc", majorPlural: "CFA Francs", minor: "Centime", minorPlural: "Centimes", conjunction: "and", suffix: "Only" }, RS: { major: "Dinar", majorPlural: "Dinars", minor: "Para", minorPlural: "Para", conjunction: "and", suffix: "Only" }, SL: { major: "Leone", majorPlural: "Leones", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, SG: { major: "Dollar", majorPlural: "Dollars", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, SK: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, SI: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, ZA: { major: "Rand", majorPlural: "Rand", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, KR: { major: "Won", majorPlural: "Won", minor: "Jeon", minorPlural: "Jeon", conjunction: "and", suffix: "Only" }, SS: { major: "Pound", majorPlural: "Pounds", minor: "Piastre", minorPlural: "Piastres", conjunction: "and", suffix: "Only" }, ES: { major: "Euro", majorPlural: "Euros", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, LK: { major: "Rupee", majorPlural: "Rupees", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, SD: { major: "Pound", majorPlural: "Pounds", minor: "Piastre", minorPlural: "Piastres", conjunction: "and", suffix: "Only" }, SE: { major: "Krona", majorPlural: "Kronor", minor: "\xD6re", minorPlural: "\xD6re", conjunction: "and", suffix: "Only" }, CH: { major: "Franc", majorPlural: "Francs", minor: "Rappen", minorPlural: "Rappen", conjunction: "and", suffix: "Only" }, SY: { major: "Pound", majorPlural: "Pounds", minor: "Piastre", minorPlural: "Piastres", conjunction: "and", suffix: "Only" }, TJ: { major: "Somoni", majorPlural: "Somoni", minor: "Diram", minorPlural: "Dirams", conjunction: "and", suffix: "Only" }, TW: { major: "Dollar", majorPlural: "Dollars", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, TZ: { major: "Shilling", majorPlural: "Shillings", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, TH: { major: "Baht", majorPlural: "Baht", minor: "Satang", minorPlural: "Satang", conjunction: "and", suffix: "Only" }, TN: { major: "Dinar", majorPlural: "Dinars", minor: "Millime", minorPlural: "Millimes", conjunction: "and", suffix: "Only" }, TR: { major: "Lira", majorPlural: "Liras", minor: "Kuru\u015F", minorPlural: "Kuru\u015F", conjunction: "and", suffix: "Only" }, TM: { major: "Manat", majorPlural: "Manats", minor: "Tennesi", minorPlural: "Tennesi", conjunction: "and", suffix: "Only" }, UG: { major: "Shilling", majorPlural: "Shillings", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" }, UA: { major: "Hryvnia", majorPlural: "Hryvnias", minor: "Kopiyka", minorPlural: "Kopiyky", conjunction: "and", suffix: "Only" }, UY: { major: "Peso", majorPlural: "Pesos", minor: "Cent\xE9simo", minorPlural: "Cent\xE9simos", conjunction: "and", suffix: "Only" }, UZ: { major: "Som", majorPlural: "Som", minor: "Tiyin", minorPlural: "Tiyin", conjunction: "and", suffix: "Only" }, VE: { major: "Bol\xEDvar", majorPlural: "Bol\xEDvares", minor: "C\xE9ntimo", minorPlural: "C\xE9ntimos", conjunction: "and", suffix: "Only" }, VN: { major: "Dong", majorPlural: "Dong", minor: "H\xE0o", minorPlural: "H\xE0o", conjunction: "and", suffix: "Only" }, YE: { major: "Rial", majorPlural: "Rials", minor: "Fils", minorPlural: "Fils", conjunction: "and", suffix: "Only" }, ZM: { major: "Kwacha", majorPlural: "Kwachas", minor: "Ngwee", minorPlural: "Ngwee", conjunction: "and", suffix: "Only" }, ZW: { major: "Dollar", majorPlural: "Dollars", minor: "Cent", minorPlural: "Cents", conjunction: "and", suffix: "Only" } }; // src/converter.ts var MAX_LIMIT = 999999999999999; var numberToWords = (num, options) => { const parsedNum = typeof num === "string" ? parseFloat(num) : num; const { noComa = false, isAnd = false, noHypen = false, titleCase = false } = options || {}; if (isNaN(parsedNum)) { return "Zero"; } if (Math.abs(parsedNum) > MAX_LIMIT) { return "The number exceeds the maximum supported limit of 999,999,999,999,999."; } const isNegative = parsedNum < 0; const absoluteNum = Math.abs(parsedNum); const [integerPart, originalDecimalPart] = absoluteNum.toFixed(2).split("."); const wholeNumber = parseInt(integerPart, 10); if (wholeNumber === 0 && (!originalDecimalPart || parseInt(originalDecimalPart, 10) === 0)) { return "Zero"; } const units = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]; const teens = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]; const tens = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]; const scales = ["", "Thousand", "Million", "Billion", "Trillion"]; const chunkToWords = (n) => { if (n === 0) return ""; let words2 = ""; if (n >= 100) { words2 += units[Math.floor(n / 100)] + " Hundred"; n %= 100; if (n > 0) words2 += " "; } if (n >= 20) { words2 += tens[Math.floor(n / 10)]; n %= 10; if (n > 0) words2 += noHypen ? "" : "-"; } else if (n >= 10) { words2 += teens[n - 10]; n = 0; } if (n > 0) { if (words2 !== "") { words2 += noHypen ? " " + units[n] : units[n].toLowerCase(); } else { words2 = units[n]; } } return words2; }; let words = ""; let scaleIndex = 0; let remaining = wholeNumber; const parts = []; while (remaining > 0) { const chunk = remaining % 1e3; if (chunk !== 0) { let chunkWords = chunkToWords(chunk); if (scaleIndex > 0) { chunkWords += " " + scales[scaleIndex]; } parts.unshift(chunkWords); } remaining = Math.floor(remaining / 1e3); scaleIndex++; } if (parts.length > 0) { words = parts[0]; for (let i = 1; i < parts.length; i++) { if (isAnd) { words += " and " + parts[i]; } else if (!noComa) { words += ", " + parts[i]; } else { words += " " + parts[i]; } } } const decimalPart = originalDecimalPart; if (decimalPart && parseInt(decimalPart, 10) > 0) { words += (words ? " " : "") + "Point"; for (const digitChar of decimalPart) { words += " " + units[parseInt(digitChar, 10)]; } } let finalWords = words || "Zero"; finalWords = isNegative ? `Negative ${finalWords}` : finalWords; if (titleCase || noHypen) { finalWords = finalWords.replace(/(\w+)-(\w+)/g, (match, p1, p2) => { return `${p1}-${p2.charAt(0).toUpperCase() + p2.slice(1)}`; }); } finalWords = finalWords.replace(/\bAnd\b/g, "and"); return finalWords.trim(); }; var numberToCurrencyWords = (amount, countryCode = "US", options) => { const num = typeof amount === "string" ? parseFloat(amount) : amount; const { noComa = false, isAnd = false, noHypen = false, titleCase = false } = options || {}; if (isNaN(num)) { const config2 = CURRENCY_CONFIG[countryCode] || CURRENCY_CONFIG.US; const cleanSuffix2 = config2.suffix.replace(/\s*only$/i, "").trim(); return `Zero ${config2.majorPlural} ${cleanSuffix2} only`; } if (Math.abs(num) > MAX_LIMIT) { return "The Amount exceeds the maximum supported limit of 999,999,999,999,999."; } const formattedNum = parseFloat(num.toFixed(2)); const config = CURRENCY_CONFIG[countryCode] || CURRENCY_CONFIG.US; let cleanSuffix = config.suffix.replace(/\s*only$/i, "").trim(); const absoluteNum = Math.abs(formattedNum); const major = Math.floor(absoluteNum); const minor = Math.round((absoluteNum - major) * 100); const majorWord = major === 1 ? config.major : config.majorPlural; let result = `${numberToWords(major, { noComa, isAnd, noHypen, titleCase })} ${majorWord}`; if (minor > 0) { const minorWord = minor === 1 ? config.minor : config.minorPlural; result += ` ${config.conjunction.toLowerCase()} ${numberToWords(minor, { noComa, isAnd, noHypen, titleCase })} ${minorWord}`; } const sign = formattedNum < 0 ? "Negative " : ""; let finalString = `${sign}${result} ${cleanSuffix} only`.replace(/\s+/g, " ").trim(); if (titleCase || noHypen) { finalString = finalString.replace(/(\w+)-(\w+)/g, (match, p1, p2) => { return `${p1}-${p2.charAt(0).toUpperCase() + p2.slice(1)}`; }); } finalString = finalString.replace(/\bAnd\b/g, "and"); return finalString; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { numberToCurrencyWords, numberToWords });