n2words
Version:
Convert numbers to words in 70+ languages with zero dependencies. Supports BigInt, decimals, and browser/Node.js environments.
3 lines (2 loc) • 6.52 kB
JavaScript
/*! n2words/tr-TR v5.1.2 | MIT License | github.com/forzagreen/n2words */
function n(n){let[e,t]=n.toLowerCase().split("e");const r=parseInt(t,10);let i="";"-"===e[0]&&(i="-",e=e.slice(1));const o=e.indexOf("."),s=-1===o?e:e.slice(0,o)+e.slice(o+1),u=(-1===o?e.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 e(e){const r=e.toString();return t(r)?n(r):r}function t(n){return n.includes("e")||n.includes("E")}function r(n){const e="-"===n[0];e&&(n=n.slice(1));const t=n.indexOf(".");if(-1===t)return{isNegative:e,integerPart:BigInt(n)};const r=n.slice(0,t)||"0",i=n.slice(t+1);return{isNegative:e,integerPart:BigInt(r),decimalPart:i}}function i(e){let r=e.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid currency format: "${e}"`);t(r)&&(r=n(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(n,e,t){if(null===e)return;if(n<e&&!(t&&BigInt(t)>=e))return;const r=e.toString().length-1,i=e===10n**BigInt(r)?`10^${r} - 1`:""+(e-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${i}`)}function s(n){return 10n**BigInt(3*(n+1))}const u=["","bir","iki","üç","dört","beş","altı","yedi","sekiz","dokuz"],c=["on","on bir","on iki","on üç","on dört","on beş","on altı","on yedi","on sekiz","on dokuz"],l=["","","yirmi","otuz","kırk","elli","altmış","yetmiş","seksen","doksan"],a="bin",f="sıfır",g="eksi",d=["milyon","milyar","trilyon","katrilyon","kentilyon"],b=s(d.length+1),p=s(d.length+1),m=s(d.length+1),h={1:"birinci",2:"ikinci",3:"üçüncü",4:"dördüncü",5:"beşinci",6:"altıncı",7:"yedinci",8:"sekizinci",9:"dokuzuncu",10:"onuncu"};function w(n,e=" "){if(0===n)return"";const t=n%10,r=Math.trunc(n/10)%10,i=Math.trunc(n/100),o=[];i>0&&o.push(1===i?"yüz":u[i]+e+"yüz");const s=n%100;return 0===s||o.push(s<10?u[t]:s<20?c[t].replace(" ",e):0===t?l[r]:l[r]+e+u[t]),o.join(e)}function y(n,e){if(0n===n)return f;const t=e?"":" ";if(n<1000n)return w(Number(n),t);if(n<1000000n){const e=Number(n/1000n),r=Number(n%1000n);let i;return i=1===e?a:w(e,t)+t+a,r>0&&(i+=t+w(r,t)),i}return function(n,e){const t=e?"":" ",r=n.toString(),i=r.length,o=[],s=i%3;let u=0;for(s>0&&(o.push(Number(r.slice(0,s))),u=s);u<i;)o.push(Number(r.slice(u,u+3))),u+=3;const c=[];let l=o.length-1;for(let n=0;n<o.length;n++){const e=o[n];if(0!==e){const n=w(e,t);c.push(0===l?n:1===l?1===e?a:n+t+a:n+t+d[l-2])}l--}return c.join(t)}(n,e)}const N={dropSpaces:!1};function v(i,s){const{isNegative:u,integerPart:c,decimalPart:l}=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(e(i))}if("string"==typeof i)return r(function(e){const r=e.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid number format: "${e}"`);return t(r)?n(r):r}(i));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(i);o(c,b,l);const{dropSpaces:a}=function(n,e){const t={...e};if(void 0===n)return t;if(!function(n){if(null===n||"object"!=typeof n)return!1;const e=Object.getPrototypeOf(n);return null===e||e===Object.prototype}(n))throw new TypeError("Invalid options: expected plain object or undefined, got "+typeof n);const r=Object.keys(e);for(const[i,o]of Object.entries(n)){if(!Object.hasOwn(e,i))throw new TypeError(`Unknown option "${i}" — expected one of: ${r.join(", ")}`);if(void 0!==o){if(typeof o!=typeof t[i])throw new TypeError(`Option "${i}" must be a ${typeof t[i]}, got ${typeof o}`);t[i]=o}}return t}(s,N),d=a?"":" ";let p="";return u&&(p=g+d),p+=y(c,a),l&&(p+=d+"virgül"+d+function(n,e){const t=e?"":" ";let r="",i=0;for(;i<n.length&&"0"===n[i];)r&&(r+=t),r+=f,i++;const o=n.slice(i);return o&&(r&&(r+=t),r+=y(BigInt(o),e)),r}(l,a)),p}function I(e){const r=function(e){const r=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 r=e.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 e=n(r);if(e.includes("."))throw new RangeError("Ordinals must be whole numbers");const t=BigInt(e);if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}try{const n=BigInt(r);if(n<=0n)throw new RangeError("Ordinals must be positive integers");return n}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 ${r}`)}(e);return o(r,p),function(n){if(n>=1n&&n<=10n)return h[Number(n)];const e=y(n,!0);return e+function(n){for(let e=n.length-1;e>=0;e--){const t=n[e];if("aıou".includes(t))return"ou".includes(t)?"uncu":"ıncı";if("eiöü".includes(t))return"öü".includes(t)?"üncü":"inci"}return"inci"}(e)}(r)}function E(n){const{isNegative:t,dollars:r,cents:s}=function(n){const t=typeof n;if("bigint"==typeof n)return n<0n?{isNegative:!0,dollars:-n,cents:0n}:{isNegative:!1,dollars:n,cents:0n};if("number"==typeof n){if(!Number.isFinite(n))throw new RangeError("Currency must be a finite number");return Number.isSafeInteger(n)?n<0?{isNegative:!0,dollars:BigInt(-n),cents:0n}:{isNegative:!1,dollars:BigInt(n),cents:0n}:i(e(n))}if("string"==typeof n)return i(n);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(n);o(r,m);let u="";return t&&(u=g+" "),(r>0n||0n===s)&&(u+=y(r,!1)+" lira"),s>0n&&(r>0n&&(u+=" "),u+=y(s,!1)+" kuruş"),u}export{N as cardinalDefaults,b as cardinalMax,m as currencyMax,p as ordinalMax,v as toCardinal,E as toCurrency,I as toOrdinal};