n2words
Version:
Convert numbers to words in 70+ languages with zero dependencies. Supports BigInt, decimals, and browser/Node.js environments.
3 lines (2 loc) • 5.2 kB
JavaScript
/*! n2words/fa-IR v5.1.2 | MIT License | github.com/forzagreen/n2words */
function e(e){let[n,r]=e.toLowerCase().split("e");const t=parseInt(r,10);let i="";"-"===n[0]&&(i="-",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)+t;return u>=o.length?i+o+"0".repeat(u-o.length):u<=0?i+"0."+"0".repeat(-u)+o:i+o.slice(0,u)+"."+o.slice(u)}function n(n){const t=n.toString();return r(t)?e(t):t}function r(e){return e.includes("e")||e.includes("E")}function t(e){const n="-"===e[0];n&&(e=e.slice(1));const r=e.indexOf(".");if(-1===r)return{isNegative:n,integerPart:BigInt(e)};const t=e.slice(0,r)||"0",i=e.slice(r+1);return{isNegative:n,integerPart:BigInt(t),decimalPart:i}}function i(n){let t=n.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${n}"`);r(t)&&(t=e(t));const i="-"===t[0];i&&(t=t.slice(1));const s=t.indexOf(".");if(-1===s)return{isNegative:i,dollars:BigInt(t),cents:0n};const o=t.slice(0,s)||"0",u=(t.slice(s+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(o),cents:BigInt(u)}}const s=null,o=s,u=s,a=s,c={1:"یک",2:"دو",3:"سه",4:"چهار",5:"پنج",6:"شش",7:"هفت",8:"هشت",9:"نه"},g={10:"ده",11:"یازده",12:"دوازده",13:"سیزده",14:"چهارده",15:"پانزده",16:"شانزده",17:"هفده",18:"هجده",19:"نوزده"},f={20:"بیست",30:"سی",40:"چهل",50:"پنجاه",60:"شصت",70:"هفتاد",80:"هشتاد",90:"نود"},l={100:"صد",200:"دویست",300:"سيصد",400:"چهار صد",500:"پانصد",600:"ششصد",700:"هفتصد",800:"هشتصد",900:"نهصد"},m="منفى",b={1:"اول",2:"دوم",3:"سوم",4:"چهارم",5:"پنجم",6:"ششم",7:"هفتم",8:"هشتم",9:"نهم"};function d(e){if(0n===e)return"صفر";if(e<=9n)return c[Number(e)];if(e<=19n)return g[Number(e)];if(e<100n){const n=e%10n,r=e-n;return 0n===n?f[Number(r)]:`${f[Number(r)]} و ${c[Number(n)]}`}if(e<1000n){const n=e/100n*100n,r=e-n;return 0n===r?l[Number(n)]:`${l[Number(n)]} و ${d(r)}`}if(e<1000000n){const n=e/1000n,r=e%1000n;return`${1n===n?"":d(n)+" "}هزار${0n===r?"":" "+d(r)}`}if(e<1000000000n){const n=e%1000000n;return`${d(e/1000000n)+" میلیون"}${0n===n?"":" و "+d(n)}`}const n=e%1000000000n;return`${d(e/1000000000n)+" میلیارد"}${0n===n?"":" و "+d(n)}`}function w(i){const{isNegative:s,integerPart:o,decimalPart:u}=function(i){const s=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)}:t(n(i))}if("string"==typeof i)return t(function(n){const t=n.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${n}"`);return r(t)?e(t):t}(i));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${s}`)}(i);let a="";return s&&(a=m+" "),a+=d(o),u&&(a+=" ممیّز "+function(e){let n="",r=0;for(;r<e.length&&"0"===e[r];)n&&(n+=" "),n+="صفر",r++;const t=e.slice(r);return t&&(n&&(n+=" "),n+=d(BigInt(t))),n}(u)),a}function N(n){const t=function(n){const t=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 t=n.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 n=e(t);if(n.includes("."))throw new RangeError("Ordinals must be whole numbers");const r=BigInt(n);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(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 ${t}`)}(n);return(i=t)>=1n&&i<=9n?b[Number(i)]:d(i)+"م";var i}function p(e){const{isNegative:r,dollars:t}=function(e){const r=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 ${r}`)}(e);let s="";return r&&(s=m+" "),s+=d(t),s+=" ریال",s}export{o as cardinalMax,a as currencyMax,u as ordinalMax,w as toCardinal,p as toCurrency,N as toOrdinal};