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.44 kB
JavaScript
/*! n2words/hr-HR 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))}const a=["","jedan","dva","tri","četiri","pet","šest","sedam","osam","devet"],u=["","jedna","dvije","tri","četiri","pet","šest","sedam","osam","devet"],c=["deset","jedanaest","dvanaest","trinaest","četrnaest","petnaest","šesnaest","sedamnaest","osamnaest","devetnaest"],l=["","","dvadeset","trideset","četrdeset","pedeset","šezdeset","sedamdeset","osamdeset","devedeset"],f=["","sto","dvjesto","tristo","četiristo","petsto","šesto","sedamsto","osamsto","devetsto"],d="nula",g="minus",m=["","prvi","drugi","treći","četvrti","peti","šesti","sedmi","osmi","deveti"],p=["deseti","jedanaesti","dvanaesti","trinaesti","četrnaesti","petnaesti","šesnaesti","sedamnaesti","osamnaesti","devetnaesti"],h=["","","dvadeseti","trideseti","četrdeseti","pedeseti","šezdeseti","sedamdeseti","osamdeseti","devedeseti"],b=["","stoti","dvjestoti","tristoti","četiristoti","petstoti","šeststoti","sedamstoti","osamstoti","devetstoti"],v=["tisućiti","milijunti","milijarditi","bilijunti"],w=["euro","eura","eura"],j=["cent","centa","centi"],N=[["tisuća","tisuće","tisuća"],["milijun","milijuna","milijuna"],["milijarda","milijarde","milijarda"],["bilijun","bilijuna","bilijuna"],["bilijarda","bilijarde","bilijarda"],["trilijun","trilijuna","trilijuna"],["trilijarda","trilijarde","trilijarda"],["kvadrilijun","kvadrilijuna","kvadrilijuna"],["kvadrilijarda","kvadrilijarde","kvadrilijarda"]];function y(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 I(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(l[n]),1===n?r.push(c[t]):t>0&&r.push(a[t]),r.join(" ")}function E(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(l[n]),1===n?r.push(c[t]):t>0&&r.push(u[t]),r.join(" ")}function O(e,t){return 0n===e?d:e<1000n?"feminine"===t?E(Number(e)):I(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?E(n):I(n));else{const e=y(n,N[u-1]),t=u%2==1?E(n):I(n);a.push(t+" "+e)}u--}return a.join(" ")}(e,t)}const B=o(N.length),R=o(v.length),$=o(N.length),P={gender:"masculine"},x={gender:["masculine","feminine"]};function k(r,o){const{isNegative:a,integerPart:u,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(u,B,c);const{gender:l}=function(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}(o,P,x);let f="";return a&&(f=g+" "),f+=O(u,l),c&&(f+=" zarez "+function(e,t){let n="",i=0;for(;i<e.length&&"0"===e[i];)n&&(n+=" "),n+=d,i++;const r=e.slice(i);return r&&(n&&(n+=" "),n+=O(BigInt(r),t)),n}(c,l)),f}function S(e){if(0===e)return"";const t=e%10,n=Math.trunc(e/10);return 0===n?m[t]:1===n?p[t]:0===t?h[n]:l[n]+" "+m[t]}function M(e){if(e<100n)return S(Number(e));if(e<1000n){const t=Number(e),n=Math.trunc(t/100),i=t%100;return 0===i?b[n]:f[n]+" "+S(i)}if(e<1000000n){const t=Number(e/1000n),n=Number(e%1000n);if(0===n)return 1===t?v[0]:E(t)+" "+v[0];const i=y(t,N[0]);return E(t)+" "+i+" "+M(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?M(BigInt(t)):I(t));else if(n)if(1===t)a.push(v[u-1]);else{const e=u%2==1?E(t):I(t);a.push(e+" "+v[u-1])}else{const e=y(t,N[u-1]),n=u%2==1?E(t):I(t);a.push(n+" "+e)}}u--}return a.join(" ")}(e)}function T(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,R),M(i)}function z(e){const{isNegative:n,dollars:i,cents:o}=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(i,$);let a="";return n&&(a=g+" "),(i>0n||0n===o)&&(a+=O(i,"masculine"),a+=" "+y(i,w)),o>0n&&(i>0n&&(a+=" "),a+=O(o,"masculine"),a+=" "+y(o,j)),a}export{P as cardinalDefaults,B as cardinalMax,x as cardinalValues,$ as currencyMax,R as ordinalMax,k as toCardinal,z as toCurrency,T as toOrdinal};