UNPKG

n2words

Version:

Convert numbers to words in 70+ languages with zero dependencies. Supports BigInt, decimals, and browser/Node.js environments.

3 lines (2 loc) 10.2 kB
/*! n2words/ru-RU v5.1.2 | MIT License | github.com/forzagreen/n2words */ var e,n;e=this,n=function(e){"use strict";function n(e){let[n,t]=e.toLowerCase().split("e");const r=parseInt(t,10);let i="";"-"===n[0]&&(i="-",n=n.slice(1));const o=n.indexOf("."),s=-1===o?n:n.slice(0,o)+n.slice(o+1),u=(-1===o?n.length:o)+r;return u>=s.length?i+s+"0".repeat(u-s.length):u<=0?i+"0."+"0".repeat(-u)+s:i+s.slice(0,u)+"."+s.slice(u)}function t(e){const t=e.toString();return r(t)?n(t):t}function r(e){return e.includes("e")||e.includes("E")}function i(e){const n="-"===e[0];n&&(e=e.slice(1));const t=e.indexOf(".");if(-1===t)return{isNegative:n,integerPart:BigInt(e)};const r=e.slice(0,t)||"0",i=e.slice(t+1);return{isNegative:n,integerPart:BigInt(r),decimalPart:i}}function o(e){let t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${e}"`);r(t)&&(t=n(t));const i="-"===t[0];i&&(t=t.slice(1));const o=t.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(t),cents:0n};const s=t.slice(0,o)||"0",u=(t.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function s(e,n,t){if(null===n)return;if(e<n&&!(t&&BigInt(t)>=n))return;const r=n.toString().length-1,i=n===10n**BigInt(r)?`10^${r} - 1`:""+(n-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${i}`)}function u(e){return 10n**BigInt(3*(e+1))}function c(e,n,t){const r={...n};if(void 0===e)return r;if(!function(e){if(null===e||"object"!=typeof e)return!1;const n=Object.getPrototypeOf(e);return null===n||n===Object.prototype}(e))throw new TypeError("Invalid options: expected plain object or undefined, got "+typeof e);const i=Object.keys(n);for(const[o,s]of Object.entries(e)){if(!Object.hasOwn(n,o))throw new TypeError(`Unknown option "${o}" — expected one of: ${i.join(", ")}`);if(void 0!==s){if(typeof s!=typeof r[o])throw new TypeError(`Option "${o}" must be a ${typeof r[o]}, got ${typeof s}`);if(void 0!==t&&Object.hasOwn(t,o)){const e=t[o];if(!e.includes(s))throw new RangeError(`Option "${o}" must be one of: ${e.join(", ")} — got ${JSON.stringify(s)}`)}r[o]=s}}return r}const f=["","один","два","три","четыре","пять","шесть","семь","восемь","девять"],l=["","одна","две","три","четыре","пять","шесть","семь","восемь","девять"],a=["десять","одиннадцать","двенадцать","тринадцать","четырнадцать","пятнадцать","шестнадцать","семнадцать","восемнадцать","девятнадцать"],g=["","","двадцать","тридцать","сорок","пятьдесят","шестьдесят","семьдесят","восемьдесят","девяносто"],p=["","сто","двести","триста","четыреста","пятьсот","шестьсот","семьсот","восемьсот","девятьсот"],h="ноль",b="минус",d=[["тысяча","тысячи","тысяч"],["миллион","миллиона","миллионов"],["миллиард","миллиарда","миллиардов"],["триллион","триллиона","триллионов"],["квадриллион","квадриллиона","квадриллионов"],["квинтиллион","квинтиллиона","квинтиллионов"],["секстиллион","секстиллиона","секстиллионов"],["септиллион","септиллиона","септиллионов"],["октиллион","октиллиона","октиллионов"],["нониллион","нониллиона","нониллионов"]],m=["","первый","второй","третий","четвёртый","пятый","шестой","седьмой","восьмой","девятый"],w=["десятый","одиннадцатый","двенадцатый","тринадцатый","четырнадцатый","пятнадцатый","шестнадцатый","семнадцатый","восемнадцатый","девятнадцатый"],N=["","","двадцатый","тридцатый","сороковой","пятидесятый","шестидесятый","семидесятый","восьмидесятый","девяностый"],y=["","сотый","двухсотый","трёхсотый","четырёхсотый","пятисотый","шестисотый","семисотый","восьмисотый","девятисотый"],v=["тысячный","миллионный","миллиардный","триллионный","квадриллионный","квинтиллионный","секстиллионный","септиллионный","октиллионный","нониллионный"],I=["","","двух","трёх","четырёх","пяти","шести","семи","восьми","девяти"],E=["рубль","рубля","рублей"],O=["копейка","копейки","копеек"];function R(e,n){const t="bigint"==typeof e?Number(e):e,r=t%10,i=t%100;return i>=11&&i<=19?n[2]:1===r?n[0]:r>=2&&r<=4?n[1]:n[2]}function B(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10)%10,r=Math.trunc(e/100),i=[];return r>0&&i.push(p[r]),t>1&&i.push(g[t]),1===t?i.push(a[n]):n>0&&i.push(f[n]),i.join(" ")}function $(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10)%10,r=Math.trunc(e/100),i=[];return r>0&&i.push(p[r]),t>1&&i.push(g[t]),1===t?i.push(a[n]):n>0&&i.push(l[n]),i.join(" ")}function j(e,n){if(0n===e)return h;const t="feminine"===n;if(e<1000n)return t?$(Number(e)):B(Number(e));if(e<1000000n){const n=Number(e/1000n),r=Number(e%1000n);let i=$(n)+" "+R(n,d[0]);return r>0&&(i+=" "+(t?$(r):B(r))),i}return function(e,n){const t="feminine"===n,r=e.toString(),i=r.length,o=[],s=i%3;let u=0;for(s>0&&(o.push(Number(r.slice(0,s))),u=s);u<i;)o.push(Number(r.slice(u,u+3))),u+=3;const c=[];let f=o.length-1;for(let e=0;e<o.length;e++){const n=o[e];if(0!==n)if(0===f)c.push(t?$(n):B(n));else{const e=R(n,d[f-1]),t=1===f?$(n):B(n);c.push(t+" "+e)}f--}return c.join(" ")}(e,n)}const x=u(d.length),P=u(v.length),T=u(d.length),S={gender:"masculine"},M={gender:["masculine","feminine"]};function U(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10);return 0===t?m[n]:1===t?w[n]:0===n?N[t]:g[t]+" "+m[n]}function F(e){if(e<100n)return U(Number(e));if(e<1000n){const n=Number(e),t=Math.trunc(n/100),r=n%100;return 0===r?y[t]:p[t]+" "+U(r)}if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);return 0===t?1===n?v[0]:n<10?I[n]+v[0]:$(n)+" "+v[0]:$(n)+" "+R(n,d[0])+" "+F(BigInt(t))}return function(e){const n=e.toString(),t=n.length,r=[],i=t%3;let o=0;for(i>0&&(r.push(Number(n.slice(0,i))),o=i);o<t;)r.push(Number(n.slice(o,o+3))),o+=3;let s=r.length-1;for(;s>=0&&0===r[s];)s--;const u=[];let c=r.length-1;for(let e=0;e<r.length;e++){const n=r[e];if(0!==n){const t=e===s;if(0===c)u.push(t?F(BigInt(n)):B(n));else if(t)if(1===n)u.push(v[c-1]);else{const e=1===c?$(n):B(n);u.push(e+" "+v[c-1])}else{const e=R(n,d[c-1]),t=1===c?$(n):B(n);u.push(t+" "+e)}}c--}return u.join(" ")}(e)}const k={and:!0},C={ruRU:function(e){const t=function(e){const t=typeof e;if("bigint"==typeof e){if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}if("number"==typeof e){if(!Number.isFinite(e))throw new RangeError("Ordinals must be finite numbers");if(!Number.isInteger(e))throw new RangeError("Ordinals must be whole numbers");if(e<=0)throw new RangeError("Ordinals must be positive integers");return BigInt(e)}if("string"==typeof e)return function(e){const t=e.trim();if(0===t.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===t[0])throw new RangeError("Ordinals cannot be negative");if(t.includes("."))throw new RangeError("Ordinals must be whole numbers");if(r(t)){const e=n(t);if(e.includes("."))throw new RangeError("Ordinals must be whole numbers");const r=BigInt(e);if(r<=0n)throw new RangeError("Ordinals must be positive integers");return r}try{const e=BigInt(t);if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}catch(n){if(n instanceof RangeError)throw n;throw new RangeError(`Invalid ordinal format: "${e}"`,{cause:n})}}(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);return s(t,P),F(t)}};e.currency={ruRU:function(e,n){const{isNegative:r,dollars:i,cents:u}=function(e){const n=typeof e;if("bigint"==typeof e)return e<0n?{isNegative:!0,dollars:-e,cents:0n}:{isNegative:!1,dollars:e,cents:0n};if("number"==typeof e){if(!Number.isFinite(e))throw new RangeError("Currency must be a finite number");return Number.isSafeInteger(e)?e<0?{isNegative:!0,dollars:BigInt(-e),cents:0n}:{isNegative:!1,dollars:BigInt(e),cents:0n}:o(t(e))}if("string"==typeof e)return o(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${n}`)}(e);s(i,T);const{and:f}=c(n,k);let l="";return r&&(l=b+" "),(i>0n||0n===u)&&(l+=j(i,"masculine"),l+=" "+R(i,E)),u>0n&&(i>0n&&(l+=f?" и ":" "),l+=j(u,"feminine"),l+=" "+R(u,O)),l}},e.ordinal=C,e.ruRU=function(e,o){const{isNegative:u,integerPart:f,decimalPart:l}=function(e){const o=typeof e;if("bigint"==typeof e)return e<0n?{isNegative:!0,integerPart:-e}:{isNegative:!1,integerPart:e};if("number"==typeof e){if(!Number.isFinite(e))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(e)?e<0?{isNegative:!0,integerPart:BigInt(-e)}:{isNegative:!1,integerPart:BigInt(e)}:i(t(e))}if("string"==typeof e)return i(function(e){const t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${e}"`);return r(t)?n(t):t}(e));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(e);s(f,x,l);const{gender:a}=c(o,S,M);let g="";return u&&(g=b+" "),g+=j(f,a),l&&(g+=" запятая "+function(e,n){let t="",r=0;for(;r<e.length&&"0"===e[r];)t&&(t+=" "),t+=h,r++;const i=e.slice(r);return i&&(t&&(t+=" "),t+=j(BigInt(i),n)),t}(l,a)),g}},"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).n2words=e.n2words||{});