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