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.27 kB
JavaScript
/*! n2words/hbo-IL 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 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 t(e){const t=e.toString();return r(t)?n(t):t}function r(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 r=e.slice(0,t)||"0",i=e.slice(t+1);return{isNegative:n,integerPart:BigInt(r),decimalPart:i}}function o(e){let t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${e}"`);r(t)&&(t=n(t));const i="-"===t[0];i&&(t=t.slice(1));const o=t.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(t),cents:0n};const s=t.slice(0,o)||"0",u=(t.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function s(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 u(e){return 10n**BigInt(3*(e+1))}const f=["","אחד","שניים","שלשה","ארבעה","חמשה","ששה","שבעה","שמונה","תשעה"],c=["עשרה","אחד עשר","שנים עשר","שלשה עשר","ארבעה עשר","חמשה עשר","ששה עשר","שבעה עשר","שמונה עשר","תשעה עשר"],a=["","אלף","אלפיים","שלשה אלפים","ארבעה אלפים","חמשה אלפים","ששה אלפים","שבעה אלפים","שמונה אלפים","תשעה אלפים"],l=["","אחת","שתים","שלש","ארבע","חמש","שש","שבע","שמונה","תשע"],g=["עשר","אחת עשרה","שתים עשרה","שלש עשרה","ארבע עשרה","חמש עשרה","שש עשרה","שבע עשרה","שמונה עשרה","תשע עשרה"],d=["","אלף","אלפיים","שלשת אלפים","ארבעת אלפים","חמשת אלפים","ששת אלפים","שבעת אלפים","שמונת אלפים","תשעת אלפים"],p=["","","עשרים","שלשים","ארבעים","חמישים","ששים","שבעים","שמונים","תשעים"],b=["","מאה","מאתיים","שלשה מאות","ארבעה מאות","חמשה מאות","ששה מאות","שבעה מאות","שמונה מאות","תשעה מאות"],m=["","מאה","מאתיים","שלש מאות","ארבע מאות","חמש מאות","שש מאות","שבע מאות","שמונה מאות","תשע מאות"],h=["","אלף","מיליון","מיליארד","טריליון","קוודרליון","קווינטיליון"],w=["","אלפים","מיליונים","מיליארדים","טריליונים","קוודרליונים","קווינטיליונים"],N=u(h.length-1),y=10n**BigInt(9),v=u(h.length-1),I="מינוס",E=["","ראשון","שני","שלישי","רביעי","חמישי","שישי","שביעי","שמיני","תשיעי"],O=["עשירי","אחד עשר","שנים עשר","שלשה עשר","ארבעה עשר","חמשה עשר","ששה עשר","שבעה עשר","שמונה עשר","תשעה עשר"],R=["","","עשרים","שלשים","ארבעים","חמישים","ששים","שבעים","שמונים","תשעים"],$="שקלים";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 f="";if(u>0&&(f=i[u]),1===s){const e=r[o];f?f+=" "+n+e:f=e}else s>=2&&(f?f+=" "+n+p[s]:f=p[s]),o>0&&(f?f+=" "+n+t[o]:f=t[o]);return f}function j(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 f="";return u>0&&(f=i[u]),1===s?f?f+=" "+r[o]:f=r[o]:(s>=2&&(f?f+=" "+p[s]:f=p[s]),o>0&&(f?f+=" "+n+t[o]:f=t[o])),f}function x(e,n,t){if(0n===e)return"אפס";const r="feminine"===n,i=r?l:f,o=r?g:c,s=r?d:a,u=r?m:b;if(e<1000n)return j(Number(e),t,i,o,u);const p=[];let N=e;for(;N>0n;)p.push(Number(N%1000n)),N/=1000n;let y="";for(let e=p.length-1;e>=0;e--){const n=p[e];if(0!==n)if(0===e){const e=j(n,t,i,o,u);y?y+=n<=9?" "+t+e:" "+e:y=e}else 1===e?n<=9?(y&&(y+=" "),y+=s[n]):(y&&(y+=" "),y+=B(n,t,i,o,u)+" "+h[1]):1===n?(y&&(y+=" "),y+=h[e]):(y&&(y+=" "),y+=B(n,t,i,o,u)+" "+w[e])}return y}const P={gender:"masculine",andWord:"ו"},T={gender:["masculine","feminine"]};function M(e){if(0===e)return"";if(e<10)return E[e];if(e<20)return O[e-10];const n=e%10,t=Math.trunc(e/10);return 0===n?R[t]+" ו"+E[1]:R[t]+" ו"+E[n]}function S(e){if(0n===e)return"";if(1n===e)return E[1];if(e<100n)return M(Number(e));if(e<1000n){const n=Number(e/100n),t=Number(e%100n);return 0===t?"מאה":b[n]+" "+M(t)}if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);if(0===t)return n<=9?a[n]:B(n,"ו",f,c,b)+" "+h[1];let r;if(r=n<=9?a[n]:B(n,"ו",f,c,b)+" "+h[1],t<100)return r+" "+M(t);const i=Math.trunc(t/100),o=t%100;return 0===o?r+" "+b[i]:r+" "+b[i]+" "+M(o)}const n=Number(e/1000000n),t=e%1000000n;if(0n===t)return 1===n?h[2]:B(n,"ו",f,c,b)+" "+w[2];let r;return r=1===n?h[2]:B(n,"ו",f,c,b)+" "+w[2],r+" "+S(t)}const L={hboIL: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(r(t)){const e=n(t);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(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 s(t,y),S(t)}};e.currency={hboIL:function(e){const{isNegative:n,dollars:r,cents:i}=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}:o(t(e))}if("string"==typeof e)return o(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${n}`)}(e);s(r,v);const u=[];if(n&&u.push(I),r>0n||0n===i)if(1n===r)u.push("שקל "+f[1]);else if(2n===r)u.push(f[2]+" "+$);else{const e=x(r,"masculine","ו");u.push(e+" "+$)}if(i>0n)if(1===Number(i))u.push("גרה "+l[1]);else{const e=x(i,"feminine","ו");u.push(e+" גרות")}return u.join(" ")}},e.hboIL=function(e,o){const{isNegative:u,integerPart:c,decimalPart:a}=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(t(e))}if("string"==typeof e)return i(function(e){const t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${e}"`);return r(t)?n(t):t}(e));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(e);s(c,N);const{gender:g,andWord:d}=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}(o,P,T);let p="";return u&&(p=I+" "),p+=x(c,g,d),a&&(p+=" נקודה "+function(e,n){const t="feminine"===n?l:f;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}(a,g)),p},e.ordinal=L},"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||{});