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.42 kB
JavaScript
/*! n2words/it-IT 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 r=parseInt(n,10);let i="";"-"===t[0]&&(i="-",t=t.slice(1));const o=t.indexOf("."),s=-1===o?t:t.slice(0,o)+t.slice(o+1),u=(-1===o?t.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 n(e){const n=e.toString();return r(n)?t(n):n}function r(e){return e.includes("e")||e.includes("E")}function i(e){const t="-"===e[0];t&&(e=e.slice(1));const n=e.indexOf(".");if(-1===n)return{isNegative:t,integerPart:BigInt(e)};const r=e.slice(0,n)||"0",i=e.slice(n+1);return{isNegative:t,integerPart:BigInt(r),decimalPart:i}}function o(e){let n=e.trim();if(0===n.length||Number.isNaN(Number(n)))throw new RangeError(`Invalid currency format: "${e}"`);r(n)&&(n=t(n));const i="-"===n[0];i&&(n=n.slice(1));const o=n.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(n),cents:0n};const s=n.slice(0,o)||"0",u=(n.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function s(e,t,n){if(null===t)return;if(e<t&&!(n&&BigInt(n)>=t))return;const r=t.toString().length-1,i=t===10n**BigInt(r)?`10^${r} - 1`:""+(t-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${i}`)}function u(e){return 10n**BigInt(6*(e+1))}const c=["","uno","due","tre","quattro","cinque","sei","sette","otto","nove"],a=["dieci","undici","dodici","tredici","quattordici","quindici","sedici","diciassette","diciotto","diciannove"],f=["","","venti","trenta","quaranta","cinquanta","sessanta","settanta","ottanta","novanta"],l=["","cento","duecento","trecento","quattrocento","cinquecento","seicento","settecento","ottocento","novecento"],g=["","","vent","trent","quarant","cinquant","sessant","settant","ottant","novant"],d="zero",m="meno",p=["m","b","tr","quadr","quint","sest","sett","ott","nov","dec"],b=u(p.length),h=u(p.length),w=u(p.length),v=["","primo","secondo","terzo","quarto","quinto","sesto","settimo","ottavo","nono","decimo"],y="esimo";function N(e){if(0===e)return"";const t=e%10,n=Math.trunc(e/10)%10,r=Math.trunc(e/100);let i="";return r>0&&(i=8===n||0===n&&8===t?l[r].slice(0,-1):l[r]),0===n&&0===t||(1===n?i+=a[t]:n>=2?i+=1===t||8===t?g[n]+c[t]:3===t?f[n]+"tré":t>0?f[n]+c[t]:f[n]:t>0&&(i+=3===t&&r>0?"tré":c[t])),i}function I(e){return 0===e?"":1===e?"un":N(e)}function E(e){return 0===e?"":1===e?"mille":N(e)+"mila"}function O(e){if(e<2)return"";const t=Math.trunc((e-2)/2),n=p[t];return n?n+((e-2)%2==1?"iliardo":"ilione"):""}function B(e){if(e<2)return"";const t=Math.trunc((e-2)/2),n=p[t];return n?n+((e-2)%2==1?"iliardi":"ilioni"):""}function R(e){if(0n===e)return d;if(e<1000n)return N(Number(e));if(e<1000000n){const t=Number(e/1000n),n=Number(e%1000n);return 0===n?E(t):E(t)+N(n)}return function(e){const t=[];let n=e,r=2,i=1000000n;for(;1000n*i<=n;)i*=1000n,r++;for(let e=r;e>=0;e--){const r=1000n**BigInt(e),i=n/r;if(n%=r,0n===i)continue;const o=Number(i);if(e>=2){const n=I(o),r=1n===i?O(e):B(e);t.push(n+" "+r)}else t.push(1===e?E(o):N(o))}return function(e){const t=e.length;if(0===t)return"";if(1===t)return e[0];const n=e[t-1];if(-1===n.indexOf(" ")){let r=e[0];for(let n=1;n<t-1;n++)r+=" "+e[n];return r+" e "+n}let r=e[0];for(let n=1;n<t;n++)r+=" "+e[n];return r}(t)}(e)}const q={and:!0},$={itIT: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(r(n)){const e=t(n);if(e.includes("."))throw new RangeError("Ordinals must be whole numbers");const r=BigInt(e);if(r<=0n)throw new RangeError("Ordinals must be positive integers");return r}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 s(n,h),(i=n)<=10n?v[Number(i)]:function(e){if(e.endsWith("é"))return e.slice(0,-1)+"e"+y;if(e.endsWith("mila"))return e.slice(0,-4)+"mill"+y;if(e.endsWith("mille"))return e.slice(0,-1)+y;if(e.endsWith("ardo")||e.endsWith("ardi"))return e.slice(0,-1)+"i"+y;const t=e.slice(-1);return"aeiouàèìòù".includes(t)?e.slice(0,-1)+y:e+y}(R(i));var i}};e.currency={itIT:function(e,t){const{isNegative:r,dollars:i,cents:u}=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}:o(n(e))}if("string"==typeof e)return o(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);s(i,w);const{and:c}=function(e,t){const n={...t};if(void 0===e)return n;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[i,o]of Object.entries(e)){if(!Object.hasOwn(t,i))throw new TypeError(`Unknown option "${i}" — expected one of: ${r.join(", ")}`);if(void 0!==o){if(typeof o!=typeof n[i])throw new TypeError(`Option "${i}" must be a ${typeof n[i]}, got ${typeof o}`);n[i]=o}}return n}(t,q);let a="";return r&&(a=m+" "),(i>0n||0n===u)&&(a+=1n===i?"un":R(i),a+=" euro"),u>0n&&(i>0n&&(a+=c?" e ":" "),a+=1n===u?"un":R(u),a+=" "+(1n===u?"centesimo":"centesimi")),a}},e.itIT=function(e){const{isNegative:o,integerPart:u,decimalPart:c}=function(e){const o=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)}:i(n(e))}if("string"==typeof e)return i(function(e){const n=e.trim();if(0===n.length||Number.isNaN(Number(n)))throw new RangeError(`Invalid number format: "${e}"`);return r(n)?t(n):n}(e));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(e);s(u,b,c);let a="";return o&&(a=m+" "),a+=R(u),c&&(a+=" virgola "+function(e){let t="",n=0;for(;n<e.length&&"0"===e[n];)t&&(t+=" "),t+=d,n++;const r=e.slice(n);return r&&(t&&(t+=" "),t+=R(BigInt(r))),t}(c)),a},e.ordinal=$},"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||{});