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) 4.97 kB
/*! n2words/th-TH 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 s=n.indexOf("."),o=-1===s?n:n.slice(0,s)+n.slice(s+1),u=(-1===s?n.length:s)+r;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 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 s=r.indexOf(".");if(-1===s)return{isNegative:i,dollars:BigInt(r),cents:0n};const o=r.slice(0,s)||"0",u=(r.slice(s+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(o),cents:BigInt(u)}}const s=null,o=s,u=s,c=s,a=["หนึ่ง","สอง","สาม","สี่","ห้า","หก","เจ็ด","แปด","เก้า"],g="ศูนย์";function f(e){if(0===e)return"";let n=e;const t=[],r=Math.trunc(n/1e5);n%=1e5;const i=Math.trunc(n/1e4);n%=1e4;const s=Math.trunc(n/1e3);n%=1e3;const o=Math.trunc(n/100);n%=100;const u=Math.trunc(n/10),c=n%10;return r>0&&t.push(a[r-1]+"แสน"),i>0&&t.push(1===i?"หนึ่งหมื่น":a[i-1]+"หมื่น"),s>0&&t.push(a[s-1]+"พัน"),o>0&&t.push(a[o-1]+"ร้อย"),u>0&&t.push(1===u?"สิบ":2===u?"ยี่สิบ":a[u-1]+"สิบ"),c>0&&t.push(1===c&&(u>0||r>0||i>0||s>0||o>0||u>0)?"เอ็ด":a[c-1]),t.join("")}function l(e){if(0n===e)return g;const n=function(e){const n=[];let t=e;const r=1000000n;for(;t>0n;){const e=Number(t%r);n.unshift(e),t/=r}return n}(e),t=[];for(let e=0;e<n.length;e++){const r=n[e];if(0===r)continue;t.push(f(r));const i=n.length-e-1;i>0&&t.push("ล้าน".repeat(i))}return t.join("")}function h(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)}: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 ${s}`)}(i);let c="";return s&&(c="ลบ"),c+=l(o),u&&(c+="จุด"+function(e){const n=[];for(const t of e){const e=parseInt(t,10);n.push(0===e?g:a[e-1])}return n.join("")}(u)),c}function p(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"ที่"+l(r)}function m(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);let o="";return t&&(o="ลบ"),o+=l(r),o+="บาท",s>0n?(o+=l(s),o+="สตางค์"):o+="ถ้วน",o}export{o as cardinalMax,c as currencyMax,u as ordinalMax,h as toCardinal,m as toCurrency,p as toOrdinal};