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.51 kB
/*! n2words/el-GR v5.1.2 | MIT License | github.com/forzagreen/n2words */ function n(n){let[e,t]=n.toLowerCase().split("e");const r=parseInt(t,10);let i="";"-"===e[0]&&(i="-",e=e.slice(1));const o=e.indexOf("."),s=-1===o?e:e.slice(0,o)+e.slice(o+1),u=(-1===o?e.length:o)+r;return u>=s.length?i+s+"0".repeat(u-s.length):u<=0?i+"0."+"0".repeat(-u)+s:i+s.slice(0,u)+"."+s.slice(u)}function e(e){const r=e.toString();return t(r)?n(r):r}function t(n){return n.includes("e")||n.includes("E")}function r(n){const e="-"===n[0];e&&(n=n.slice(1));const t=n.indexOf(".");if(-1===t)return{isNegative:e,integerPart:BigInt(n)};const r=n.slice(0,t)||"0",i=n.slice(t+1);return{isNegative:e,integerPart:BigInt(r),decimalPart:i}}function i(e){let r=e.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid currency format: "${e}"`);t(r)&&(r=n(r));const i="-"===r[0];i&&(r=r.slice(1));const o=r.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(r),cents:0n};const s=r.slice(0,o)||"0",u=(r.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function o(n,e,t){if(null===e)return;if(n<e)return;const r=e.toString().length-1,i=e===10n**BigInt(r)?`10^${r} - 1`:""+(e-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${i}`)}function s(n){return 10n**BigInt(3*(n+1))}const u=["","ένα","δύο","τρία","τέσσερα","πέντε","έξι","επτά","οκτώ","εννέα"],c=["δέκα","έντεκα","δώδεκα","δεκατρία","δεκατέσσερα","δεκαπέντε","δεκαέξι","δεκαεπτά","δεκαοκτώ","δεκαεννέα"],f=["","","είκοσι","τριάντα","σαράντα","πενήντα","εξήντα","εβδομήντα","ογδόντα","ενενήντα"],a=["","εκατό","διακόσια","τριακόσια","τετρακόσια","πεντακόσια","εξακόσια","επτακόσια","οκτακόσια","εννιακόσια"],g="χίλια",l="μηδέν",m="μείον",b=["εκατομμύριο","δισεκατομμύριο","τρισεκατομμύριο"],h=s(b.length+1),p=10n**BigInt(9),w=s(b.length+1),N=["","πρώτος","δεύτερος","τρίτος","τέταρτος","πέμπτος","έκτος","έβδομος","όγδοος","ένατος"],d=["δέκατος","ενδέκατος","δωδέκατος","δέκατος τρίτος","δέκατος τέταρτος","δέκατος πέμπτος","δέκατος έκτος","δέκατος έβδομος","δέκατος όγδοος","δέκατος ένατος"],v=["","","εικοστός","τριακοστός","τεσσαρακοστός","πεντηκοστός","εξηκοστός","εβδομηκοστός","ογδοηκοστός","ενενηκοστός"],I=["","εκατοστός","διακοσιοστός","τριακοσιοστός","τετρακοσιοστός","πεντακοσιοστός","εξακοσιοστός","επτακοσιοστός","οκτακοσιοστός","εννιακοσιοστός"],y="χιλιοστός",E="εκατομμυριοστός",R=["ευρώ","ευρώ"],B=["λεπτό","λεπτά"];function O(n){if(0===n)return"";const e=n%10,t=Math.trunc(n/10)%10,r=Math.trunc(n/100),i=[];r>0&&i.push(a[r]);const o=n%100;return 0===o||i.push(o<10?u[e]:o<20?c[e]:0===e?f[t]:f[t]+" "+u[e]),i.join(" ")}function P(n){if(0n===n)return l;if(n<1000n)return O(Number(n));if(n<1000000n){const e=Number(n/1000n),t=Number(n%1000n);let r;return r=1===e?g:O(e)+" "+g,t>0&&(r+=" "+O(t)),r}return function(n){const e=n.toString(),t=e.length,r=[],i=t%3;let o=0;for(i>0&&(r.push(Number(e.slice(0,i))),o=i);o<t;)r.push(Number(e.slice(o,o+3))),o+=3;const s=[];let u=r.length-1;for(let n=0;n<r.length;n++){const e=r[n];if(0!==e){const n=O(e);if(0===u)s.push(n);else if(1===u)s.push(1===e?g:n+" "+g);else{const t=b[u-2];s.push(1===e?t:n+" "+t)}}u--}return s.join(" ")}(n)}function $(i){const{isNegative:s,integerPart:c,decimalPart:f}=function(i){const o=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(e(i))}if("string"==typeof i)return r(function(e){const r=e.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid number format: "${e}"`);return t(r)?n(r):r}(i));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(i);o(c,h);let a="";return s&&(a=m+" "),a+=P(c),f&&(a+=" κόμμα "+function(n){const e=[];for(const t of n){const n=parseInt(t,10);e.push(0===n?l:u[n])}return e.join(" ")}(f)),a}function x(n){if(0===n)return"";if(n<10)return N[n];if(n<20)return d[n-10];const e=n%10,t=Math.trunc(n/10);return 0===e?v[t]:v[t]+" "+N[e]}function S(n){if(0n===n)return"";if(1n===n)return N[1];if(n<100n)return x(Number(n));if(n<1000n){const e=Number(n/100n),t=Number(n%100n);return 0===t?I[e]:a[e]+" "+x(t)}if(n<1000000n){const e=Number(n/1000n),t=Number(n%1000n);if(0===t)return 1===e?y:O(e)+" "+y;let r;if(r=1===e?g:O(e)+" "+g,t<100)return r+" "+x(t);const i=Math.trunc(t/100),o=t%100;return 0===o?r+" "+I[i]:r+" "+a[i]+" "+x(o)}const e=Number(n/1000000n),t=n%1000000n;if(0n===t)return 1===e?E:O(e)+" "+E;let r;return r=1===e?b[0]:O(e)+" "+b[0],r+" "+S(t)}function j(e){const r=function(e){const r=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 r=e.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(t(r)){const e=n(r);if(e.includes("."))throw new RangeError("Ordinals must be whole numbers");const t=BigInt(e);if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}try{const n=BigInt(r);if(n<=0n)throw new RangeError("Ordinals must be positive integers");return n}catch(n){if(n instanceof RangeError)throw n;throw new RangeError(`Invalid ordinal format: "${e}"`,{cause:n})}}(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${r}`)}(e);return o(r,p),S(r)}function M(n){const{isNegative:t,dollars:r,cents:s}=function(n){const t=typeof n;if("bigint"==typeof n)return n<0n?{isNegative:!0,dollars:-n,cents:0n}:{isNegative:!1,dollars:n,cents:0n};if("number"==typeof n){if(!Number.isFinite(n))throw new RangeError("Currency must be a finite number");return Number.isSafeInteger(n)?n<0?{isNegative:!0,dollars:BigInt(-n),cents:0n}:{isNegative:!1,dollars:BigInt(n),cents:0n}:i(e(n))}if("string"==typeof n)return i(n);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(n);o(r,w);const u=[];if(t&&u.push(m),r>0n||0n===s){const n=P(r);u.push(n+" "+(1n===r?R[0]:R[1]))}if(s>0n){const n=P(s);u.push(n+" "+(1n===s?B[0]:B[1]))}return u.join(" ")}export{h as cardinalMax,w as currencyMax,p as ordinalMax,$ as toCardinal,M as toCurrency,j as toOrdinal};