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.59 kB
JavaScript
/*! n2words/hr-HR v5.1.2 | MIT License | github.com/forzagreen/n2words */
var e,t;e=this,t=function(e){"use strict";function t(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 n(e){const n=e.toString();return i(n)?t(n):n}function i(e){return e.includes("e")||e.includes("E")}function r(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 s(e){let n=e.trim();if(0===n.length||Number.isNaN(Number(n)))throw new RangeError(`Invalid currency format: "${e}"`);i(n)&&(n=t(n));const r="-"===n[0];r&&(n=n.slice(1));const s=n.indexOf(".");if(-1===s)return{isNegative:r,dollars:BigInt(n),cents:0n};const o=n.slice(0,s)||"0",a=(n.slice(s+1)+"00").slice(0,2);return{isNegative:r,dollars:BigInt(o),cents:BigInt(a)}}function o(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 a(e){return 10n**BigInt(3*(e+1))}const u=["","jedan","dva","tri","četiri","pet","šest","sedam","osam","devet"],c=["","jedna","dvije","tri","četiri","pet","šest","sedam","osam","devet"],l=["deset","jedanaest","dvanaest","trinaest","četrnaest","petnaest","šesnaest","sedamnaest","osamnaest","devetnaest"],f=["","","dvadeset","trideset","četrdeset","pedeset","šezdeset","sedamdeset","osamdeset","devedeset"],d=["","sto","dvjesto","tristo","č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","dvjestoti","tristoti","četiristoti","petstoti","šeststoti","sedamstoti","osamstoti","devetstoti"],w=["tisućiti","milijunti","milijarditi","bilijunti"],j=["euro","eura","eura"],N=["cent","centa","centi"],y=[["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 I(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 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(d[i]),n>1&&r.push(f[n]),1===n?r.push(l[t]):t>0&&r.push(u[t]),r.join(" ")}function O(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(d[i]),n>1&&r.push(f[n]),1===n?r.push(l[t]):t>0&&r.push(c[t]),r.join(" ")}function R(e,t){return 0n===e?g:e<1000n?"feminine"===t?O(Number(e)):E(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?O(n):E(n));else{const e=I(n,y[u-1]),t=u%2==1?O(n):E(n);a.push(t+" "+e)}u--}return a.join(" ")}(e,t)}const B=a(y.length),$=a(w.length),x=a(y.length),P={gender:"masculine"},k={gender:["masculine","feminine"]};function T(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]:f[n]+" "+p[t]}function S(e){if(e<100n)return T(Number(e));if(e<1000n){const t=Number(e),n=Math.trunc(t/100),i=t%100;return 0===i?v[n]:d[n]+" "+T(i)}if(e<1000000n){const t=Number(e/1000n),n=Number(e%1000n);if(0===n)return 1===t?w[0]:O(t)+" "+w[0];const i=I(t,y[0]);return O(t)+" "+i+" "+S(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?S(BigInt(t)):E(t));else if(n)if(1===t)a.push(w[u-1]);else{const e=u%2==1?O(t):E(t);a.push(e+" "+w[u-1])}else{const e=I(t,y[u-1]),n=u%2==1?O(t):E(t);a.push(n+" "+e)}}u--}return a.join(" ")}(e)}const M={hrHR:function(e){const n=function(e){const n=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 n=e.trim();if(0===n.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===n[0])throw new RangeError("Ordinals cannot be negative");if(n.includes("."))throw new RangeError("Ordinals must be whole numbers");if(i(n)){const e=t(n);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(n);if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}catch(t){if(t instanceof RangeError)throw t;throw new RangeError(`Invalid ordinal format: "${e}"`,{cause:t})}}(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${n}`)}(e);return o(n,$),S(n)}};e.currency={hrHR:function(e){const{isNegative:t,dollars:i,cents:r}=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}:s(n(e))}if("string"==typeof e)return s(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);o(i,x);let a="";return t&&(a=m+" "),(i>0n||0n===r)&&(a+=R(i,"masculine"),a+=" "+I(i,j)),r>0n&&(i>0n&&(a+=" "),a+=R(r,"masculine"),a+=" "+I(r,N)),a}},e.hrHR=function(e,s){const{isNegative:a,integerPart:u,decimalPart:c}=function(e){const s=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(n(e))}if("string"==typeof e)return r(function(e){const n=e.trim();if(0===n.length||Number.isNaN(Number(n)))throw new RangeError(`Invalid number format: "${e}"`);return i(n)?t(n):n}(e));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${s}`)}(e);o(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}(s,P,k);let f="";return a&&(f=m+" "),f+=R(u,l),c&&(f+=" zarez "+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+=R(BigInt(r),t)),n}(c,l)),f},e.ordinal=M},"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).n2words=e.n2words||{});