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) 8.56 kB
/*! n2words/sr-Latn-RS v5.1.2 | MIT License | github.com/forzagreen/n2words */ function e(e){let[t,n]=e.toLowerCase().split("e");const i=parseInt(n,10);let r="";"-"===t[0]&&(r="-",t=t.slice(1));const s=t.indexOf("."),o=-1===s?t:t.slice(0,s)+t.slice(s+1),a=(-1===s?t.length:s)+i;return a>=o.length?r+o+"0".repeat(a-o.length):a<=0?r+"0."+"0".repeat(-a)+o:r+o.slice(0,a)+"."+o.slice(a)}function t(t){const i=t.toString();return n(i)?e(i):i}function n(e){return e.includes("e")||e.includes("E")}function i(e){const t="-"===e[0];t&&(e=e.slice(1));const n=e.indexOf(".");if(-1===n)return{isNegative:t,integerPart:BigInt(e)};const i=e.slice(0,n)||"0",r=e.slice(n+1);return{isNegative:t,integerPart:BigInt(i),decimalPart:r}}function r(t){let i=t.trim();if(0===i.length||Number.isNaN(Number(i)))throw new RangeError(`Invalid currency format: "${t}"`);n(i)&&(i=e(i));const r="-"===i[0];r&&(i=i.slice(1));const s=i.indexOf(".");if(-1===s)return{isNegative:r,dollars:BigInt(i),cents:0n};const o=i.slice(0,s)||"0",a=(i.slice(s+1)+"00").slice(0,2);return{isNegative:r,dollars:BigInt(o),cents:BigInt(a)}}function s(e,t,n){if(null===t)return;if(e<t&&!(n&&BigInt(n)>=t))return;const i=t.toString().length-1,r=t===10n**BigInt(i)?`10^${i} - 1`:""+(t-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${r}`)}function o(e){return 10n**BigInt(3*(e+1))}function a(e,t,n){const i={...t};if(void 0===e)return i;if(!function(e){if(null===e||"object"!=typeof e)return!1;const t=Object.getPrototypeOf(e);return null===t||t===Object.prototype}(e))throw new TypeError("Invalid options: expected plain object or undefined, got "+typeof e);const r=Object.keys(t);for(const[s,o]of Object.entries(e)){if(!Object.hasOwn(t,s))throw new TypeError(`Unknown option "${s}" — expected one of: ${r.join(", ")}`);if(void 0!==o){if(typeof o!=typeof i[s])throw new TypeError(`Option "${s}" must be a ${typeof i[s]}, got ${typeof o}`);if(void 0!==n&&Object.hasOwn(n,s)){const e=n[s];if(!e.includes(o))throw new RangeError(`Option "${s}" must be one of: ${e.join(", ")} — got ${JSON.stringify(o)}`)}i[s]=o}}return i}const u=["","jedan","dva","tri","četiri","pet","šest","sedam","osam","devet"],l=["","jedna","dve","tri","četiri","pet","šest","sedam","osam","devet"],c=["deset","jedanaest","dvanaest","trinaest","četrnaest","petnaest","šesnaest","sedamnaest","osamnaest","devetnaest"],d=["","","dvadeset","trideset","četrdeset","pedeset","šezdeset","sedamdeset","osamdeset","devedeset"],f=["","sto","dvesta","trista","četiristo","petsto","šesto","sedamsto","osamsto","devetsto"],g="nula",m="minus",p=["","prvi","drugi","treći","četvrti","peti","šesti","sedmi","osmi","deveti"],h=["deseti","jedanaesti","dvanaesti","trinaesti","četrnaesti","petnaesti","šesnaesti","sedamnaesti","osamnaesti","devetnaesti"],b=["","","dvadeseti","trideseti","četrdeseti","pedeseti","šezdeseti","sedamdeseti","osamdeseti","devedeseti"],v=["","stoti","dvestoti","tristoti","četiristoti","petstoti","šestoti","sedamstoti","osamstoti","devetstoti"],w=["hiljaditi","milioniti","milijarditi","bilioniti","bilijarditi","trilioniti","trilijarditi","kvadrilioniti","kvadrilijarditi"],N=["dinar","dinara","dinara"],j=["para","pare","para"],y=[["hiljada","hiljade","hiljada"],["milion","miliona","miliona"],["milijarda","milijarde","milijarda"],["bilion","biliona","biliona"],["bilijarda","bilijarde","bilijarda"],["trilion","triliona","triliona"],["trilijarda","trilijarde","trilijarda"],["kvadrilion","kvadriliona","kvadriliona"],["kvadrilijarda","kvadrilijarde","kvadrilijarda"]],I=o(y.length),E=o(w.length),O=o(y.length);function B(e,t){const n="bigint"==typeof e?Number(e):e,i=n%10,r=n%100;return r>=11&&r<=19?t[2]:1===i?t[0]:i>=2&&i<=4?t[1]:t[2]}function R(e){if(0===e)return"";const t=e%10,n=Math.trunc(e/10)%10,i=Math.trunc(e/100),r=[];return i>0&&r.push(f[i]),n>1&&r.push(d[n]),1===n?r.push(c[t]):t>0&&r.push(u[t]),r.join(" ")}function $(e){if(0===e)return"";const t=e%10,n=Math.trunc(e/10)%10,i=Math.trunc(e/100),r=[];return i>0&&r.push(f[i]),n>1&&r.push(d[n]),1===n?r.push(c[t]):t>0&&r.push(l[t]),r.join(" ")}function k(e,t){return 0n===e?g:e<1000n?"feminine"===t?$(Number(e)):R(Number(e)):function(e,t){const n=e.toString(),i=n.length,r=[],s=i%3;let o=0;for(s>0&&(r.push(Number(n.slice(0,s))),o=s);o<i;)r.push(Number(n.slice(o,o+3))),o+=3;const a=[];let u=r.length-1;for(let e=0;e<r.length;e++){const n=r[e];if(0!==n)if(0===u)a.push("feminine"===t?$(n):R(n));else{const e=B(n,y[u-1]),t=u%2==1?$(n):R(n);a.push(t+" "+e)}u--}return a.join(" ")}(e,t)}const P={gender:"masculine"},x={gender:["masculine","feminine"]};function S(r,o){const{isNegative:u,integerPart:l,decimalPart:c}=function(r){const s=typeof r;if("bigint"==typeof r)return r<0n?{isNegative:!0,integerPart:-r}:{isNegative:!1,integerPart:r};if("number"==typeof r){if(!Number.isFinite(r))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(r)?r<0?{isNegative:!0,integerPart:BigInt(-r)}:{isNegative:!1,integerPart:BigInt(r)}:i(t(r))}if("string"==typeof r)return i(function(t){const i=t.trim();if(0===i.length||Number.isNaN(Number(i)))throw new RangeError(`Invalid number format: "${t}"`);return n(i)?e(i):i}(r));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${s}`)}(r);s(l,I,c);const{gender:d}=a(o,P,x);let f="";return u&&(f=m+" "),f+=k(l,d),c&&(f+=" zapeta "+function(e,t){let n="",i=0;for(;i<e.length&&"0"===e[i];)n&&(n+=" "),n+=g,i++;const r=e.slice(i);return r&&(n&&(n+=" "),n+=k(BigInt(r),t)),n}(c,d)),f}function M(e){if(0===e)return"";const t=e%10,n=Math.trunc(e/10);return 0===n?p[t]:1===n?h[t]:0===t?b[n]:d[n]+" "+p[t]}function T(e){if(e<100n)return M(Number(e));if(e<1000n){const t=Number(e),n=Math.trunc(t/100),i=t%100;return 0===i?v[n]:f[n]+" "+M(i)}if(e<1000000n){const t=Number(e/1000n),n=Number(e%1000n);return 0===n?1===t?w[0]:$(t)+" "+w[0]:$(t)+" "+B(t,y[0])+" "+T(BigInt(n))}return function(e){const t=e.toString(),n=t.length,i=[],r=n%3;let s=0;for(r>0&&(i.push(Number(t.slice(0,r))),s=r);s<n;)i.push(Number(t.slice(s,s+3))),s+=3;let o=i.length-1;for(;o>=0&&0===i[o];)o--;const a=[];let u=i.length-1;for(let e=0;e<i.length;e++){const t=i[e];if(0!==t){const n=e===o;if(0===u)a.push(n?T(BigInt(t)):R(t));else if(n)if(1===t)a.push(w[u-1]);else{const e=u%2==1?$(t):R(t);a.push(e+" "+w[u-1])}else{const e=B(t,y[u-1]),n=u%2==1?$(t):R(t);a.push(n+" "+e)}}u--}return a.join(" ")}(e)}function z(t){const i=function(t){const i=typeof t;if("bigint"==typeof t){if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}if("number"==typeof t){if(!Number.isFinite(t))throw new RangeError("Ordinals must be finite numbers");if(!Number.isInteger(t))throw new RangeError("Ordinals must be whole numbers");if(t<=0)throw new RangeError("Ordinals must be positive integers");return BigInt(t)}if("string"==typeof t)return function(t){const i=t.trim();if(0===i.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===i[0])throw new RangeError("Ordinals cannot be negative");if(i.includes("."))throw new RangeError("Ordinals must be whole numbers");if(n(i)){const t=e(i);if(t.includes("."))throw new RangeError("Ordinals must be whole numbers");const n=BigInt(t);if(n<=0n)throw new RangeError("Ordinals must be positive integers");return n}try{const e=BigInt(i);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: "${t}"`,{cause:e})}}(t);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${i}`)}(t);return s(i,E),T(i)}const F={and:!0};function C(e,n){const{isNegative:i,dollars:o,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}:r(t(e))}if("string"==typeof e)return r(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${n}`)}(e);s(o,O);const{and:l}=a(n,F);let c="";return i&&(c=m+" "),(o>0n||0n===u)&&(c+=k(o,"masculine"),c+=" "+B(o,N)),u>0n&&(o>0n&&(c+=l?" i ":" "),c+=k(u,"feminine"),c+=" "+B(u,j)),c}export{P as cardinalDefaults,I as cardinalMax,x as cardinalValues,F as currencyDefaults,O as currencyMax,E as ordinalMax,S as toCardinal,C as toCurrency,z as toOrdinal};