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.7 kB
/*! n2words/el-GR v5.1.2 | MIT License | github.com/forzagreen/n2words */ var n,e;n=this,e=function(n){"use strict";function e(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 t(n){const t=n.toString();return r(t)?e(t):t}function r(n){return n.includes("e")||n.includes("E")}function i(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 o(n){let t=n.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${n}"`);r(t)&&(t=e(t));const i="-"===t[0];i&&(t=t.slice(1));const o=t.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(t),cents:0n};const s=t.slice(0,o)||"0",u=(t.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function s(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 u(n){return 10n**BigInt(3*(n+1))}const c=["","ένα","δύο","τρία","τέσσερα","πέντε","έξι","επτά","οκτώ","εννέα"],f=["δέκα","έντεκα","δώδεκα","δεκατρία","δεκατέσσερα","δεκαπέντε","δεκαέξι","δεκαεπτά","δεκαοκτώ","δεκαεννέα"],a=["","","είκοσι","τριάντα","σαράντα","πενήντα","εξήντα","εβδομήντα","ογδόντα","ενενήντα"],l=["","εκατό","διακόσια","τριακόσια","τετρακόσια","πεντακόσια","εξακόσια","επτακόσια","οκτακόσια","εννιακόσια"],g="χίλια",b="μηδέν",m="μείον",h=["εκατομμύριο","δισεκατομμύριο","τρισεκατομμύριο"],d=u(h.length+1),p=10n**BigInt(9),w=u(h.length+1),N=["","πρώτος","δεύτερος","τρίτος","τέταρτος","πέμπτος","έκτος","έβδομος","όγδοος","ένατος"],v=["δέκατος","ενδέκατος","δωδέκατος","δέκατος τρίτος","δέκατος τέταρτος","δέκατος πέμπτος","δέκατος έκτος","δέκατος έβδομος","δέκατος όγδοος","δέκατος ένατος"],y=["","","εικοστός","τριακοστός","τεσσαρακοστός","πεντηκοστός","εξηκοστός","εβδομηκοστός","ογδοηκοστός","ενενηκοστός"],I=["","εκατοστός","διακοσιοστός","τριακοσιοστός","τετρακοσιοστός","πεντακοσιοστός","εξακοσιοστός","επτακοσιοστός","οκτακοσιοστός","εννιακοσιοστός"],E="χιλιοστός",R="εκατομμυριοστός",B=["ευρώ","ευρώ"],O=["λεπτό","λεπτά"];function x(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(l[r]);const o=n%100;return 0===o||i.push(o<10?c[e]:o<20?f[e]:0===e?a[t]:a[t]+" "+c[e]),i.join(" ")}function P(n){if(0n===n)return b;if(n<1000n)return x(Number(n));if(n<1000000n){const e=Number(n/1000n),t=Number(n%1000n);let r;return r=1===e?g:x(e)+" "+g,t>0&&(r+=" "+x(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=x(e);if(0===u)s.push(n);else if(1===u)s.push(1===e?g:n+" "+g);else{const t=h[u-2];s.push(1===e?t:n+" "+t)}}u--}return s.join(" ")}(n)}function $(n){if(0===n)return"";if(n<10)return N[n];if(n<20)return v[n-10];const e=n%10,t=Math.trunc(n/10);return 0===e?y[t]:y[t]+" "+N[e]}function j(n){if(0n===n)return"";if(1n===n)return N[1];if(n<100n)return $(Number(n));if(n<1000n){const e=Number(n/100n),t=Number(n%100n);return 0===t?I[e]:l[e]+" "+$(t)}if(n<1000000n){const e=Number(n/1000n),t=Number(n%1000n);if(0===t)return 1===e?E:x(e)+" "+E;let r;if(r=1===e?g:x(e)+" "+g,t<100)return r+" "+$(t);const i=Math.trunc(t/100),o=t%100;return 0===o?r+" "+I[i]:r+" "+l[i]+" "+$(o)}const e=Number(n/1000000n),t=n%1000000n;if(0n===t)return 1===e?R:x(e)+" "+R;let r;return r=1===e?h[0]:x(e)+" "+h[0],r+" "+j(t)}const S={elGR:function(n){const t=function(n){const t=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 t=n.trim();if(0===t.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===t[0])throw new RangeError("Ordinals cannot be negative");if(t.includes("."))throw new RangeError("Ordinals must be whole numbers");if(r(t)){const n=e(t);if(n.includes("."))throw new RangeError("Ordinals must be whole numbers");const r=BigInt(n);if(r<=0n)throw new RangeError("Ordinals must be positive integers");return r}try{const n=BigInt(t);if(n<=0n)throw new RangeError("Ordinals must be positive integers");return n}catch(e){if(e instanceof RangeError)throw e;throw new RangeError(`Invalid ordinal format: "${n}"`,{cause:e})}}(n);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(n);return s(t,p),j(t)}};n.currency={elGR:function(n){const{isNegative:e,dollars:r,cents:i}=function(n){const e=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}:o(t(n))}if("string"==typeof n)return o(n);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${e}`)}(n);s(r,w);const u=[];if(e&&u.push(m),r>0n||0n===i){const n=P(r);u.push(n+" "+(1n===r?B[0]:B[1]))}if(i>0n){const n=P(i);u.push(n+" "+(1n===i?O[0]:O[1]))}return u.join(" ")}},n.elGR=function(n){const{isNegative:o,integerPart:u,decimalPart:f}=function(n){const o=typeof n;if("bigint"==typeof n)return n<0n?{isNegative:!0,integerPart:-n}:{isNegative:!1,integerPart:n};if("number"==typeof n){if(!Number.isFinite(n))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(n)?n<0?{isNegative:!0,integerPart:BigInt(-n)}:{isNegative:!1,integerPart:BigInt(n)}:i(t(n))}if("string"==typeof n)return i(function(n){const t=n.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${n}"`);return r(t)?e(t):t}(n));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(n);s(u,d);let a="";return o&&(a=m+" "),a+=P(u),f&&(a+=" κόμμα "+function(n){const e=[];for(const t of n){const n=parseInt(t,10);e.push(0===n?b:c[n])}return e.join(" ")}(f)),a},n.ordinal=S},"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).n2words=n.n2words||{});