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.54 kB
JavaScript
/*! n2words/ms-MY 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("."),u=-1===s?n:n.slice(0,s)+n.slice(s+1),o=(-1===s?n.length:s)+r;return o>=u.length?i+u+"0".repeat(o-u.length):o<=0?i+"0."+"0".repeat(-o)+u:i+u.slice(0,o)+"."+u.slice(o)}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 u=r.slice(0,s)||"0",o=(r.slice(s+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(u),cents:BigInt(o)}}function s(e,n,t){if(null===n)return;if(e<n&&!(t&&BigInt(t)>=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 o=["","satu","dua","tiga","empat","lima","enam","tujuh","lapan","sembilan"],a=["sepuluh","sebelas","dua belas","tiga belas","empat belas","lima belas","enam belas","tujuh belas","lapan belas","sembilan belas"],l=["","","dua puluh","tiga puluh","empat puluh","lima puluh","enam puluh","tujuh puluh","lapan puluh","sembilan puluh"],g="ratus",c="ribu",f=["juta","bilion","trilion"],m=u(f.length+1),b=u(f.length+1),h=u(f.length+1),p="sifar",d="minus";function w(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10)%10,r=Math.trunc(e/100),i=[];r>0&&i.push(1===r?"se"+g:o[r]+" "+g);const s=e%100;return 0===s||i.push(s<10?o[s]:s<20?a[s-10]:0===n?l[t]:l[t]+" "+o[n]),i.join(" ")}function N(e){if(0n===e)return p;if(e<1000n)return w(Number(e));if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);let r;return r=1===n?"se"+c:w(n)+" "+c,t>0&&(r+=" "+w(t)),r}return function(e){const n=e.toString(),t=n.length,r=[],i=t%3;let s=0;for(i>0&&(r.push(Number(n.slice(0,i))),s=i);s<t;)r.push(Number(n.slice(s,s+3))),s+=3;const u=[];let o=r.length-1;for(let e=0;e<r.length;e++){const n=r[e];if(0!==n)if(0===o)u.push(w(n));else if(1===o)u.push(1===n?"se"+c:w(n)+" "+c);else{const e=f[o-2];u.push(1===n?"se"+e:w(n)+" "+e)}o--}return u.join(" ")}(e)}function v(i){const{isNegative:u,integerPart:o,decimalPart:a}=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);s(o,m,a);let l="";return u&&(l=d+" "),l+=N(o),a&&(l+=" perpuluhan "+function(e){let n="",t=0;for(;t<e.length&&"0"===e[t];)n&&(n+=" "),n+=p,t++;const r=e.slice(t);return r&&(n&&(n+=" "),n+=N(BigInt(r))),n}(a)),l}function I(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 s(r,b),1n===(i=r)?"pertama":"ke"+N(i);var i}function y(e){const{isNegative:t,dollars:r,cents:u}=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);s(r,h);let o="";return t&&(o=d+" "),(r>0n||0n===u)&&(o+=N(r),o+=" ringgit"),u>0n&&(r>0n&&(o+=" "),o+=N(u),o+=" sen"),o}export{m as cardinalMax,h as currencyMax,b as ordinalMax,v as toCardinal,y as toCurrency,I as toOrdinal};