n2words
Version:
Convert numbers to words in 70+ languages with zero dependencies. Supports BigInt, decimals, and browser/Node.js environments.
3 lines (2 loc) • 7.82 kB
JavaScript
/*! n2words/cs-CZ v5.1.2 | MIT License | github.com/forzagreen/n2words */
var t,n;t=this,n=function(t){"use strict";function n(t){let[n,e]=t.toLowerCase().split("e");const r=parseInt(e,10);let i="";"-"===n[0]&&(i="-",n=n.slice(1));const s=n.indexOf("."),o=-1===s?n:n.slice(0,s)+n.slice(s+1),u=(-1===s?n.length:s)+r;return u>=o.length?i+o+"0".repeat(u-o.length):u<=0?i+"0."+"0".repeat(-u)+o:i+o.slice(0,u)+"."+o.slice(u)}function e(t){const e=t.toString();return r(e)?n(e):e}function r(t){return t.includes("e")||t.includes("E")}function i(t){const n="-"===t[0];n&&(t=t.slice(1));const e=t.indexOf(".");if(-1===e)return{isNegative:n,integerPart:BigInt(t)};const r=t.slice(0,e)||"0",i=t.slice(e+1);return{isNegative:n,integerPart:BigInt(r),decimalPart:i}}function s(t){let e=t.trim();if(0===e.length||Number.isNaN(Number(e)))throw new RangeError(`Invalid currency format: "${t}"`);r(e)&&(e=n(e));const i="-"===e[0];i&&(e=e.slice(1));const s=e.indexOf(".");if(-1===s)return{isNegative:i,dollars:BigInt(e),cents:0n};const o=e.slice(0,s)||"0",u=(e.slice(s+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(o),cents:BigInt(u)}}function o(t,n,e){if(null===n)return;if(t<n&&!(e&&BigInt(e)>=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(t){return 10n**BigInt(3*(t+1))}const c=["","jedna","dva","tři","čtyři","pět","šest","sedm","osm","devět"],a=["deset","jedenáct","dvanáct","třináct","čtrnáct","patnáct","šestnáct","sedmnáct","osmnáct","devatenáct"],l=["","","dvacet","třicet","čtyřicet","padesát","šedesát","sedmdesát","osmdesát","devadesát"],f=["","sto","dvě stě","tři sta","čtyři sta","pět set","šest set","sedm set","osm set","devět set"],d={1:["tisíc","tisíce","tisíc"],2:["milion","miliony","milionů"],3:["miliarda","miliardy","miliard"],4:["bilion","biliony","bilionů"],5:["biliarda","biliardy","biliard"],6:["trilion","triliony","trilionů"],7:["triliarda","triliardy","triliard"],8:["kvadrilion","kvadriliony","kvadrilionů"],9:["kvadriliarda","kvadriliardy","kvadriliard"]},g=Math.max(...Object.keys(d).map(Number)),m=u(g),b=u(g),h=u(g),p="nula",v="mínus",N=["","první","druhý","třetí","čtvrtý","pátý","šestý","sedmý","osmý","devátý"],w=["desátý","jedenáctý","dvanáctý","třináctý","čtrnáctý","patnáctý","šestnáctý","sedmnáctý","osmnáctý","devatenáctý"],y=["","","dvacátý","třicátý","čtyřicátý","padesátý","šedesátý","sedmdesátý","osmdesátý","devadesátý"],I=["","stý","dvoustý","třístý","čtyřstý","pětistý","šestistý","sedmistý","osmistý","devítistý"],E=["tisící","miliontý","miliardtý","biliontý"],B=["koruna","koruny","korun"],R=["haléř","haléře","haléřů"];function O(t){if(0===t)return"";const n=t%10,e=Math.trunc(t/10)%10,r=Math.trunc(t/100),i=[];return r>0&&i.push(f[r]),1===e?i.push(a[n]):e>=2?(i.push(l[e]),n>0&&i.push(c[n])):n>0&&i.push(c[n]),i.join(" ")}function k(t){if(0===t)return"";const n=t%10,e=Math.trunc(t/10)%10,r=Math.trunc(t/100),i=[];return r>0&&i.push(f[r]),1===e?i.push(a[n]):e>=2?(i.push(l[e]),n>0&&i.push(c[n])):n>0&&i.push(c[n]),i.join(" ")}function x(t,n){if(1n===t)return n[0];const e=t%10n,r=t%100n;return e>=2n&&e<=4n&&(r<10n||r>20n)?n[1]:n[2]}function P(t){if(0n===t)return p;if(t<1000n)return O(Number(t));if(t<1000000n){const n=t/1000n,e=Number(t%1000n),r=x(n,d[1]);let i;return i=1n===n?r:O(Number(n))+" "+r,e>0&&(i+=" "+k(e)),i}return function(t){const n=[];let e=t;for(;e>0n;)n.push(e%1000n),e/=1000n;let r="";for(let t=n.length-1;t>=0;t--){const e=n[t];if(0n!==e)if(r&&(r+=" "),0===t)r+=k(Number(e));else{const n=d[t];if(n){const t=x(e,n);r+=1n===e?t:O(Number(e))+" "+t}else r+=O(Number(e))}}return r}(t)}function j(t){if(0===t)return"";const n=t%10,e=Math.trunc(t/10);return 0===e?N[n]:1===e?w[n]:0===n?y[e]:l[e]+" "+N[n]}function $(t){if(t<100n)return j(Number(t));if(t<1000n){const n=Number(t),e=Math.trunc(n/100),r=n%100;return 0===r?I[e]:f[e]+" "+j(r)}if(t<1000000n){const n=Number(t/1000n),e=Number(t%1000n);if(0===e)return 1===n?E[0]:O(n)+" "+E[0];const r=x(BigInt(n),d[1]),i=1===n?"":O(n)+" ";return(1===n?"tisíc":i+r)+" "+$(BigInt(e))}return function(t){const n=[];let e=t;for(;e>0n;)n.push(e%1000n),e/=1000n;let r=0;for(let t=0;t<n.length;t++)0n!==n[t]&&(r=t);const i=[];for(let t=n.length-1;t>=0;t--){const e=n[t];if(0n===e)continue;const s=t===r;if(0===t)i.push(s?$(e):k(Number(e)));else if(s)i.push(1n===e?E[t-1]||d[t][0]:O(Number(e))+" "+(E[t-1]||d[t][0]));else{const n=d[t];if(n){const t=x(e,n);i.push(1n===e?t:O(Number(e))+" "+t)}}}return i.join(" ")}(t)}const M={csCZ:function(t){const e=function(t){const e=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 e=t.trim();if(0===e.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===e[0])throw new RangeError("Ordinals cannot be negative");if(e.includes("."))throw new RangeError("Ordinals must be whole numbers");if(r(e)){const t=n(e);if(t.includes("."))throw new RangeError("Ordinals must be whole numbers");const r=BigInt(t);if(r<=0n)throw new RangeError("Ordinals must be positive integers");return r}try{const t=BigInt(e);if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}catch(n){if(n instanceof RangeError)throw n;throw new RangeError(`Invalid ordinal format: "${t}"`,{cause:n})}}(t);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${e}`)}(t);return o(e,b),$(e)}},C={csCZ:function(t){const{isNegative:n,dollars:r,cents:i}=function(t){const n=typeof t;if("bigint"==typeof t)return t<0n?{isNegative:!0,dollars:-t,cents:0n}:{isNegative:!1,dollars:t,cents:0n};if("number"==typeof t){if(!Number.isFinite(t))throw new RangeError("Currency must be a finite number");return Number.isSafeInteger(t)?t<0?{isNegative:!0,dollars:BigInt(-t),cents:0n}:{isNegative:!1,dollars:BigInt(t),cents:0n}:s(e(t))}if("string"==typeof t)return s(t);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${n}`)}(t);o(r,h);let u="";return n&&(u=v+" "),(r>0n||0n===i)&&(u+=P(r),u+=" "+x(r,B)),i>0n&&(r>0n&&(u+=" "),u+=P(i),u+=" "+x(i,R)),u}};t.csCZ=function(t){const{isNegative:s,integerPart:u,decimalPart:c}=function(t){const s=typeof t;if("bigint"==typeof t)return t<0n?{isNegative:!0,integerPart:-t}:{isNegative:!1,integerPart:t};if("number"==typeof t){if(!Number.isFinite(t))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(t)?t<0?{isNegative:!0,integerPart:BigInt(-t)}:{isNegative:!1,integerPart:BigInt(t)}:i(e(t))}if("string"==typeof t)return i(function(t){const e=t.trim();if(0===e.length||Number.isNaN(Number(e)))throw new RangeError(`Invalid number format: "${t}"`);return r(e)?n(e):e}(t));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${s}`)}(t);o(u,m,c);let a="";if(s&&(a=v+" "),a+=P(u),c){const t=function(t){return 0n===t||1n===t?"celá":t>=2n&&t<=4n?"celé":"celých"}(u);a+=" "+t+" "+function(t){let n="",e=0;for(;e<t.length&&"0"===t[e];)n&&(n+=" "),n+=p,e++;const r=t.slice(e);return r&&(n&&(n+=" "),n+=P(BigInt(r))),n}(c)}return a},t.currency=C,t.ordinal=M},"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).n2words=t.n2words||{});