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.62 kB
/*! n2words/sr-Cyrl-RS 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=["","сто","двеста","триста","четиристо","петсто","шесто","седамсто","осамсто","девестo"],h="нула",d="минус",b=["","први","други","трећи","четврти","пети","шести","седми","осми","девети"],m=["десети","једанаести","дванаести","тринаести","четрнаести","петнаести","шеснаести","седамнаести","осамнаести","деветнаести"],w=["","","двадесети","тридесети","четрдесети","педесети","шездесети","седамдесети","осамдесети","деведесети"],y=["","стоти","двестоти","тристоти","четиристоти","петстоти","шестоти","седамстоти","осамстоти","деветстоти"],N=["хиљадити","милионити","милијардити","билионити","билијардити","трилионити","трилијардити","квадрилионити","квадрилијардити"],v=["динар","динара","динара"],I=["пара","паре","пара"],E=[["хиљада","хиљаде","хиљада"],["милион","милиона","милиона"],["милијарда","милијарде","милијарда"],["билион","билиона","билиона"],["билијарда","билијарде","билијарда"],["трилион","трилиона","трилиона"],["трилијарда","трилијарде","трилијарда"],["квадрилион","квадрилиона","квадрилиона"],["квадрилијарда","квадрилијарде","квадрилијарда"]],O=u(E.length),R=u(N.length),B=u(E.length);function $(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 j(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 x(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 P(e,n){return 0n===e?h:e<1000n?"feminine"===n?x(Number(e)):j(Number(e)):function(e,n){const t=e.toString(),r=t.length,i=[],o=r%3;let s=0;for(o>0&&(i.push(Number(t.slice(0,o))),s=o);s<r;)i.push(Number(t.slice(s,s+3))),s+=3;const u=[];let c=i.length-1;for(let e=0;e<i.length;e++){const t=i[e];if(0!==t)if(0===c)u.push("feminine"===n?x(t):j(t));else{const e=$(t,E[c-1]),n=c%2==1?x(t):j(t);u.push(n+" "+e)}c--}return u.join(" ")}(e,n)}const S={gender:"masculine"},T={gender:["masculine","feminine"]};function M(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10);return 0===t?b[n]:1===t?m[n]:0===n?w[t]:g[t]+" "+b[n]}function C(e){if(e<100n)return M(Number(e));if(e<1000n){const n=Number(e),t=Math.trunc(n/100),r=n%100;return 0===r?y[t]:p[t]+" "+M(r)}if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);return 0===t?1===n?N[0]:x(n)+" "+N[0]:x(n)+" "+$(n,E[0])+" "+C(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?C(BigInt(n)):j(n));else if(t)if(1===n)u.push(N[c-1]);else{const e=c%2==1?x(n):j(n);u.push(e+" "+N[c-1])}else{const e=$(n,E[c-1]),t=c%2==1?x(n):j(n);u.push(t+" "+e)}}c--}return u.join(" ")}(e)}const F={and:!0},k={srCyrlRS: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,R),C(t)}};e.currency={srCyrlRS: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,B);const{and:f}=c(n,F);let l="";return r&&(l=d+" "),(i>0n||0n===u)&&(l+=P(i,"masculine"),l+=" "+$(i,v)),u>0n&&(i>0n&&(l+=f?" и ":" "),l+=P(u,"feminine"),l+=" "+$(u,I)),l}},e.ordinal=k,e.srCyrlRS=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,O,l);const{gender:a}=c(o,S,T);let g="";return u&&(g=d+" "),g+=P(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+=P(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||{});