n2words
Version:
Convert numbers to words in 70+ languages with zero dependencies. Supports BigInt, decimals, and browser/Node.js environments.
3 lines (2 loc) • 8.24 kB
JavaScript
/*! n2words/kn-IN 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 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 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 o=r.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(r),cents:0n};const s=r.slice(0,o)||"0",u=(r.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function o(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 s(e){return 10n**BigInt(3+2*(e-1))}const u="ಸೊನ್ನೆ",a="ಋಣಾತ್ಮಕ",c="ನೂರು",g=["","ಮೊದಲನೇ","ಎರಡನೇ","ಮೂರನೇ","ನಾಲ್ಕನೇ","ಐದನೇ","ಆರನೇ"],f=["ಸೊನ್ನೆ","ಒಂದು","ಎರಡು","ಮೂರು","ನಾಲ್ಕು","ಐದು","ಆರು","ಏಳು","ಎಂಟು","ಒಂಬತ್ತು","ಹತ್ತು","ಹನ್ನೊಂದು","ಹನ್ನೆರಡು","ಹದಿಮೂರು","ಹದಿನಾಲ್ಕು","ಹದಿನೈದು","ಹದಿನಾರು","ಹದಿನೇಳು","ಹದಿನೆಂಟು","ಹತ್ತೊಂಬತ್ತು","ಇಪ್ಪತ್ತು","ಇಪ್ಪತ್ತೊಂದು","ಇಪ್ಪತ್ತೆರಡು","ಇಪ್ಪತ್ತಮೂರು","ಇಪ್ಪತ್ತನಾಲ್ಕು","ಇಪ್ಪತ್ತೈದು","ಇಪ್ಪತ್ತಾರು","ಇಪ್ಪತ್ತೇಳು","ಇಪ್ಪತ್ತೆಂಟು","ಇಪ್ಪತ್ತೊಂಬತ್ತು","ಮೂವತ್ತು","ಮೂವತ್ತೊಂದು","ಮೂವತ್ತೆರಡು","ಮೂವತ್ತಮೂರು","ಮೂವತ್ತನಾಲ್ಕು","ಮೂವತ್ತೈದು","ಮೂವತ್ತಾರು","ಮೂವತ್ತೇಳು","ಮೂವತ್ತೆಂಟು","ಮೂವತ್ತೊಂಬತ್ತು","ನಲವತ್ತು","ನಲವತ್ತೊಂದು","ನಲವತ್ತೆರಡು","ನಲವತ್ತಮೂರು","ನಲವತ್ತನಾಲ್ಕು","ನಲವತ್ತೈದು","ನಲವತ್ತಾರು","ನಲವತ್ತೇಳು","ನಲವತ್ತೆಂಟು","ನಲವತ್ತೊಂಬತ್ತು","ಐವತ್ತು","ಐವತ್ತೊಂದು","ಐವತ್ತೆರಡು","ಐವತ್ತಮೂರು","ಐವತ್ತನಾಲ್ಕು","ಐವತ್ತೈದು","ಐವತ್ತಾರು","ಐವತ್ತೇಳು","ಐವತ್ತೆಂಟು","ಐವತ್ತೊಂಬತ್ತು","ಅರವತ್ತು","ಅರವತ್ತೊಂದು","ಅರವತ್ತೆರಡು","ಅರವತ್ತಮೂರು","ಅರವತ್ತನಾಲ್ಕು","ಅರವತ್ತೈದು","ಅರವತ್ತಾರು","ಅರವತ್ತೇಳು","ಅರವತ್ತೆಂಟು","ಅರವತ್ತೊಂಬತ್ತು","ಎಪ್ಪತ್ತು","ಎಪ್ಪತ್ತೊಂದು","ಎಪ್ಪತ್ತೆರಡು","ಎಪ್ಪತ್ತಮೂರು","ಎಪ್ಪತ್ತನಾಲ್ಕು","ಎಪ್ಪತ್ತೈದು","ಎಪ್ಪತ್ತಾರು","ಎಪ್ಪತ್ತೇಳು","ಎಪ್ಪತ್ತೆಂಟು","ಎಪ್ಪತ್ತೊಂಬತ್ತು","ಎಂಬತ್ತು","ಎಂಬತ್ತೊಂದು","ಎಂಬತ್ತೆರಡು","ಎಂಬತ್ತಮೂರು","ಎಂಬತ್ತನಾಲ್ಕು","ಎಂಬತ್ತೈದು","ಎಂಬತ್ತಾರು","ಎಂಬತ್ತೇಳು","ಎಂಬತ್ತೆಂಟು","ಎಂಬತ್ತೊಂಬತ್ತು","ತೊಂಬತ್ತು","ತೊಂಬತ್ತೊಂದು","ತೊಂಬತ್ತೆರಡು","ತೊಂಬತ್ತಮೂರು","ತೊಂಬತ್ತನಾಲ್ಕು","ತೊಂಬತ್ತೈದು","ತೊಂಬತ್ತಾರು","ತೊಂಬತ್ತೇಳು","ತೊಂಬತ್ತೆಂಟು","ತೊಂಬತ್ತೊಂಬತ್ತು"],l=["","ಸಾವಿರ","ಲಕ್ಷ","ಕೋಟಿ","ಅಬ್ಜ","ಖರ್ವ","ನೀಲ","ಪದ್ಮ","ಶಂಖ"],m=s(l.length),b=s(l.length),w=s(l.length);function d(e){if(0===e)return"";if(e<100)return f[e];const n=Math.trunc(e/100),t=e%100;return 0===t?f[n]+" "+c:f[n]+" "+c+" "+f[t]}function h(e){if(0n===e)return u;if(e<1000n)return d(Number(e));const n=[];n.push(Number(e%1000n));let t=e/1000n;for(;t>0n;)n.push(Number(t%100n)),t/=100n;const r=[];for(let e=n.length-1;e>=0;e--){const t=n[e];0!==t&&(r.push(0===e?d(t):f[t]),e>0&&l[e]&&r.push(l[e]))}return r.join(" ")}function p(i){const{isNegative:s,integerPart:c,decimalPart:g}=function(i){const o=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 ${o}`)}(i);o(c,m);let l="";return s&&(l=a+" "),l+=h(c),g&&(l+=" ದಶಮಾಂಶ "+function(e){const n=[];for(const t of e){const e=parseInt(t,10);n.push(0===e?u:f[e])}return n.join(" ")}(g)),l}function N(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 o(r,b),(i=r)>=1n&&i<=6n?g[Number(i)]:h(i)+"ನೇ";var i}function v(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);o(r,w);let u="";return t&&(u=a+" "),(r>0n||0n===s)&&(u+=h(r),u+=" "+(1n===r?"ರೂಪಾಯಿ":"ರೂಪಾಯಿಗಳು")),s>0n&&(r>0n&&(u+=" "),u+=h(s),u+=" "+(1n===s?"ಪೈಸೆ":"ಪೈಸೆಗಳು")),u}export{m as cardinalMax,w as currencyMax,b as ordinalMax,p as toCardinal,v as toCurrency,N as toOrdinal};