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.49 kB
/*! n2words/sr-Cyrl-RS v5.1.2 | MIT License | github.com/forzagreen/n2words */ function e(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 n(n){const r=n.toString();return t(r)?e(r):r}function t(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 r=e.slice(0,t)||"0",i=e.slice(t+1);return{isNegative:n,integerPart:BigInt(r),decimalPart:i}}function i(n){let r=n.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid currency format: "${n}"`);t(r)&&(r=e(r));const i="-"===r[0];i&&(r=r.slice(1));const o=r.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(r),cents:0n};const s=r.slice(0,o)||"0",u=(r.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function o(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 s(e){return 10n**BigInt(3*(e+1))}function u(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 c=["","један","два","три","четири","пет","шест","седам","осам","девет"],f=["","једна","две","три","четири","пет","шест","седам","осам","девет"],g=["десет","једанаест","дванаест","тринаест","четрнаест","петнаест","шеснаест","седамнаест","осамнаест","деветнаест"],l=["","","двадесет","тридесет","четрдесет","педесет","шездесет","седамдесет","осамдесет","деведесет"],a=["","сто","двеста","триста","четиристо","петсто","шесто","седамсто","осамсто","девестo"],h="нула",p="минус",b=["","први","други","трећи","четврти","пети","шести","седми","осми","девети"],m=["десети","једанаести","дванаести","тринаести","четрнаести","петнаести","шеснаести","седамнаести","осамнаести","деветнаести"],d=["","","двадесети","тридесети","четрдесети","педесети","шездесети","седамдесети","осамдесети","деведесети"],w=["","стоти","двестоти","тристоти","четиристоти","петстоти","шестоти","седамстоти","осамстоти","деветстоти"],N=["хиљадити","милионити","милијардити","билионити","билијардити","трилионити","трилијардити","квадрилионити","квадрилијардити"],v=["динар","динара","динара"],y=["пара","паре","пара"],I=[["хиљада","хиљаде","хиљада"],["милион","милиона","милиона"],["милијарда","милијарде","милијарда"],["билион","билиона","билиона"],["билијарда","билијарде","билијарда"],["трилион","трилиона","трилиона"],["трилијарда","трилијарде","трилијарда"],["квадрилион","квадрилиона","квадрилиона"],["квадрилијарда","квадрилијарде","квадрилијарда"]],E=s(I.length),O=s(N.length),B=s(I.length);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 $(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(a[r]),t>1&&i.push(l[t]),1===t?i.push(g[n]):n>0&&i.push(c[n]),i.join(" ")}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(a[r]),t>1&&i.push(l[t]),1===t?i.push(g[n]):n>0&&i.push(f[n]),i.join(" ")}function P(e,n){return 0n===e?h:e<1000n?"feminine"===n?j(Number(e)):$(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?j(t):$(t));else{const e=R(t,I[c-1]),n=c%2==1?j(t):$(t);u.push(n+" "+e)}c--}return u.join(" ")}(e,n)}const x={gender:"masculine"},S={gender:["masculine","feminine"]};function M(i,s){const{isNegative:c,integerPart:f,decimalPart:g}=function(i){const o=typeof i;if("bigint"==typeof i)return i<0n?{isNegative:!0,integerPart:-i}:{isNegative:!1,integerPart:i};if("number"==typeof i){if(!Number.isFinite(i))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(i)?i<0?{isNegative:!0,integerPart:BigInt(-i)}:{isNegative:!1,integerPart:BigInt(i)}:r(n(i))}if("string"==typeof i)return r(function(n){const r=n.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid number format: "${n}"`);return t(r)?e(r):r}(i));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(i);o(f,E,g);const{gender:l}=u(s,x,S);let a="";return c&&(a=p+" "),a+=P(f,l),g&&(a+=" запета "+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}(g,l)),a}function T(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?d[t]:l[t]+" "+b[n]}function F(e){if(e<100n)return T(Number(e));if(e<1000n){const n=Number(e),t=Math.trunc(n/100),r=n%100;return 0===r?w[t]:a[t]+" "+T(r)}if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);return 0===t?1===n?N[0]:j(n)+" "+N[0]:j(n)+" "+R(n,I[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)):$(n));else if(t)if(1===n)u.push(N[c-1]);else{const e=c%2==1?j(n):$(n);u.push(e+" "+N[c-1])}else{const e=R(n,I[c-1]),t=c%2==1?j(n):$(n);u.push(t+" "+e)}}c--}return u.join(" ")}(e)}function k(n){const r=function(n){const r=typeof n;if("bigint"==typeof n){if(n<=0n)throw new RangeError("Ordinals must be positive integers");return n}if("number"==typeof n){if(!Number.isFinite(n))throw new RangeError("Ordinals must be finite numbers");if(!Number.isInteger(n))throw new RangeError("Ordinals must be whole numbers");if(n<=0)throw new RangeError("Ordinals must be positive integers");return BigInt(n)}if("string"==typeof n)return function(n){const r=n.trim();if(0===r.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===r[0])throw new RangeError("Ordinals cannot be negative");if(r.includes("."))throw new RangeError("Ordinals must be whole numbers");if(t(r)){const n=e(r);if(n.includes("."))throw new RangeError("Ordinals must be whole numbers");const t=BigInt(n);if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}try{const e=BigInt(r);if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}catch(e){if(e instanceof RangeError)throw e;throw new RangeError(`Invalid ordinal format: "${n}"`,{cause:e})}}(n);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${r}`)}(n);return o(r,O),F(r)}const C={and:!0};function J(e,t){const{isNegative:r,dollars:s,cents:c}=function(e){const t=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}:i(n(e))}if("string"==typeof e)return i(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);o(s,B);const{and:f}=u(t,C);let g="";return r&&(g=p+" "),(s>0n||0n===c)&&(g+=P(s,"masculine"),g+=" "+R(s,v)),c>0n&&(s>0n&&(g+=f?" и ":" "),g+=P(c,"feminine"),g+=" "+R(c,y)),g}export{x as cardinalDefaults,E as cardinalMax,S as cardinalValues,C as currencyDefaults,B as currencyMax,O as ordinalMax,M as toCardinal,J as toCurrency,k as toOrdinal};