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) 9.12 kB
/*! n2words/hbo-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=["","מאה","מאתיים","שלשה מאות","ארבעה מאות","חמשה מאות","ששה מאות","שבעה מאות","שמונה מאות","תשעה מאות"],m=["","מאה","מאתיים","שלש מאות","ארבע מאות","חמש מאות","שש מאות","שבע מאות","שמונה מאות","תשע מאות"],d=["","אלף","מיליון","מיליארד","טריליון","קוודרליון","קווינטיליון"],h=["","אלפים","מיליונים","מיליארדים","טריליונים","קוודרליונים","קווינטיליונים"],w=s(d.length-1),N=10n**BigInt(9),v=s(d.length-1),y="מינוס",I=["","ראשון","שני","שלישי","רביעי","חמישי","שישי","שביעי","שמיני","תשיעי"],E=["עשירי","אחד עשר","שנים עשר","שלשה עשר","ארבעה עשר","חמשה עשר","ששה עשר","שבעה עשר","שמונה עשר","תשעה עשר"],O=["","","עשרים","שלשים","ארבעים","חמישים","ששים","שבעים","שמונים","תשעים"],R="שקלים";function $(e,n,t,r,i){if(0===e)return"";const o=e%10,s=Math.trunc(e/10)%10,u=Math.trunc(e/100);let c="";if(u>0&&(c=i[u]),1===s){const e=r[o];c?c+=" "+n+e:c=e}else s>=2&&(c?c+=" "+n+p[s]:c=p[s]),o>0&&(c?c+=" "+n+t[o]:c=t[o]);return c}function B(e,n,t,r,i){if(0===e)return"";const o=e%10,s=Math.trunc(e/10)%10,u=Math.trunc(e/100);let c="";return u>0&&(c=i[u]),1===s?c?c+=" "+r[o]:c=r[o]:(s>=2&&(c?c+=" "+p[s]:c=p[s]),o>0&&(c?c+=" "+n+t[o]:c=t[o])),c}function j(e,n,t){if(0n===e)return"אפס";const r="feminine"===n,i=r?a:u,o=r?l:c,s=r?g:f,p=r?m:b;if(e<1000n)return B(Number(e),t,i,o,p);const w=[];let N=e;for(;N>0n;)w.push(Number(N%1000n)),N/=1000n;let v="";for(let e=w.length-1;e>=0;e--){const n=w[e];if(0!==n)if(0===e){const e=B(n,t,i,o,p);v?v+=n<=9?" "+t+e:" "+e:v=e}else 1===e?n<=9?(v&&(v+=" "),v+=s[n]):(v&&(v+=" "),v+=$(n,t,i,o,p)+" "+d[1]):1===n?(v&&(v+=" "),v+=d[e]):(v&&(v+=" "),v+=$(n,t,i,o,p)+" "+h[e])}return v}const P={gender:"masculine",andWord:"ו"},x={gender:["masculine","feminine"]};function M(i,s){const{isNegative:c,integerPart:f,decimalPart:l}=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,w);const{gender:g,andWord:p}=function(e,n,t){const r={...n};if(void 0===e)return r;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 i=Object.keys(n);for(const[o,s]of Object.entries(e)){if(!Object.hasOwn(n,o))throw new TypeError(`Unknown option "${o}" — expected one of: ${i.join(", ")}`);if(void 0!==s){if(typeof s!=typeof r[o])throw new TypeError(`Option "${o}" must be a ${typeof r[o]}, got ${typeof s}`);if(void 0!==t&&Object.hasOwn(t,o)){const e=t[o];if(!e.includes(s))throw new RangeError(`Option "${o}" must be one of: ${e.join(", ")} — got ${JSON.stringify(s)}`)}r[o]=s}}return r}(s,P,x);let b="";return c&&(b=y+" "),b+=j(f,g,p),l&&(b+=" נקודה "+function(e,n){const t="feminine"===n?a:u;let r="";for(let n=0;n<e.length;n++){const i=parseInt(e[n],10);r&&(r+=" "),r+=0===i?"אפס":t[i]}return r}(l,g)),b}function T(e){if(0===e)return"";if(e<10)return I[e];if(e<20)return E[e-10];const n=e%10,t=Math.trunc(e/10);return 0===n?O[t]+" ו"+I[1]:O[t]+" ו"+I[n]}function S(e){if(0n===e)return"";if(1n===e)return I[1];if(e<100n)return T(Number(e));if(e<1000n){const n=Number(e/100n),t=Number(e%100n);return 0===t?"מאה":b[n]+" "+T(t)}if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);if(0===t)return n<=9?f[n]:$(n,"ו",u,c,b)+" "+d[1];let r;if(r=n<=9?f[n]:$(n,"ו",u,c,b)+" "+d[1],t<100)return r+" "+T(t);const i=Math.trunc(t/100),o=t%100;return 0===o?r+" "+b[i]:r+" "+b[i]+" "+T(o)}const n=Number(e/1000000n),t=e%1000000n;if(0n===t)return 1===n?d[2]:$(n,"ו",u,c,b)+" "+h[2];let r;return r=1===n?d[2]:$(n,"ו",u,c,b)+" "+h[2],r+" "+S(t)}function F(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,N),S(r)}function k(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,v);const c=[];if(t&&c.push(y),r>0n||0n===s)if(1n===r)c.push("שקל "+u[1]);else if(2n===r)c.push(u[2]+" "+R);else{const e=j(r,"masculine","ו");c.push(e+" "+R)}if(s>0n)if(1===Number(s))c.push("גרה "+a[1]);else{const e=j(s,"feminine","ו");c.push(e+" גרות")}return c.join(" ")}export{P as cardinalDefaults,w as cardinalMax,x as cardinalValues,v as currencyMax,N as ordinalMax,M as toCardinal,k as toCurrency,F as toOrdinal};