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.94 kB
JavaScript
/*! n2words/pl-PL 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 i=parseInt(t,10);let r="";"-"===n[0]&&(r="-",n=n.slice(1));const s=n.indexOf("."),o=-1===s?n:n.slice(0,s)+n.slice(s+1),c=(-1===s?n.length:s)+i;return c>=o.length?r+o+"0".repeat(c-o.length):c<=0?r+"0."+"0".repeat(-c)+o:r+o.slice(0,c)+"."+o.slice(c)}function t(e){const t=e.toString();return i(t)?n(t):t}function i(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 i=e.slice(0,t)||"0",r=e.slice(t+1);return{isNegative:n,integerPart:BigInt(i),decimalPart:r}}function s(e){let t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${e}"`);i(t)&&(t=n(t));const r="-"===t[0];r&&(t=t.slice(1));const s=t.indexOf(".");if(-1===s)return{isNegative:r,dollars:BigInt(t),cents:0n};const o=t.slice(0,s)||"0",c=(t.slice(s+1)+"00").slice(0,2);return{isNegative:r,dollars:BigInt(o),cents:BigInt(c)}}function o(e,n,t){if(null===n)return;if(e<n&&!(t&&BigInt(t)>=n))return;const i=n.toString().length-1,r=n===10n**BigInt(i)?`10^${i} - 1`:""+(n-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${r}`)}function c(e){return 10n**BigInt(3*(e+1))}const u=["","jeden","dwa","trzy","cztery","pięć","sześć","siedem","osiem","dziewięć"],a=["","jedna","dwie","trzy","cztery","pięć","sześć","siedem","osiem","dziewięć"],l=["dziesięć","jedenaście","dwanaście","trzynaście","czternaście","piętnaście","szesnaście","siedemnaście","osiemnaście","dziewiętnaście"],f=["","","dwadzieścia","trzydzieści","czterdzieści","pięćdziesiąt","sześćdziesiąt","siedemdziesiąt","osiemdziesiąt","dziewięćdziesiąt"],d=["","sto","dwieście","trzysta","czterysta","pięćset","sześćset","siedemset","osiemset","dziewięćset"],y={1:["tysiąc","tysiące","tysięcy"],2:["milion","miliony","milionów"],3:["miliard","miliardy","miliardów"],4:["bilion","biliony","bilionów"],5:["biliard","biliardy","biliardów"],6:["trylion","tryliony","trylionów"],7:["tryliard","tryliardy","tryliardów"],8:["kwadrylion","kwadryliony","kwadrylionów"],9:["kwaryliard","kwadryliardy","kwadryliardów"],10:["kwintylion","kwintyliony","kwintylionów"]},g="zero",w="minus",m=["","pierwszy","drugi","trzeci","czwarty","piąty","szósty","siódmy","ósmy","dziewiąty"],p=["dziesiąty","jedenasty","dwunasty","trzynasty","czternasty","piętnasty","szesnasty","siedemnasty","osiemnasty","dziewiętnasty"],b=["","","dwudziesty","trzydziesty","czterdziesty","pięćdziesiąty","sześćdziesiąty","siedemdziesiąty","osiemdziesiąty","dziewięćdziesiąty"],h=["","setny","dwusetny","trzechsetny","czterechsetny","pięćsetny","sześćsetny","siedemsetny","osiemsetny","dziewięćsetny"],z=["tysięczny","milionowy","miliardowy","bilionowy","biliardowy","trylionowy","tryliardowy"],N=["złoty","złote","złotych"],v=["grosz","grosze","groszy"];function I(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10)%10,i=Math.trunc(e/100),r=[];return i>0&&r.push(d[i]),1===t?r.push(l[n]):(t>=2&&r.push(f[t]),n>0&&r.push(u[n])),r.join(" ")}function O(e,n){if(1n===e)return n[0];const t=e%10n,i=e%100n;return t>=2n&&t<=4n&&(i<10n||i>20n)?n[1]:n[2]}function E(e,n){if(0n===e)return g;if(e<1000n)return"feminine"===n?function(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10)%10,i=Math.trunc(e/100),r=[];return i>0&&r.push(d[i]),1===t?r.push(l[n]):(t>=2&&r.push(f[t]),n>0&&r.push(a[n])),r.join(" ")}(Number(e)):I(Number(e));if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n),i=O(BigInt(n),y[1]);let r;return r=1===n?i:I(n)+" "+i,t>0&&(r+=" "+I(t)),r}return function(e){const n=[];let t=e;for(;t>0n;)n.push(t%1000n),t/=1000n;let i="";for(let e=n.length-1;e>=0;e--){const t=n[e];if(0n===t)continue;const r=I(Number(t));if(i&&(i+=" "),0===e)i+=r;else{const n=y[e];if(n){const e=O(t,n);i+=1n===t?e:r+" "+e}}}return i}(e)}const B=Math.max(...Object.keys(y).map(Number)),j=c(B),R=c(z.length),$=c(B),k={gender:"masculine"},P={gender:["masculine","feminine"]};function x(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10);return 0===t?m[n]:1===t?p[n]:0===n?b[t]:b[t]+" "+m[n]}function T(e){if(e<100n)return x(Number(e));if(e<1000n){const n=Number(e),t=Math.trunc(n/100),i=n%100;return 0===i?h[t]:d[t]+" "+x(i)}if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);if(0===t)return 1===n?z[0]:I(n)+" "+z[0];const i=O(BigInt(n),y[1]);return(1===n?"":I(n)+" ")+i+" "+T(BigInt(t))}return function(e){const n=e.toString(),t=n.length,i=[],r=t%3;let s=0;for(r>0&&(i.push(Number(n.slice(0,r))),s=r);s<t;)i.push(Number(n.slice(s,s+3))),s+=3;let o=i.length-1;for(;o>=0&&0===i[o];)o--;const c=[];let u=i.length-1;for(let e=0;e<i.length;e++){const n=i[e];if(0!==n){const t=e===o;if(0===u)c.push(t?T(BigInt(n)):I(n));else if(t)c.push(1===n?z[u-1]:I(n)+" "+z[u-1]);else{const e=y[u],t=e?O(BigInt(n),e):"";c.push(1===n?t:I(n)+" "+t)}}u--}return c.join(" ")}(e)}const M={plPL: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(i(t)){const e=n(t);if(e.includes("."))throw new RangeError("Ordinals must be whole numbers");const i=BigInt(e);if(i<=0n)throw new RangeError("Ordinals must be positive integers");return i}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 o(t,R),T(t)}};e.currency={plPL:function(e){const{isNegative:n,dollars:i,cents:r}=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}:s(t(e))}if("string"==typeof e)return s(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${n}`)}(e);o(i,$);let c="";return n&&(c=w+" "),(i>0n||0n===r)&&(c+=E(i,"masculine"),c+=" "+O(i,N)),r>0n&&(i>0n&&(c+=" "),c+=E(r,"masculine"),c+=" "+O(r,v)),c}},e.ordinal=M,e.plPL=function(e,s){const{isNegative:c,integerPart:u,decimalPart:a}=function(e){const s=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)}:r(t(e))}if("string"==typeof e)return r(function(e){const t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${e}"`);return i(t)?n(t):t}(e));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${s}`)}(e);o(u,j,a);const{gender:l}=function(e,n,t){const i={...n};if(void 0===e)return i;if(!function(e){if(null===e||"object"!=typeof e)return!1;const n=Object.getPrototypeOf(e);return null===n||n===Object.prototype}(e))throw new TypeError("Invalid options: expected plain object or undefined, got "+typeof e);const r=Object.keys(n);for(const[s,o]of Object.entries(e)){if(!Object.hasOwn(n,s))throw new TypeError(`Unknown option "${s}" — expected one of: ${r.join(", ")}`);if(void 0!==o){if(typeof o!=typeof i[s])throw new TypeError(`Option "${s}" must be a ${typeof i[s]}, got ${typeof o}`);if(void 0!==t&&Object.hasOwn(t,s)){const e=t[s];if(!e.includes(o))throw new RangeError(`Option "${s}" must be one of: ${e.join(", ")} — got ${JSON.stringify(o)}`)}i[s]=o}}return i}(s,k,P);let f="";return c&&(f=w+" "),f+=E(u,l),a&&(f+=" przecinek "+function(e,n){let t="",i=0;for(;i<e.length&&"0"===e[i];)t&&(t+=" "),t+=g,i++;const r=e.slice(i);return r&&(t&&(t+=" "),t+=E(BigInt(r),n)),t}(a,l)),f}},"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||{});