amount-in-words
Version:
Convert amount in words
171 lines (170 loc) • 6.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AmountToWords = exports.CountryCodes = void 0;
var CountryCodes;
(function (CountryCodes) {
CountryCodes["IND"] = "IND";
CountryCodes["USA"] = "USA";
CountryCodes["GBR"] = "GBR";
})(CountryCodes = exports.CountryCodes || (exports.CountryCodes = {}));
var AmountToWords = /** @class */ (function () {
function AmountToWords() {
var _this = this;
this.first = [
"",
"One ",
"Two ",
"Three ",
"Four ",
"Five ",
"Six ",
"Seven ",
"Eight ",
"Nine ",
"Ten ",
"Eleven ",
"Twelve ",
"Thirteen ",
"Fourteen ",
"Fifteen ",
"Sixteen ",
"Seventeen ",
"Eighteen ",
"Nineteen ",
];
this.tens = [
"",
"",
"Twenty ",
"Thirty ",
"Forty ",
"Fifty ",
"Sixty ",
"Seventy ",
"Eighty ",
"Ninety ",
];
this.numSys = {
usNumSys: [
"",
"Hundred ",
"Thousand ",
"Million ",
"Billion ",
"Trillion ",
],
inNumSys: ["", "Hundred ", "Thousand ", "Lakh ", "Crore "],
};
this.curCodes = {
IND: ["Rupee", "Paisa", "Paise", "₹", "inNumSys"],
USA: ["Dollar", "Cent", "Cents", "$", "usNumSys"],
GBR: ["Pound", "Pence", "Pence", "£", "usNumSys"],
};
this.toWords = function (amount, countryCode) {
if (countryCode === void 0) { countryCode = "IND"; }
// console.log(num);
var numSys = _this.numSys[_this.getNumSys(countryCode)];
var nStr = amount.toString().split(".");
// Remove any other characters than numbers
var wholeStr = Number(nStr[0].replace(/[^a-z\d\s]+/gi, ""));
var decimalStr = nStr.length > 1 ? Number(nStr[1]) : 0;
var wholeStrPart = _this.getNumSys(countryCode) === "inNumSys"
? _this.convert(wholeStr, numSys).trim()
: _this.convertInUS(wholeStr, numSys).trim();
var decimalPart = _this.convert(decimalStr, numSys).trim();
var valueInStr = wholeStrPart.length > 0
? wholeStrPart + " " + _this.getCurrencyWhole(countryCode, wholeStr)
: "Zero " + _this.getCurrencyWhole(countryCode, wholeStr);
valueInStr =
decimalPart.length > 0
? valueInStr + " And " + decimalPart + " " + _this.getCurrencyChange(countryCode, decimalStr)
: valueInStr;
// console.log(valueInStr);
return valueInStr;
};
this.getCurrencyWhole = function (countryCode, amount) {
if (countryCode === void 0) { countryCode = "IND"; }
if (amount === void 0) { amount = 0; }
var cur = _this.curCodes[countryCode]
? _this.curCodes[countryCode][0]
: _this.curCodes["IND"][0];
if (amount > 1)
return cur + "s";
else
return cur;
};
this.getCurrencyChange = function (countryCode, amount) {
if (amount === void 0) { amount = 0; }
if (amount > 1)
return _this.curCodes[countryCode]
? _this.curCodes[countryCode][2]
: _this.curCodes["IND"][2];
else
return _this.curCodes[countryCode]
? _this.curCodes[countryCode][1]
: _this.curCodes["IND"][1];
};
this.getCurrencySymbol = function (countryCode) {
return _this.curCodes[countryCode]
? _this.curCodes[countryCode][2]
: _this.curCodes["IND"][2];
};
this.getNumSys = function (countryCode) {
return _this.curCodes[countryCode]
? _this.curCodes[countryCode][4]
: _this.curCodes["IND"][4];
};
this.convert = function (num, numSys) {
var numStr = num.toString().split("");
var finalStr = [];
while (numStr.length > 0) {
for (var i = 0; i < numSys.length - 1; ++i) {
if (i === 1)
finalStr.unshift(_this.getUnits(numStr.splice(-1), numSys, i));
else
finalStr.unshift(_this.getUnits(numStr.splice(-2), numSys, i));
}
if (numStr.length > 0)
finalStr.unshift.apply(finalStr, numSys.slice(-1));
}
return finalStr.join("");
};
this.convertInUS = function (num, numSys) {
var numStr = num.toString().split("");
var finalStr = [];
while (numStr.length > 0) {
// console.log(numSys.length);
for (var i = 0, l = numSys.length * 2 - 1; i < l; ++i) {
// console.log(numStr, i, (i/2)+1);
if (i === 0)
finalStr.unshift(_this.getUnits(numStr.splice(-2), numSys, 0));
else if (i % 2 === 1)
finalStr.unshift(_this.getUnits(numStr.splice(-1), numSys, 1));
else {
finalStr.unshift(_this.getUnits(numStr.splice(-2), numSys, i / 2 + 1));
}
}
if (numStr.length > 0)
finalStr.unshift.apply(finalStr, numSys.slice(-1));
else
break;
}
return finalStr.join("");
};
this.getUnits = function (lastTwo, numSys, place) {
var _a;
if (!lastTwo || lastTwo.length === 0)
return "";
var numInStr = "";
if (_this.first[Number(lastTwo.join(""))])
numInStr = _this.first[Number(lastTwo.join(""))];
else
numInStr = "" + _this.tens[Number(lastTwo.shift())] + _this.getUnits(lastTwo.slice(-1), numSys);
if (numInStr && place)
numInStr = "" + numInStr + ((_a = numSys[place]) !== null && _a !== void 0 ? _a : "");
return numInStr;
};
}
return AmountToWords;
}());
exports.AmountToWords = AmountToWords;