mx-wolf-amount-spelled-out
Version:
сумма прописью для платежки ЦБ
76 lines (75 loc) • 2.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.amountSpelledOut = void 0;
const name_and_twenty_1 = require("./name-and-twenty");
const plurality_1 = require("./plurality");
const systematic_class_1 = require("./systematic-class");
const three_digits_1 = require("./three-digits");
const THOUSAND = 1e3;
function number_parser(_num, _desc) {
if (_num === 0) {
if (_desc === 0 /* unit */) {
return "рублей";
}
else {
return "";
}
}
let [_string, plu] = (0, name_and_twenty_1.getRawNameAndTwenty)((0, three_digits_1.getThreeDigits)(_num));
if (systematic_class_1.isFeminine.get(_desc)) {
_string = _string.replace("один ", "одна ");
_string = _string.replace("два ", "две ");
}
_string += systematic_class_1.names.get(_desc)?.get((0, plurality_1.getPlurality)(plu));
_string = _string.replace(" ", " ");
return _string;
}
function decimals_parser(_num) {
const u = _num % 10;
const t = Math.floor(_num / 10);
let _string = " " + t + u;
if (t !== 1) {
if (u === 1) {
_string += " копейка";
}
else if (u > 1 && u < 5) {
_string += " копейки";
}
else {
_string += " копеек";
}
}
else {
_string += " копеек";
}
return _string;
}
function rubsToString(rubs) {
if (rubs <= 0) {
return "ноль рублей";
}
return systematic_class_1.reverseSystematicGroups
.map((g) => {
const de = THOUSAND ** g;
const v = Math.floor((rubs / de) % THOUSAND);
return number_parser(v, g);
})
.join(" ");
}
function amountSpelledOut(_number) {
if (typeof _number !== "number") {
throw new Error("Numbers only");
}
if (!isFinite(_number)) {
throw new Error("finite numbers only");
}
if (_number <= 0) {
throw new Error("Positive numbers only");
}
const cents = Math.floor((_number * 100) % 100);
const rubs = Math.floor(_number);
const res = rubsToString(rubs);
const r1 = (res + decimals_parser(cents)).replace(/\s+/g, " ").trim();
return r1[0].toLocaleUpperCase() + r1.substring(1);
}
exports.amountSpelledOut = amountSpelledOut;