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) 7.63 kB
/*! n2words/cs-CZ v5.1.2 | MIT License | github.com/forzagreen/n2words */ function t(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 n(n){const r=n.toString();return e(r)?t(r):r}function e(t){return t.includes("e")||t.includes("E")}function r(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 i(n){let r=n.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid currency format: "${n}"`);e(r)&&(r=t(r));const i="-"===r[0];i&&(r=r.slice(1));const s=r.indexOf(".");if(-1===s)return{isNegative:i,dollars:BigInt(r),cents:0n};const o=r.slice(0,s)||"0",u=(r.slice(s+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(o),cents:BigInt(u)}}function s(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 o(t){return 10n**BigInt(3*(t+1))}const u=["","jedna","dva","tři","čtyři","pět","šest","sedm","osm","devět"],c=["deset","jedenáct","dvanáct","třináct","čtrnáct","patnáct","šestnáct","sedmnáct","osmnáct","devatenáct"],a=["","","dvacet","třicet","čtyřicet","padesát","šedesát","sedmdesát","osmdesát","devadesát"],l=["","sto","dvě stě","tři sta","čtyři sta","pět set","šest set","sedm set","osm set","devět set"],f={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"]},d=Math.max(...Object.keys(f).map(Number)),g=o(d),m=o(d),b=o(d),h="nula",p="mínus",v=["","první","druhý","třetí","čtvrtý","pátý","šestý","sedmý","osmý","devátý"],N=["desátý","jedenáctý","dvanáctý","třináctý","čtrnáctý","patnáctý","šestnáctý","sedmnáctý","osmnáctý","devatenáctý"],w=["","","dvacátý","třicátý","čtyřicátý","padesátý","šedesátý","sedmdesátý","osmdesátý","devadesátý"],y=["","stý","dvoustý","třístý","čtyřstý","pětistý","šestistý","sedmistý","osmistý","devítistý"],I=["tisící","miliontý","miliardtý","biliontý"],E=["koruna","koruny","korun"],B=["haléř","haléře","haléřů"];function R(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(l[r]),1===e?i.push(c[n]):e>=2?(i.push(a[e]),n>0&&i.push(u[n])):n>0&&i.push(u[n]),i.join(" ")}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(l[r]),1===e?i.push(c[n]):e>=2?(i.push(a[e]),n>0&&i.push(u[n])):n>0&&i.push(u[n]),i.join(" ")}function k(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 h;if(t<1000n)return R(Number(t));if(t<1000000n){const n=t/1000n,e=Number(t%1000n),r=k(n,f[1]);let i;return i=1n===n?r:R(Number(n))+" "+r,e>0&&(i+=" "+O(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+=O(Number(e));else{const n=f[t];if(n){const t=k(e,n);r+=1n===e?t:R(Number(e))+" "+t}else r+=R(Number(e))}}return r}(t)}function x(i){const{isNegative:o,integerPart:u,decimalPart:c}=function(i){const s=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 e(r)?t(r):r}(i));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${s}`)}(i);s(u,g,c);let a="";if(o&&(a=p+" "),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+=h,e++;const r=t.slice(e);return r&&(n&&(n+=" "),n+=P(BigInt(r))),n}(c)}return a}function $(t){if(0===t)return"";const n=t%10,e=Math.trunc(t/10);return 0===e?v[n]:1===e?N[n]:0===n?w[e]:a[e]+" "+v[n]}function j(t){if(t<100n)return $(Number(t));if(t<1000n){const n=Number(t),e=Math.trunc(n/100),r=n%100;return 0===r?y[e]:l[e]+" "+$(r)}if(t<1000000n){const n=Number(t/1000n),e=Number(t%1000n);if(0===e)return 1===n?I[0]:R(n)+" "+I[0];const r=k(BigInt(n),f[1]),i=1===n?"":R(n)+" ";return(1===n?"tisíc":i+r)+" "+j(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?j(e):O(Number(e)));else if(s)i.push(1n===e?I[t-1]||f[t][0]:R(Number(e))+" "+(I[t-1]||f[t][0]));else{const n=f[t];if(n){const t=k(e,n);i.push(1n===e?t:R(Number(e))+" "+t)}}}return i.join(" ")}(t)}function M(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(e(r)){const n=t(r);if(n.includes("."))throw new RangeError("Ordinals must be whole numbers");const e=BigInt(n);if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}try{const t=BigInt(r);if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}catch(t){if(t instanceof RangeError)throw t;throw new RangeError(`Invalid ordinal format: "${n}"`,{cause:t})}}(n);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${r}`)}(n);return s(r,m),j(r)}function S(t){const{isNegative:e,dollars:r,cents:o}=function(t){const e=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}:i(n(t))}if("string"==typeof t)return i(t);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${e}`)}(t);s(r,b);let u="";return e&&(u=p+" "),(r>0n||0n===o)&&(u+=P(r),u+=" "+k(r,E)),o>0n&&(r>0n&&(u+=" "),u+=P(o),u+=" "+k(o,B)),u}export{g as cardinalMax,b as currencyMax,m as ordinalMax,x as toCardinal,S as toCurrency,M as toOrdinal};