currency-in-words
Version:
A light-weight, fast and efficient lib that converts currency or any numbers to corresponding words
86 lines • 3.69 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertIntl = exports.handleHundreds = exports.resolveValue = exports.spliceLastThree = void 0;
var common_1 = require("./common");
var lang_1 = __importDefault(require("../lang"));
var globalConfig_1 = require("../lib/globalConfig");
var lang = (0, globalConfig_1.getGlobalConfig)().lang;
var singleDigit = lang_1.default[lang].singleDigit;
/**
* splice the last three characters of the given array
* CAUTION: this function is meant to modify the passed in value; not a pure fn().
*
* @param value value to be sliced
* @returns the same length of the input if the passed in array is of length less than three
* @returns an empty string if the passed in array is empty
* @returns a string of the length three
*/
function spliceLastThree(value) {
return value.splice(-3).join('');
}
exports.spliceLastThree = spliceLastThree;
/**
* handles all values of international-system of length greater than 3
*
* @param value the value to be translated to words
* @param placeholder placeholder values, eg: million, trillion etc
* @returns <empty string> for invalid input
* @returns `translated-value placeholder`, eg: one hundred trillion
*/
function resolveValue(value, placeholder) {
if (!parseInt(value))
return '';
return "".concat(handleHundreds(value), " ").concat(placeholder, " ");
}
exports.resolveValue = resolveValue;
/**
* @param value handles values of length upto 3
* @returns translated value
*/
function handleHundreds(value) {
var parsedValue = "".concat(parseInt(value));
if (parseInt(value) === 0)
return "";
if (parsedValue.length === 1)
return "".concat(singleDigit[+value]);
if (parsedValue.length === 2)
return "".concat((0, common_1.handleTens)(parsedValue));
return "".concat(singleDigit[+value[0]], " hundred ").concat((0, common_1.handleTens)(value.substring(1))).trim();
}
exports.handleHundreds = handleHundreds;
/**
*
* @param integer integer part of the value
* @param fraction fractional part of the value
* @param twoDecimalPlaces fractional part corrected to two decimal places
* @returns translated value
*/
function convertIntl(integer, fraction, twoDecimalPlaces) {
if (parseInt(integer) === 0)
return 'zero';
var integerSplitted = integer.split('');
/**
* slice the string into subarrays of length 3
* eg: '123456789' -> [[1,2,3], [4,5,6], [7,8,9]]
*
* here we are deliberately slices the last 3 element of the value array
* refer the function <spliceLastThree> for more info
*/
var hundreds = spliceLastThree(integerSplitted);
var thousands = spliceLastThree(integerSplitted);
var millions = spliceLastThree(integerSplitted);
var billions = spliceLastThree(integerSplitted);
var trillions = spliceLastThree(integerSplitted);
/**
* Function, resolveValue will append an empty sting for invalid values to the output. This is further sanitized with output.trim()
*/
var result = "".concat(resolveValue(trillions, 'trillion')).concat(resolveValue(billions, 'billion')).concat(resolveValue(millions, 'million')).concat(resolveValue(thousands, 'thousand')).concat(handleHundreds(hundreds));
if (fraction && twoDecimalPlaces && +fraction[0] !== 0)
result = "".concat(result.trim(), ".").concat((0, common_1.handleTens)(twoDecimalPlaces));
return result.trim();
}
exports.convertIntl = convertIntl;
//# sourceMappingURL=international-system.js.map