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.6 kB
JavaScript
/*! n2words/fr-FR v5.1.2 | MIT License | github.com/forzagreen/n2words */
function e(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 n(n){const i=n.toString();return t(i)?e(i):i}function t(e){return e.includes("e")||e.includes("E")}function i(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 r(n){let i=n.trim();if(0===i.length||Number.isNaN(Number(i)))throw new RangeError(`Invalid currency format: "${n}"`);t(i)&&(i=e(i));const r="-"===i[0];r&&(i=i.slice(1));const s=i.indexOf(".");if(-1===s)return{isNegative:r,dollars:BigInt(i),cents:0n};const o=i.slice(0,s)||"0",u=(i.slice(s+1)+"00").slice(0,2);return{isNegative:r,dollars:BigInt(o),cents:BigInt(u)}}function s(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 o(e){return 10n**BigInt(6*(e+1))}function u(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 l=["","un","deux","trois","quatre","cinq","six","sept","huit","neuf"],c=["dix","onze","douze","treize","quatorze","quinze","seize","dix-sept","dix-huit","dix-neuf"],a=["","","vingt","trente","quarante","cinquante","soixante"],f=["million","billion","trillion","quadrillion"],g=["milliard","billiard","trilliard","quadrilliard"],d=o(f.length),h=o(f.length),p=o(f.length),b="mille",m="cent",w="zéro",v="moins",y="ième";function N(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(m):0===n?(i.push(l[t]+" "+m+"s"),r=!0):i.push(l[t]+" "+m)),0===n);else if(n<10)i.push(l[n]);else if(n<17)i.push(c[n-10]);else if(n<20)i.push(c[n-10]);else if(n<70){const e=Math.trunc(n/10),t=n%10;i.push(0===t?a[e]:1===t?a[e]+" et "+l[1]:a[e]+"-"+l[t])}else if(n<80){const e=n-60;i.push(11===e?"soixante et onze":"soixante-"+c[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-"+l[e]:"quatre-vingt-"+c[e-10])}return{word:i.join(" "),endsWithCents:r,endsWithVingts:s}}function I(e,n){if(1===e)return b;if(e%2==0){const t=f[e/2-1];return t?n>1n?t+"s":t:""}{const t=g[(e-1)/2-1];return t?n>1n?t+"s":t:b}}function E(e,n=!1){if(0n===e)return w;if(e<1000n){const{word:t}=N(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=b;else{const{word:e,endsWithCents:i,endsWithVingts:s}=N(t);let o=e;(i||s)&&(o=e.slice(0,-1)),r=o+(n?"-":" ")+b}if(i>0){const{word:e}=N(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?I(l,BigInt(n)):"",{word:t,endsWithCents:i,endsWithVingts:r}=N(n);if(0===l)u.push(t);else if(1===l)if(1===n)u.push(b);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 O={withHyphenSeparator:!1};function W(r,o){const{isNegative:l,integerPart:c,decimalPart:a}=function(r){const s=typeof r;if("bigint"==typeof r)return r<0n?{isNegative:!0,integerPart:-r}:{isNegative:!1,integerPart:r};if("number"==typeof r){if(!Number.isFinite(r))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(r)?r<0?{isNegative:!0,integerPart:BigInt(-r)}:{isNegative:!1,integerPart:BigInt(r)}:i(n(r))}if("string"==typeof r)return i(function(n){const i=n.trim();if(0===i.length||Number.isNaN(Number(i)))throw new RangeError(`Invalid number format: "${n}"`);return t(i)?e(i):i}(r));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${s}`)}(r);s(c,d,a);const{withHyphenSeparator:f}=u(o,O);let g="";const h=f?"-":" ";return l&&(g=v+h),g+=E(c,f),a&&(g+=h+"virgule"+h+function(e,n){let t="";const i=n?"-":" ";let r=0;for(;r<e.length&&"0"===e[r];)t&&(t+=i),t+=w,r++;const s=e.slice(r);return s&&(t&&(t+=i),t+=E(BigInt(s),n)),t}(a,f)),g}function x(n){const i=function(n){const i=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 i=n.trim();if(0===i.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===i[0])throw new RangeError("Ordinals cannot be negative");if(i.includes("."))throw new RangeError("Ordinals must be whole numbers");if(t(i)){const n=e(i);if(n.includes("."))throw new RangeError("Ordinals must be whole numbers");const t=BigInt(n);if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}try{const e=BigInt(i);if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}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 ${i}`)}(n);return s(i,h),1n===(r=i)?"premier":(o=E(r,!1)).endsWith("cinq")?o+"u"+y:o.endsWith("neuf")?o.slice(0,-1)+"v"+y:o.endsWith("cents")||o.endsWith("vingts")||o.endsWith("millions")||o.endsWith("milliards")||o.endsWith("billions")||o.endsWith("billiards")||o.endsWith("trillions")||o.endsWith("trilliards")||o.endsWith("quadrillions")||o.endsWith("quadrilliards")||o.endsWith("e")?o.slice(0,-1)+y:o+y;var r,o}const B={and:!0};function R(e,t){const{isNegative:i,dollars:o,cents:l}=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}:r(n(e))}if("string"==typeof e)return r(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);s(o,p);const{and:c}=u(t,B);let a="";return i&&(a=v+" "),(o>0n||0n===l)&&(a+=E(o,!1),a+=" "+(o<=1n?"euro":"euros")),l>0n&&(o>0n&&(a+=c?" et ":" "),a+=E(l,!1),a+=" "+(1n===l?"centime":"centimes")),a}export{O as cardinalDefaults,d as cardinalMax,B as currencyDefaults,p as currencyMax,h as ordinalMax,W as toCardinal,R as toCurrency,x as toOrdinal};