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.95 kB
JavaScript
/*! n2words/lv-LV v5.1.2 | MIT License | github.com/forzagreen/n2words */
var t,e;t=this,e=function(t){"use strict";function e(t){let[e,i]=t.toLowerCase().split("e");const n=parseInt(i,10);let r="";"-"===e[0]&&(r="-",e=e.slice(1));const s=e.indexOf("."),o=-1===s?e:e.slice(0,s)+e.slice(s+1),u=(-1===s?e.length:s)+n;return u>=o.length?r+o+"0".repeat(u-o.length):u<=0?r+"0."+"0".repeat(-u)+o:r+o.slice(0,u)+"."+o.slice(u)}function i(t){const i=t.toString();return n(i)?e(i):i}function n(t){return t.includes("e")||t.includes("E")}function r(t){const e="-"===t[0];e&&(t=t.slice(1));const i=t.indexOf(".");if(-1===i)return{isNegative:e,integerPart:BigInt(t)};const n=t.slice(0,i)||"0",r=t.slice(i+1);return{isNegative:e,integerPart:BigInt(n),decimalPart:r}}function s(t){let i=t.trim();if(0===i.length||Number.isNaN(Number(i)))throw new RangeError(`Invalid currency format: "${t}"`);n(i)&&(i=e(i));const r="-"===i[0];r&&(i=i.slice(1));const s=i.indexOf(".");if(-1===s)return{isNegative:r,dollars:BigInt(i),cents:0n};const o=i.slice(0,s)||"0",u=(i.slice(s+1)+"00").slice(0,2);return{isNegative:r,dollars:BigInt(o),cents:BigInt(u)}}function o(t,e,i){if(null===e)return;if(t<e&&!(i&&BigInt(i)>=e))return;const n=e.toString().length-1,r=e===10n**BigInt(n)?`10^${n} - 1`:""+(e-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${r}`)}function u(t){return 10n**BigInt(3*(t+1))}const a=["","viens","divi","trīs","četri","pieci","seši","septiņi","astoņi","deviņi"],c=["","viena","divas","trīs","četras","piecas","sešas","septiņas","astoņas","deviņas"],l=["desmit","vienpadsmit","divpadsmit","trīspadsmit","četrpadsmit","piecpadsmit","sešpadsmit","septiņpadsmit","astoņpadsmit","deviņpadsmit"],f=["","","divdesmit","trīsdesmit","četrdesmit","piecdesmit","sešdesmit","septiņdesmit","astoņdesmit","deviņdesmit"],d="simts",m="simti",p="simtu",g="nulle",h="mīnus",v=["","pirmais","otrais","trešais","ceturtais","piektais","sestais","septītais","astotais","devītais"],b=["desmitais","vienpadsmitais","divpadsmitais","trīspadsmitais","četrpadsmitais","piecpadsmitais","sešpadsmitais","septiņpadsmitais","astoņpadsmitais","deviņpadsmitais"],w=["","","divdesmitais","trīsdesmitais","četrdesmitais","piecdesmitais","sešdesmitais","septiņdesmitais","astoņdesmitais","deviņdesmitais"],N=["","simtais","divsimtais","trīssimtais","četrsimtais","piecsimtais","sešsimtais","septiņsimtais","astoņsimtais","deviņsimtais"],j=["tūkstošais","miljonais","miljardais","triljonais"],y=["cents","centi","centu"],I=[["tūkstotis","tūkstoši","tūkstošu"],["miljons","miljoni","miljonu"],["miljards","miljardi","miljardu"],["triljons","triljoni","triljonu"],["kvadriljons","kvadriljoni","kvadriljonu"],["kvintiljons","kvintiljoni","kvintiljonu"],["sikstiljons","sikstiljoni","sikstiljonu"],["septiljons","septiljoni","septiljonu"],["oktiljons","oktiljoni","oktiljonu"]],E=u(I.length),O=u(j.length),k=u(I.length);function B(t){if(0===t)return"";const e=t%10,i=Math.trunc(t/10)%10,n=Math.trunc(t/100),r=[];return n>0&&(1===n&&0===i&&e>0?r.push(p):n>1?(r.push(a[n]),r.push(m)):r.push(d)),i>1&&r.push(f[i]),1===i?r.push(l[e]):e>0&&r.push(a[e]),r.join(" ")}function R(t,e){return 0===t?e[2]:1==t%10&&11!=t%100?e[0]:e[1]}function $(t,e){if(0n===t)return g;if(t<1000n){const i=Number(t);return"feminine"===e?function(t){if(0===t)return"";const e=t%10,i=Math.trunc(t/10)%10,n=Math.trunc(t/100),r=[];return n>0&&(1===n&&0===i&&e>0?r.push(p):n>1?(r.push(a[n]),r.push(m)):r.push(d)),i>1&&r.push(f[i]),1===i?r.push(l[e]):e>0&&r.push(c[e]),r.join(" ")}(i):B(i)}return function(t){const e=t.toString(),i=e.length,n=[],r=i%3;let s=0;for(r>0&&(n.push(Number(e.slice(0,r))),s=r);s<i;)n.push(Number(e.slice(s,s+3))),s+=3;const o=[];let u=n.length-1;for(let t=0;t<n.length;t++){const e=n[t];if(0!==e){const t=B(e);if(0===u)o.push(t);else{const i=R(e,I[u-1]);o.push(1===e?i:t+" "+i)}}u--}return o.join(" ")}(t)}const x={gender:"masculine"},P={gender:["masculine","feminine"]};function T(t){if(0===t)return"";const e=t%10,i=Math.trunc(t/10);return 0===i?v[e]:1===i?b[e]:0===e?w[i]:f[i]+" "+v[e]}function S(t){if(t<100n)return T(Number(t));if(t<1000n){const e=Number(t),i=Math.trunc(e/100),n=e%100;if(0===n)return N[i];let r;return r=1===i?d:a[i]+" "+m,r+" "+T(n)}if(t<1000000n){const e=Number(t/1000n),i=Number(t%1000n);if(0===i){if(1===e)return j[0];const t=R(e,I[0]);return B(e)+" "+t+" "+j[0]}const n=R(e,I[0]),r=1===e?"":B(e)+" ";return(1===e?n:r+n)+" "+S(BigInt(i))}return function(t){const e=t.toString(),i=e.length,n=[],r=i%3;let s=0;for(r>0&&(n.push(Number(e.slice(0,r))),s=r);s<i;)n.push(Number(e.slice(s,s+3))),s+=3;let o=n.length-1;for(;o>=0&&0===n[o];)o--;const u=[];let a=n.length-1;for(let t=0;t<n.length;t++){const e=n[t];if(0!==e){const i=t===o;if(0===a)u.push(i?S(BigInt(e)):B(e));else if(i)if(1===e)u.push(j[a-1]);else{const t=R(e,I[a-1]);u.push(B(e)+" "+t+" "+j[a-1])}else{const t=R(e,I[a-1]);u.push(1===e?t:B(e)+" "+t)}}a--}return u.join(" ")}(t)}const M={lvLV:function(t){const i=function(t){const i=typeof t;if("bigint"==typeof t){if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}if("number"==typeof t){if(!Number.isFinite(t))throw new RangeError("Ordinals must be finite numbers");if(!Number.isInteger(t))throw new RangeError("Ordinals must be whole numbers");if(t<=0)throw new RangeError("Ordinals must be positive integers");return BigInt(t)}if("string"==typeof t)return function(t){const i=t.trim();if(0===i.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===i[0])throw new RangeError("Ordinals cannot be negative");if(i.includes("."))throw new RangeError("Ordinals must be whole numbers");if(n(i)){const t=e(i);if(t.includes("."))throw new RangeError("Ordinals must be whole numbers");const n=BigInt(t);if(n<=0n)throw new RangeError("Ordinals must be positive integers");return n}try{const t=BigInt(i);if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}catch(e){if(e instanceof RangeError)throw e;throw new RangeError(`Invalid ordinal format: "${t}"`,{cause:e})}}(t);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${i}`)}(t);return o(i,O),S(i)}};t.currency={lvLV:function(t){const{isNegative:e,dollars:n,cents:r}=function(t){const e=typeof t;if("bigint"==typeof t)return t<0n?{isNegative:!0,dollars:-t,cents:0n}:{isNegative:!1,dollars:t,cents:0n};if("number"==typeof t){if(!Number.isFinite(t))throw new RangeError("Currency must be a finite number");return Number.isSafeInteger(t)?t<0?{isNegative:!0,dollars:BigInt(-t),cents:0n}:{isNegative:!1,dollars:BigInt(t),cents:0n}:s(i(t))}if("string"==typeof t)return s(t);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${e}`)}(t);o(n,k);let u="";return e&&(u=h+" "),(n>0n||0n===r)&&(u+=$(n,"masculine"),u+=" eiro"),r>0n&&(n>0n&&(u+=" "),u+=$(r,"masculine"),u+=" "+function(t,e){if(0===t)return e[2];const i=t%10,n=t%100;return n>=10&&n<=19||0===i?e[2]:1===i?e[0]:e[1]}(Number(r),y)),u}},t.lvLV=function(t,s){const{isNegative:u,integerPart:a,decimalPart:c}=function(t){const s=typeof t;if("bigint"==typeof t)return t<0n?{isNegative:!0,integerPart:-t}:{isNegative:!1,integerPart:t};if("number"==typeof t){if(!Number.isFinite(t))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(t)?t<0?{isNegative:!0,integerPart:BigInt(-t)}:{isNegative:!1,integerPart:BigInt(t)}:r(i(t))}if("string"==typeof t)return r(function(t){const i=t.trim();if(0===i.length||Number.isNaN(Number(i)))throw new RangeError(`Invalid number format: "${t}"`);return n(i)?e(i):i}(t));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${s}`)}(t);o(a,E,c);const{gender:l}=function(t,e,i){const n={...e};if(void 0===t)return n;if(!function(t){if(null===t||"object"!=typeof t)return!1;const e=Object.getPrototypeOf(t);return null===e||e===Object.prototype}(t))throw new TypeError("Invalid options: expected plain object or undefined, got "+typeof t);const r=Object.keys(e);for(const[s,o]of Object.entries(t)){if(!Object.hasOwn(e,s))throw new TypeError(`Unknown option "${s}" — expected one of: ${r.join(", ")}`);if(void 0!==o){if(typeof o!=typeof n[s])throw new TypeError(`Option "${s}" must be a ${typeof n[s]}, got ${typeof o}`);if(void 0!==i&&Object.hasOwn(i,s)){const t=i[s];if(!t.includes(o))throw new RangeError(`Option "${s}" must be one of: ${t.join(", ")} — got ${JSON.stringify(o)}`)}n[s]=o}}return n}(s,x,P);let f="";return u&&(f=h+" "),f+=$(a,l),c&&(f+=" komats "+function(t,e){let i="",n=0;for(;n<t.length&&"0"===t[n];)i&&(i+=" "),i+=g,n++;const r=t.slice(n);return r&&(i&&(i+=" "),i+=$(BigInt(r),e)),i}(c,l)),f},t.ordinal=M},"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).n2words=t.n2words||{});