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.28 kB
JavaScript
/*! n2words/ko-KR 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)}}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 o(e){return 10n**BigInt(4*(e+1))}const u=["","일","이","삼","사","오","육","칠","팔","구"],c="마이너스",a=["만","억","조","경","해","자","양"],l=o(a.length),g=o(a.length),f=o(a.length);function h(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10)%10,r=Math.trunc(e/100)%10,i=Math.trunc(e/1e3);let s="";return i>0&&(s+=1===i?"천":u[i]+"천"),r>0&&(s+=1===r?"백":u[r]+"백"),t>0&&(s+=1===t?"십":u[t]+"십"),n>0&&(s+=u[n]),s}function p(e){return 0n===e?"영":e<10000n?h(Number(e)):function(e){const n=e.toString(),t=n.length,r=[],i=t%4;let s=0;for(i>0&&(r.push(Number(n.slice(0,i))),s=i);s<t;)r.push(Number(n.slice(s,s+4))),s+=4;const o=[];let u=r.length-1;for(let e=0;e<r.length;e++){const n=r[e];if(0!==n)if(0===u)o.push({word:h(n),isScale:!1});else{const e=a[u-1];1===n||o.push({word:h(n),isScale:!1}),o.push({word:e,isScale:!0})}u--}return function(e){if(0===e.length)return"영";if(1===e.length)return e[0].word;const n=[];for(let t=0;t<e.length;t++){const r=e[t],i=t>0?e[t-1]:null;i&&i.isScale&&!r.isScale&&n.push(" "),n.push(r.word)}return n.join("")}(o)}(e)}function w(i){const{isNegative:o,integerPart:u,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(u,l,a);const g=[];return o&&g.push(c),g.push(p(u)),a&&(g.push("점"),g.push(function(e){const n=[];let t=0;for(;t<e.length&&"0"===e[t];)n.push("영"),t++;const r=e.slice(t);return r&&n.push(p(BigInt(r))),n.join(" ")}(a))),g.join(" ")}function d(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,g),"제"+p(r)}function m(e){const{isNegative:t,dollars:r}=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,f);let o="";return t&&(o=c+" "),o+=p(r),o+="원",o}export{l as cardinalMax,f as currencyMax,g as ordinalMax,w as toCardinal,m as toCurrency,d as toOrdinal};