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) 9.61 kB
/*! n2words/uk-UA 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 i=parseInt(t,10);let r="";"-"===n[0]&&(r="-",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)+i;return u>=s.length?r+s+"0".repeat(u-s.length):u<=0?r+"0."+"0".repeat(-u)+s:r+s.slice(0,u)+"."+s.slice(u)}function t(e){const t=e.toString();return i(t)?n(t):t}function i(e){return e.includes("e")||e.includes("E")}function r(e){const n="-"===e[0];n&&(e=e.slice(1));const t=e.indexOf(".");if(-1===t)return{isNegative:n,integerPart:BigInt(e)};const i=e.slice(0,t)||"0",r=e.slice(t+1);return{isNegative:n,integerPart:BigInt(i),decimalPart:r}}function o(e){let t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${e}"`);i(t)&&(t=n(t));const r="-"===t[0];r&&(t=t.slice(1));const o=t.indexOf(".");if(-1===o)return{isNegative:r,dollars:BigInt(t),cents:0n};const s=t.slice(0,o)||"0",u=(t.slice(o+1)+"00").slice(0,2);return{isNegative:r,dollars:BigInt(s),cents:BigInt(u)}}function s(e,n,t){if(null===n)return;if(e<n&&!(t&&BigInt(t)>=n))return;const i=n.toString().length-1,r=n===10n**BigInt(i)?`10^${i} - 1`:""+(n-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${r}`)}function u(e){return 10n**BigInt(3*(e+1))}const c=["","один","два","три","чотири","п'ять","шiсть","сiм","вiсiм","дев'ять"],f=["","одна","двi","три","чотири","п'ять","шiсть","сiм","вiсiм","дев'ять"],l=["десять","одинадцять","дванадцять","тринадцять","чотирнадцять","п'ятнадцять","шiстнадцять","сiмнадцять","вiсiмнадцять","дев'ятнадцять"],g=["","","двадцять","тридцять","сорок","п'ятдесят","шiстдесят","сiмдесят","вiсiмдесят","дев'яносто"],a=["","сто","двiстi","триста","чотириста","п'ятсот","шiстсот","сiмсот","вiсiмсот","дев'ятсот"],p="нуль",h="мiнус",b=["","перший","другий","третiй","четвертий","п'ятий","шостий","сьомий","восьмий","дев'ятий"],d=["десятий","одинадцятий","дванадцятий","тринадцятий","чотирнадцятий","п'ятнадцятий","шiстнадцятий","сiмнадцятий","вiсiмнадцятий","дев'ятнадцятий"],m=["","","двадцятий","тридцятий","сороковий","п'ятдесятий","шiстдесятий","сiмдесятий","вiсiмдесятий","дев'яностий"],w=["","сотий","двохсотий","трьохсотий","чотирьохсотий","п'ятисотий","шестисотий","семисотий","восьмисотий","дев'ятисотий"],N=["тисячний","мiльйонний","мiльярдний","трильйонний"],y=["гривня","гривнi","гривень"],v=["копiйка","копiйки","копiйок"],I=[["тисяча","тисячi","тисяч"],["мiльйон","мiльйони","мiльйонiв"],["мiльярд","мiльярди","мiльярдiв"],["трильйон","трильйони","трильйонiв"],["квадрильйон","квадрильйони","квадрильйонiв"],["квiнтильйон","квiнтильйони","квiнтильйонiв"],["секстильйон","секстильйони","секстильйонiв"],["септильйон","септильйони","септильйонiв"],["октильйон","октильйони","октильйонiв"]];function E(e,n){const t="bigint"==typeof e?Number(e):e,i=t%10,r=t%100;return r>=11&&r<=19?n[2]:1===i?n[0]:i>=2&&i<=4?n[1]:n[2]}function O(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10)%10,i=Math.trunc(e/100),r=[];return i>0&&r.push(a[i]),t>1&&r.push(g[t]),1===t?r.push(l[n]):n>0&&r.push(c[n]),r.join(" ")}function B(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10)%10,i=Math.trunc(e/100),r=[];return i>0&&r.push(a[i]),t>1&&r.push(g[t]),1===t?r.push(l[n]):n>0&&r.push(f[n]),r.join(" ")}function R(e,n){return 0n===e?p:e<1000n?"feminine"===n?B(Number(e)):O(Number(e)):function(e,n){const t=e.toString(),i=t.length,r=[],o=i%3;let s=0;for(o>0&&(r.push(Number(t.slice(0,o))),s=o);s<i;)r.push(Number(t.slice(s,s+3))),s+=3;const u=[];let c=r.length-1;for(let e=0;e<r.length;e++){const t=r[e];if(0!==t)if(0===c)u.push("feminine"===n?B(t):O(t));else{const e=E(t,I[c-1]),n=1===c?B(t):O(t);u.push(n+" "+e)}c--}return u.join(" ")}(e,n)}const $=u(I.length),j=u(N.length),x=u(I.length),P={gender:"masculine"},T={gender:["masculine","feminine"]};function S(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10);return 0===t?b[n]:1===t?d[n]:0===n?m[t]:g[t]+" "+b[n]}function M(e){if(e<100n)return S(Number(e));if(e<1000n){const n=Number(e),t=Math.trunc(n/100),i=n%100;return 0===i?w[t]:a[t]+" "+S(i)}if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);if(0===t)return 1===n?N[0]:B(n)+" "+N[0];const i=E(n,I[0]);return B(n)+" "+i+" "+M(BigInt(t))}return function(e){const n=e.toString(),t=n.length,i=[],r=t%3;let o=0;for(r>0&&(i.push(Number(n.slice(0,r))),o=r);o<t;)i.push(Number(n.slice(o,o+3))),o+=3;let s=i.length-1;for(;s>=0&&0===i[s];)s--;const u=[];let c=i.length-1;for(let e=0;e<i.length;e++){const n=i[e];if(0!==n){const t=e===s;if(0===c)u.push(t?M(BigInt(n)):O(n));else if(t)if(1===n)u.push(N[c-1]);else{const e=1===c?B(n):O(n);u.push(e+" "+N[c-1])}else{const e=E(n,I[c-1]),t=1===c?B(n):O(n);u.push(t+" "+e)}}c--}return u.join(" ")}(e)}const k={ukUA: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(i(t)){const e=n(t);if(e.includes("."))throw new RangeError("Ordinals must be whole numbers");const i=BigInt(e);if(i<=0n)throw new RangeError("Ordinals must be positive integers");return i}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,j),M(t)}};e.currency={ukUA:function(e){const{isNegative:n,dollars:i,cents:r}=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,x);let u="";return n&&(u=h+" "),(i>0n||0n===r)&&(u+=R(i,"feminine"),u+=" "+E(i,y)),r>0n&&(i>0n&&(u+=" "),u+=R(r,"feminine"),u+=" "+E(r,v)),u}},e.ordinal=k,e.ukUA=function(e,o){const{isNegative:u,integerPart:c,decimalPart:f}=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)}:r(t(e))}if("string"==typeof e)return r(function(e){const t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${e}"`);return i(t)?n(t):t}(e));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(e);s(c,$,f);const{gender:l}=function(e,n,t){const i={...n};if(void 0===e)return i;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 r=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: ${r.join(", ")}`);if(void 0!==s){if(typeof s!=typeof i[o])throw new TypeError(`Option "${o}" must be a ${typeof i[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)}`)}i[o]=s}}return i}(o,P,T);let g="";return u&&(g=h+" "),g+=R(c,l),f&&(g+=" кома "+function(e,n){let t="",i=0;for(;i<e.length&&"0"===e[i];)t&&(t+=" "),t+=p,i++;const r=e.slice(i);return r&&(t&&(t+=" "),t+=R(BigInt(r),n)),t}(f,l)),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||{});