currency-in-words
Version:
A light-weight, fast and efficient lib that converts currency or any numbers to corresponding words
167 lines • 6.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertInd = exports.handleTenCrore = exports.handleCrore = exports.handleTenLakh = exports.handleLakh = exports.handleTenThousands = exports.handleThousands = exports.handleHundreds = exports.indianFormat = void 0;
var common_1 = require("./common");
var lang_1 = __importDefault(require("../lang"));
var placeholders_1 = require("../lang/placeholders");
var globalConfig_1 = require("../lib/globalConfig");
var lang = (0, globalConfig_1.getGlobalConfig)().lang;
var _a = lang_1.default[lang], singleDigit = _a.singleDigit, twoDigit = _a.twoDigit;
var _b = (0, placeholders_1.getIndianPlaceValues)(), hundred = _b.hundred, thousand = _b.thousand, lakh = _b.lakh, crore = _b.crore;
var indianFormat = {
0: '',
1: common_1.handleOnes,
2: common_1.handleTens,
3: handleHundreds,
4: handleThousands,
5: handleTenThousands,
6: handleLakh,
7: handleTenLakh,
8: handleCrore,
9: handleTenCrore,
10: handleArabs
};
exports.indianFormat = indianFormat;
/**
* handles all three-digit values
*
* @param value the value to be translated to words
* @returns translated value
*/
function handleHundreds(value) {
if ((0, common_1.isFirstCharZero)(value))
return "".concat((0, common_1.handleTens)(value.substring(1)));
return "".concat(singleDigit[+value[0]], " ").concat(hundred, " ").concat((0, common_1.handleTens)(value.substring(1)));
}
exports.handleHundreds = handleHundreds;
/**
* handles all four-digit values
*
* @param value the value to be translated to words
* @returns translated value
*/
function handleThousands(value) {
if ((0, common_1.isFirstCharZero)(value))
return "".concat(handleHundreds(value.substring(1)));
if ((0, common_1.isSecondCharZero)(value))
return "".concat(singleDigit[+value[0]], " ").concat(thousand, " ").concat(handleHundreds(value.substring(1)));
return "".concat(singleDigit[+value[0]], " ").concat(thousand, " ").concat(handleHundreds(value.substring(1)));
}
exports.handleThousands = handleThousands;
/**
* handles all five-digit values
*
* @param value the value to be translated to words
* @returns translated value
*/
function handleTenThousands(value) {
if ((0, common_1.isFirstCharZero)(value))
return "".concat(handleThousands(value.substring(1)));
if ((0, common_1.isSecondCharZero)(value))
return "".concat(twoDigit[+value[0]], " ").concat(thousand, " ").concat(handleHundreds(value.substring(2)));
return "".concat((0, common_1.handleTens)(value.substring(0, 2)), " ").concat(thousand, " ").concat(handleHundreds(value.slice(-3)));
}
exports.handleTenThousands = handleTenThousands;
/**
* handles all six-digit values
*
* @param value the value to be translated to words
* @returns translated value
*/
function handleLakh(value) {
if ((0, common_1.isFirstCharZero)(value))
return "".concat(handleTenThousands(value.substring(1)));
return "".concat(singleDigit[+value[0]], " ").concat(lakh, " ").concat(handleTenThousands(value.substring(1)));
}
exports.handleLakh = handleLakh;
/**
* handles all seven-digit values
*
* @param value the value to be translated to words
* @returns translated value
*/
function handleTenLakh(value) {
if ((0, common_1.isFirstCharZero)(value))
return "".concat(handleLakh(value.substring(1)));
if ((0, common_1.isSecondCharZero)(value))
return "".concat(twoDigit[+value[0]], " ").concat(lakh, " ").concat(handleTenThousands(value.substring(2)));
return "".concat((0, common_1.handleTens)(value.substring(0, 2)), " ").concat(lakh, " ").concat(handleTenThousands(value.substring(2)));
}
exports.handleTenLakh = handleTenLakh;
/**
* handles all eight-digit values
*
* @param value the value to be translated to words
* @returns translated value
*/
function handleCrore(value) {
if ((0, common_1.isFirstCharZero)(value))
return "".concat(handleTenLakh(value.substring(1)));
return "".concat(singleDigit[+value[0]], " ").concat(crore, " ").concat(handleTenLakh(value.substring(1)));
}
exports.handleCrore = handleCrore;
/**
* handles all nine-digit values
*
* @param value the value to be translated to words
* @returns translated value
*/
function handleTenCrore(value) {
if ((0, common_1.isSecondCharZero)(value))
return "".concat(twoDigit[+value[0]], " ").concat(crore, " ").concat(handleTenLakh(value.substring(2)));
return "".concat((0, common_1.handleTens)(value.substring(0, 2)), " ").concat(crore, " ").concat(handleTenLakh(value.substring(2)));
}
exports.handleTenCrore = handleTenCrore;
/**
* handles everything greater than 9 digits
*
* @param value the value to be translated to words
* @returns translated value
*/
function handleArabs(value) {
return convertInd(value).trim();
}
/**
* Converts numeric string to corresponding word string in Indian-format
*
* @param integer integer part of the value
* @param fraction fractional part of the value
* @param twoDecimalPlaces fraction corrected to two decimal places
* @returns expect an empty or translated string
*/
function convertInd(integer, fraction, twoDecimalPlaces) {
var result = "";
/**
* In indian system, values of length b/w 10-15(value > 9) are usually a repetation.
* eg:
* Length of nine is, "ten crore"
* Length of ten is, "one hundred crore"
* Length of eleven is, "one thousand crore"
* ...
* Length of 15 is, "one crore crore"
*
* The best way to handle this is to short-circuit and reuse the same functions which handles the values of the length 0-9
*/
if (integer.length > 9) {
result =
"".concat(indianFormat[10](integer.substring(0, integer.length - 7)), " ").concat(crore, " ").concat(indianFormat[7](integer.substring(integer.length - 7)));
return result.trim();
}
// values of length 0-9
result = indianFormat[integer.length](integer);
/**
* Fractional parts are corrected to two places
* ignores fractional parts from .01 to .09
* ie, fraction > .10 to .99 is handled, the ones before that is rejected, since it's negligible
*
* However, these constraints can be cleverly handled. Refer documentation for further clarification.
*/
if (fraction && twoDecimalPlaces && +fraction[0] !== 0) // ignore .01 - .09
result = "".concat(result, ".").concat(indianFormat[twoDecimalPlaces.length](twoDecimalPlaces)); // limit to two decimal places.`
return result.trim();
}
exports.convertInd = convertInd;
//# sourceMappingURL=indian-system.js.map