Files
clean-cuts/composables/usePagination.ts
Blossomi Shymae 32b961dd32 Add champions feat
2024-05-02 08:48:37 -05:00

32 lines
689 B
TypeScript

export default function usePagination<T>(iterable: Array<T>, pageSize: number) {
const length = Math.ceil(iterable.length / pageSize);
const pages = Array.from({ length: length }, (v, k) => {
return iterable.slice(k * pageSize, k * pageSize + pageSize);
});
const index = ref(0);
const prev = () => {
if (index.value - 1 < 0) return;
index.value--;
};
const next = () => {
if (index.value + 1 >= length) return;
index.value++;
};
const first = () => {
index.value = 0;
};
const last = () => {
index.value = length - 1;
};
return {
count: length,
pages: pages,
index: index,
prev,
next,
first,
last,
};
}