n2words
Version:
Convert numbers to words in 70+ languages with zero dependencies. Supports BigInt, decimals, and browser/Node.js environments.
3 lines (2 loc) • 8.48 kB
JavaScript
/*! n2words/he-IL v5.1.2 | MIT License | github.com/forzagreen/n2words */
function e(e){let[n,t]=e.toLowerCase().split("e");const r=parseInt(t,10);let i="";"-"===n[0]&&(i="-",n=n.slice(1));const o=n.indexOf("."),s=-1===o?n:n.slice(0,o)+n.slice(o+1),u=(-1===o?n.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(n){const r=n.toString();return t(r)?e(r):r}function t(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 r=e.slice(0,t)||"0",i=e.slice(t+1);return{isNegative:n,integerPart:BigInt(r),decimalPart:i}}function i(n){let r=n.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid currency format: "${n}"`);t(r)&&(r=e(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(e,n,t){if(null===n)return;if(e<n)return;const r=n.toString().length-1,i=n===10n**BigInt(r)?`10^${r} - 1`:""+(n-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${i}`)}function s(e){return 10n**BigInt(3*(e+1))}const u=["","אחת","שתים","שלש","ארבע","חמש","שש","שבע","שמונה","תשע"],c=["עשר","אחת עשרה","שתים עשרה","שלש עשרה","ארבע עשרה","חמש עשרה","שש עשרה","שבע עשרה","שמונה עשרה","תשע עשרה"],f=["","","עשרים","שלשים","ארבעים","חמישים","ששים","שבעים","שמונים","תשעים"],a=["","מאה","מאתיים","שלש מאות","ארבע מאות","חמש מאות","שש מאות","שבע מאות","שמונה מאות","תשע מאות"],l=["","אלף","אלפיים","שלשת אלפים","ארבעת אלפים","חמשת אלפים","ששת אלפים","שבעת אלפים","שמונת אלפים","תשעת אלפים"],g=["","אלף","מיליון","מיליארד","טריליון","קוודרליון","קווינטיליון"],p=["","אלפים","מיליונים","מיליארדים","טריליונים","קוודרליונים","קווינטיליונים"],b=s(g.length-1),h=10n**BigInt(9),m=s(g.length-1),d="אפס",w="מינוס",N=["","אחד","שניים","שלשה","ארבעה","חמשה","ששה","שבעה","שמונה","תשעה"],v=["","ראשון","שני","שלישי","רביעי","חמישי","שישי","שביעי","שמיני","תשיעי"],y=["עשירי","אחד עשר","שנים עשר","שלשה עשר","ארבעה עשר","חמשה עשר","ששה עשר","שבעה עשר","שמונה עשר","תשעה עשר"],I=["","","עשרים","שלשים","ארבעים","חמישים","ששים","שבעים","שמונים","תשעים"],E="שקלים";function O(e,n){if(0===e)return"";const t=e%10,r=Math.trunc(e/10)%10,i=Math.trunc(e/100);let o="";if(i>0&&(o=a[i]),1===r){const e=c[t];o?o+=" "+n+e:o=e}else r>=2&&(o?o+=" "+n+f[r]:o=f[r]),t>0&&(o?o+=" "+n+u[t]:o=u[t]);return o}function R(e,n){if(0===e)return"";const t=e%10,r=Math.trunc(e/10)%10,i=Math.trunc(e/100);let o="";return i>0&&(o=a[i]),1===r?o?o+=" "+c[t]:o=c[t]:(r>=2&&(o?o+=" "+f[r]:o=f[r]),t>0&&(o?o+=" "+n+u[t]:o=u[t])),o}function B(e,n){if(0n===e)return d;if(e<1000n)return R(Number(e),n);const t=[];let r=e;for(;r>0n;)t.push(Number(r%1000n)),r/=1000n;let i="";for(let e=t.length-1;e>=0;e--){const r=t[e];if(0!==r)if(0===e){const e=R(r,n);i?i+=r<=9?" "+n+e:" "+e:i=e}else 1===e?r<=9?(i&&(i+=" "),i+=l[r]):(i&&(i+=" "),i+=O(r,n)+" "+g[1]):1===r?(i&&(i+=" "),i+=g[e]):(i&&(i+=" "),i+=O(r,n)+" "+p[e])}return i}const $={andWord:"ו"};function P(i,s){const{isNegative:c,integerPart:f,decimalPart:a}=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(n(i))}if("string"==typeof i)return r(function(n){const r=n.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid number format: "${n}"`);return t(r)?e(r):r}(i));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(i);o(f,b);const{andWord:l}=function(e,n){const t={...n};if(void 0===e)return t;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[i,o]of Object.entries(e)){if(!Object.hasOwn(n,i))throw new TypeError(`Unknown option "${i}" — expected one of: ${r.join(", ")}`);if(void 0!==o){if(typeof o!=typeof t[i])throw new TypeError(`Option "${i}" must be a ${typeof t[i]}, got ${typeof o}`);t[i]=o}}return t}(s,$);let g="";return c&&(g=w+" "),g+=B(f,l),a&&(g+=" נקודה "+function(e){let n="";for(let t=0;t<e.length;t++){const r=parseInt(e[t],10);n&&(n+=" "),n+=0===r?d:u[r]}return n}(a)),g}function j(e){if(0===e)return"";if(e<10)return v[e];if(e<20)return y[e-10];const n=e%10,t=Math.trunc(e/10);return 0===n?I[t]+" ו"+v[1]:I[t]+" ו"+v[n]}function x(e){if(0n===e)return"";if(1n===e)return v[1];if(e<100n)return j(Number(e));if(e<1000n){const n=Number(e/100n),t=Number(e%100n);let r;if(1===n){if(0===t)return"מאה";r=a[n]}else{if(0===t)return a[n];r=a[n]}return r+" "+j(t)}if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);if(0===t)return n<=9?l[n]:O(n,"ו")+" "+g[1];let r;if(r=n<=9?l[n]:O(n,"ו")+" "+g[1],t<100)return r+" "+j(t);const i=Math.trunc(t/100),o=t%100;return 0===o?r+" "+a[i]:r+" "+a[i]+" "+j(o)}const n=Number(e/1000000n),t=e%1000000n;if(0n===t)return 1===n?g[2]:O(n,"ו")+" "+p[2];let r;return r=1===n?g[2]:O(n,"ו")+" "+p[2],r+" "+x(t)}function M(n){const r=function(n){const r=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 r=n.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 n=e(r);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(r);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 ${r}`)}(n);return o(r,h),x(r)}function T(e){const{isNegative:t,dollars:r,cents:s}=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}:i(n(e))}if("string"==typeof e)return i(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);o(r,m);const l=[];if(t&&l.push(w),r>0n||0n===s)if(1n===r)l.push("שקל "+N[1]);else if(2n===r)l.push(N[2]+" "+E);else{const e=0n===(g=r)?d:g<1000n?function(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10)%10,r=Math.trunc(e/100);let i="";if(r>0&&(i=a[r]),1===t){const e=c[n];i?i+=" "+e:i=e}else t>=2&&(i?i+=" "+f[t]:i=f[t]),n>0&&(i?i+=" ו"+N[n]:i=N[n]);return i}(Number(g)):B(g,"ו");l.push(e+" "+E)}var g;if(s>0n)if(1===Number(s))l.push("אגורה "+u[1]);else{const e=B(s,"ו");l.push(e+" אגורות")}return l.join(" ")}export{$ as cardinalDefaults,b as cardinalMax,m as currencyMax,h as ordinalMax,P as toCardinal,T as toCurrency,M as toOrdinal};