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.43 kB
JavaScript
/*! n2words/kn-IN v5.1.2 | MIT License | github.com/forzagreen/n2words */
var e,n;e=this,n=function(e){"use strict";function n(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 t(e){const t=e.toString();return r(t)?n(t):t}function r(e){return e.includes("e")||e.includes("E")}function i(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 o(e){let t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${e}"`);r(t)&&(t=n(t));const i="-"===t[0];i&&(t=t.slice(1));const o=t.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(t),cents:0n};const s=t.slice(0,o)||"0",u=(t.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function s(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 u(e){return 10n**BigInt(3+2*(e-1))}const a="ಸೊನ್ನೆ",c="ಋಣಾತ್ಮಕ",f="ನೂರು",g=["","ಮೊದಲನೇ","ಎರಡನೇ","ಮೂರನೇ","ನಾಲ್ಕನೇ","ಐದನೇ","ಆರನೇ"],l=["ಸೊನ್ನೆ","ಒಂದು","ಎರಡು","ಮೂರು","ನಾಲ್ಕು","ಐದು","ಆರು","ಏಳು","ಎಂಟು","ಒಂಬತ್ತು","ಹತ್ತು","ಹನ್ನೊಂದು","ಹನ್ನೆರಡು","ಹದಿಮೂರು","ಹದಿನಾಲ್ಕು","ಹದಿನೈದು","ಹದಿನಾರು","ಹದಿನೇಳು","ಹದಿನೆಂಟು","ಹತ್ತೊಂಬತ್ತು","ಇಪ್ಪತ್ತು","ಇಪ್ಪತ್ತೊಂದು","ಇಪ್ಪತ್ತೆರಡು","ಇಪ್ಪತ್ತಮೂರು","ಇಪ್ಪತ್ತನಾಲ್ಕು","ಇಪ್ಪತ್ತೈದು","ಇಪ್ಪತ್ತಾರು","ಇಪ್ಪತ್ತೇಳು","ಇಪ್ಪತ್ತೆಂಟು","ಇಪ್ಪತ್ತೊಂಬತ್ತು","ಮೂವತ್ತು","ಮೂವತ್ತೊಂದು","ಮೂವತ್ತೆರಡು","ಮೂವತ್ತಮೂರು","ಮೂವತ್ತನಾಲ್ಕು","ಮೂವತ್ತೈದು","ಮೂವತ್ತಾರು","ಮೂವತ್ತೇಳು","ಮೂವತ್ತೆಂಟು","ಮೂವತ್ತೊಂಬತ್ತು","ನಲವತ್ತು","ನಲವತ್ತೊಂದು","ನಲವತ್ತೆರಡು","ನಲವತ್ತಮೂರು","ನಲವತ್ತನಾಲ್ಕು","ನಲವತ್ತೈದು","ನಲವತ್ತಾರು","ನಲವತ್ತೇಳು","ನಲವತ್ತೆಂಟು","ನಲವತ್ತೊಂಬತ್ತು","ಐವತ್ತು","ಐವತ್ತೊಂದು","ಐವತ್ತೆರಡು","ಐವತ್ತಮೂರು","ಐವತ್ತನಾಲ್ಕು","ಐವತ್ತೈದು","ಐವತ್ತಾರು","ಐವತ್ತೇಳು","ಐವತ್ತೆಂಟು","ಐವತ್ತೊಂಬತ್ತು","ಅರವತ್ತು","ಅರವತ್ತೊಂದು","ಅರವತ್ತೆರಡು","ಅರವತ್ತಮೂರು","ಅರವತ್ತನಾಲ್ಕು","ಅರವತ್ತೈದು","ಅರವತ್ತಾರು","ಅರವತ್ತೇಳು","ಅರವತ್ತೆಂಟು","ಅರವತ್ತೊಂಬತ್ತು","ಎಪ್ಪತ್ತು","ಎಪ್ಪತ್ತೊಂದು","ಎಪ್ಪತ್ತೆರಡು","ಎಪ್ಪತ್ತಮೂರು","ಎಪ್ಪತ್ತನಾಲ್ಕು","ಎಪ್ಪತ್ತೈದು","ಎಪ್ಪತ್ತಾರು","ಎಪ್ಪತ್ತೇಳು","ಎಪ್ಪತ್ತೆಂಟು","ಎಪ್ಪತ್ತೊಂಬತ್ತು","ಎಂಬತ್ತು","ಎಂಬತ್ತೊಂದು","ಎಂಬತ್ತೆರಡು","ಎಂಬತ್ತಮೂರು","ಎಂಬತ್ತನಾಲ್ಕು","ಎಂಬತ್ತೈದು","ಎಂಬತ್ತಾರು","ಎಂಬತ್ತೇಳು","ಎಂಬತ್ತೆಂಟು","ಎಂಬತ್ತೊಂಬತ್ತು","ತೊಂಬತ್ತು","ತೊಂಬತ್ತೊಂದು","ತೊಂಬತ್ತೆರಡು","ತೊಂಬತ್ತಮೂರು","ತೊಂಬತ್ತನಾಲ್ಕು","ತೊಂಬತ್ತೈದು","ತೊಂಬತ್ತಾರು","ತೊಂಬತ್ತೇಳು","ತೊಂಬತ್ತೆಂಟು","ತೊಂಬತ್ತೊಂಬತ್ತು"],d=["","ಸಾವಿರ","ಲಕ್ಷ","ಕೋಟಿ","ಅಬ್ಜ","ಖರ್ವ","ನೀಲ","ಪದ್ಮ","ಶಂಖ"],b=u(d.length),m=u(d.length),p=u(d.length);function h(e){if(0===e)return"";if(e<100)return l[e];const n=Math.trunc(e/100),t=e%100;return 0===t?l[n]+" "+f:l[n]+" "+f+" "+l[t]}function w(e){if(0n===e)return a;if(e<1000n)return h(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?h(t):l[t]),e>0&&d[e]&&r.push(d[e]))}return r.join(" ")}const N={knIN:function(e){const t=function(e){const t=typeof e;if("bigint"==typeof e){if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}if("number"==typeof e){if(!Number.isFinite(e))throw new RangeError("Ordinals must be finite numbers");if(!Number.isInteger(e))throw new RangeError("Ordinals must be whole numbers");if(e<=0)throw new RangeError("Ordinals must be positive integers");return BigInt(e)}if("string"==typeof e)return function(e){const t=e.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 e=n(t);if(e.includes("."))throw new RangeError("Ordinals must be whole numbers");const r=BigInt(e);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(n){if(n instanceof RangeError)throw n;throw new RangeError(`Invalid ordinal format: "${e}"`,{cause:n})}}(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);return s(t,m),(i=t)>=1n&&i<=6n?g[Number(i)]:w(i)+"ನೇ";var i}};e.currency={knIN:function(e){const{isNegative:n,dollars:r,cents:i}=function(e){const n=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}:o(t(e))}if("string"==typeof e)return o(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${n}`)}(e);s(r,p);let u="";return n&&(u=c+" "),(r>0n||0n===i)&&(u+=w(r),u+=" "+(1n===r?"ರೂಪಾಯಿ":"ರೂಪಾಯಿಗಳು")),i>0n&&(r>0n&&(u+=" "),u+=w(i),u+=" "+(1n===i?"ಪೈಸೆ":"ಪೈಸೆಗಳು")),u}},e.knIN=function(e){const{isNegative:o,integerPart:u,decimalPart:f}=function(e){const o=typeof e;if("bigint"==typeof e)return e<0n?{isNegative:!0,integerPart:-e}:{isNegative:!1,integerPart:e};if("number"==typeof e){if(!Number.isFinite(e))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(e)?e<0?{isNegative:!0,integerPart:BigInt(-e)}:{isNegative:!1,integerPart:BigInt(e)}:i(t(e))}if("string"==typeof e)return i(function(e){const t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${e}"`);return r(t)?n(t):t}(e));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(e);s(u,b);let g="";return o&&(g=c+" "),g+=w(u),f&&(g+=" ದಶಮಾಂಶ "+function(e){const n=[];for(const t of e){const e=parseInt(t,10);n.push(0===e?a:l[e])}return n.join(" ")}(f)),g},e.ordinal=N},"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).n2words=e.n2words||{});