UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

1 lines 1.6 kB
(()=>{"use strict";const e={d:(t,r)=>{if(Array.isArray(r))for(var a=0;a<r.length;){var i=r[a++],n=r[a++];e.o(t,i)?0===n&&a++:0===n?Object.defineProperty(t,i,{enumerable:!0,value:r[a++]}):Object.defineProperty(t,i,{enumerable:!0,get:n})}else for(var i in r)e.o(r,i)&&!e.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:r[i]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)};let t={};e.d(t,{TinyArrayPaginator:()=>r});const r=class{#e;get data(){return this.#e}set data(e){if(!(Array.isArray(e)||e instanceof Set))throw new TypeError("Paginator expects an array or Set as data source.");this.#e=e}get size(){return Array.isArray(this.#e)?this.#e.length:this.#e.size}constructor(e){if(!(Array.isArray(e)||e instanceof Set))throw new TypeError("Paginator expects an array or Set as data source.");this.#e=e}get({page:e,perPage:t,filter:r}){if(!Number.isInteger(e)||e<1)throw new RangeError("Page number must be a positive integer.");if(!Number.isInteger(t)||t<1)throw new RangeError("Items per page must be a positive integer.");const a=Array.isArray(this.#e)?this.#e:Array.from(this.#e);let i=a;if(r)if("function"==typeof r)i=a.filter((e,t,a)=>r(e,t,a));else{if("object"!=typeof r)throw new TypeError("Filter must be an object or a function");i=a.filter(e=>Object.entries(r).every(([t,r])=>{const a=e[t];return r instanceof RegExp?r.test(a):a===r}))}else i=a;const n=i.length,s=Math.max(1,Math.ceil(n/t)),o=Math.min(e,s),y=(o-1)*t,c=y+t,g=o>1,f=o<s;return{items:i.slice(y,c),page:o,perPage:t,totalItems:n,totalPages:s,hasPrev:g,hasNext:f}}};window.TinyArrayPaginator=t.TinyArrayPaginator})();