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.74 kB
JavaScript
/*! n2words/fr-FR v5.1.2 | MIT License | github.com/forzagreen/n2words */
var e,n;e=this,n=function(e){"use strict";function n(e){let[n,t]=e.toLowerCase().split("e");const i=parseInt(t,10);let r="";"-"===n[0]&&(r="-",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)+i;return u>=o.length?r+o+"0".repeat(u-o.length):u<=0?r+"0."+"0".repeat(-u)+o:r+o.slice(0,u)+"."+o.slice(u)}function t(e){const t=e.toString();return i(t)?n(t):t}function i(e){return e.includes("e")||e.includes("E")}function r(e){const n="-"===e[0];n&&(e=e.slice(1));const t=e.indexOf(".");if(-1===t)return{isNegative:n,integerPart:BigInt(e)};const i=e.slice(0,t)||"0",r=e.slice(t+1);return{isNegative:n,integerPart:BigInt(i),decimalPart:r}}function s(e){let t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${e}"`);i(t)&&(t=n(t));const r="-"===t[0];r&&(t=t.slice(1));const s=t.indexOf(".");if(-1===s)return{isNegative:r,dollars:BigInt(t),cents:0n};const o=t.slice(0,s)||"0",u=(t.slice(s+1)+"00").slice(0,2);return{isNegative:r,dollars:BigInt(o),cents:BigInt(u)}}function o(e,n,t){if(null===n)return;if(e<n&&!(t&&BigInt(t)>=n))return;const i=n.toString().length-1,r=n===10n**BigInt(i)?`10^${i} - 1`:""+(n-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${r}`)}function u(e){return 10n**BigInt(6*(e+1))}function l(e,n,t){const i={...n};if(void 0===e)return i;if(!function(e){if(null===e||"object"!=typeof e)return!1;const n=Object.getPrototypeOf(e);return null===n||n===Object.prototype}(e))throw new TypeError("Invalid options: expected plain object or undefined, got "+typeof e);const r=Object.keys(n);for(const[t,s]of Object.entries(e)){if(!Object.hasOwn(n,t))throw new TypeError(`Unknown option "${t}" — expected one of: ${r.join(", ")}`);if(void 0!==s){if(typeof s!=typeof i[t])throw new TypeError(`Option "${t}" must be a ${typeof i[t]}, got ${typeof s}`);i[t]=s}}return i}const c=["","un","deux","trois","quatre","cinq","six","sept","huit","neuf"],a=["dix","onze","douze","treize","quatorze","quinze","seize","dix-sept","dix-huit","dix-neuf"],f=["","","vingt","trente","quarante","cinquante","soixante"],g=["million","billion","trillion","quadrillion"],d=["milliard","billiard","trilliard","quadrilliard"],h=u(g.length),p=u(g.length),b=u(g.length),m="mille",w="cent",v="zéro",y="moins",N="ième";function I(e){if(0===e)return{word:"",endsWithCents:!1,endsWithVingts:!1};const n=e%100,t=Math.trunc(e/100),i=[];let r=!1,s=!1;if(t>0&&(1===t?i.push(w):0===n?(i.push(c[t]+" "+w+"s"),r=!0):i.push(c[t]+" "+w)),0===n);else if(n<10)i.push(c[n]);else if(n<17)i.push(a[n-10]);else if(n<20)i.push(a[n-10]);else if(n<70){const e=Math.trunc(n/10),t=n%10;i.push(0===t?f[e]:1===t?f[e]+" et "+c[1]:f[e]+"-"+c[t])}else if(n<80){const e=n-60;i.push(11===e?"soixante et onze":"soixante-"+a[e-10])}else if(80===n)i.push("quatre-vingts"),s=!0;else if(n<100){const e=n-80;i.push(e<10?"quatre-vingt-"+c[e]:"quatre-vingt-"+a[e-10])}return{word:i.join(" "),endsWithCents:r,endsWithVingts:s}}function E(e,n){if(1===e)return m;if(e%2==0){const t=g[e/2-1];return t?n>1n?t+"s":t:""}{const t=d[(e-1)/2-1];return t?n>1n?t+"s":t:m}}function O(e,n=!1){if(0n===e)return v;if(e<1000n){const{word:t}=I(Number(e));return n?t.replace(/ /g,"-"):t}if(e<1000000n){const t=Number(e/1000n),i=Number(e%1000n);let r;if(1===t)r=m;else{const{word:e,endsWithCents:i,endsWithVingts:s}=I(t);let o=e;(i||s)&&(o=e.slice(0,-1)),r=o+(n?"-":" ")+m}if(i>0){const{word:e}=I(i);r+=(n?"-":" ")+e}return n&&(r=r.replace(/ /g,"-")),r}return function(e,n){const t=e.toString(),i=t.length,r=[],s=i%3;let o=0;for(s>0&&(r.push(Number(t.slice(0,s))),o=s);o<i;)r.push(Number(t.slice(o,o+3))),o+=3;const u=[];let l=r.length-1;for(let e=0;e<r.length;e++){const n=r[e];if(0!==n){const e=l>0?E(l,BigInt(n)):"",{word:t,endsWithCents:i,endsWithVingts:r}=I(n);if(0===l)u.push(t);else if(1===l)if(1===n)u.push(m);else{let n=t;(i||r)&&(n=t.slice(0,-1)),u.push(n),u.push(e)}else u.push(t),u.push(e)}l--}let c=u.join(n?"-":" ");return n&&(c=c.replace(/ /g,"-")),c}(e,n)}const W={withHyphenSeparator:!1},x={and:!0},R={frFR:function(e){const t=function(e){const t=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 t=e.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(i(t)){const e=n(t);if(e.includes("."))throw new RangeError("Ordinals must be whole numbers");const i=BigInt(e);if(i<=0n)throw new RangeError("Ordinals must be positive integers");return i}try{const e=BigInt(t);if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}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 ${t}`)}(e);return o(t,p),1n===(r=t)?"premier":(s=O(r,!1)).endsWith("cinq")?s+"u"+N:s.endsWith("neuf")?s.slice(0,-1)+"v"+N:s.endsWith("cents")||s.endsWith("vingts")||s.endsWith("millions")||s.endsWith("milliards")||s.endsWith("billions")||s.endsWith("billiards")||s.endsWith("trillions")||s.endsWith("trilliards")||s.endsWith("quadrillions")||s.endsWith("quadrilliards")||s.endsWith("e")?s.slice(0,-1)+N:s+N;var r,s}};e.currency={frFR:function(e,n){const{isNegative:i,dollars:r,cents:u}=function(e){const n=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}:s(t(e))}if("string"==typeof e)return s(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${n}`)}(e);o(r,b);const{and:c}=l(n,x);let a="";return i&&(a=y+" "),(r>0n||0n===u)&&(a+=O(r,!1),a+=" "+(r<=1n?"euro":"euros")),u>0n&&(r>0n&&(a+=c?" et ":" "),a+=O(u,!1),a+=" "+(1n===u?"centime":"centimes")),a}},e.frFR=function(e,s){const{isNegative:u,integerPart:c,decimalPart:a}=function(e){const s=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)}:r(t(e))}if("string"==typeof e)return r(function(e){const t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${e}"`);return i(t)?n(t):t}(e));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${s}`)}(e);o(c,h,a);const{withHyphenSeparator:f}=l(s,W);let g="";const d=f?"-":" ";return u&&(g=y+d),g+=O(c,f),a&&(g+=d+"virgule"+d+function(e,n){let t="";const i=n?"-":" ";let r=0;for(;r<e.length&&"0"===e[r];)t&&(t+=i),t+=v,r++;const s=e.slice(r);return s&&(t&&(t+=i),t+=O(BigInt(s),n)),t}(a,f)),g},e.ordinal=R},"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).n2words=e.n2words||{});