UNPKG

@dbs-portal/tool-mock

Version:

API mocking toolkit using MSW for DBS Portal development workflows

151 lines (150 loc) 3.45 kB
function p(n) { const { items: e, page: a, pageSize: t, total: o } = n, r = o ?? e.length, s = Math.ceil(r / t), i = (a - 1) * t, l = i + t, c = e.slice(i, l); return { data: { data: c, meta: { page: a, pageSize: t, total: r, totalPages: s, hasNextPage: a < s, hasPreviousPage: a > 1 } }, items: c }; } function x(n, e, a) { const t = Math.ceil(a / e); return { page: n, pageSize: e, total: a, totalPages: t, hasNextPage: n < t, hasPreviousPage: n > 1 }; } function I(n, e = { page: 1, pageSize: 10, maxPageSize: 100 }) { const a = Math.max(1, parseInt(n.get("page") || "1", 10)), t = Math.min( e.maxPageSize, Math.max(1, parseInt(n.get("pageSize") || String(e.pageSize), 10)) ); return { page: a, pageSize: t }; } function P(n, e, a, t) { let o = 0; if (e) { const u = n.findIndex((f) => t(f) === e); u !== -1 && (o = u + 1); } const r = o + a, s = n.slice(o, r), i = r < n.length, l = o > 0, c = s[s.length - 1], d = i && s.length > 0 && c ? t(c) : null, g = n[o - 1], h = l && o > 0 && g ? t(g) : null; return { data: s, meta: { hasNextPage: i, hasPreviousPage: l, nextCursor: d, previousCursor: h, limit: a } }; } function m(n, e, a) { const t = Math.max(0, e), o = t + a, r = n.slice(t, o), s = o < n.length; return { data: r, meta: { offset: t, limit: a, total: n.length, hasMore: s } }; } function M(n, e, a) { const t = (e - 1) * a, o = t + a, r = n.slice(t, o), s = o < n.length; return { data: r, meta: { page: e, pageSize: a, hasMore: s, nextPage: s ? e + 1 : null } }; } function R(n, e, a = 100) { const t = []; return n < 1 && t.push("Page must be greater than 0"), e < 1 && t.push("Page size must be greater than 0"), e > a && t.push(`Page size cannot exceed ${a}`), { valid: t.length === 0, errors: t }; } function v(n, e, a = 5) { const t = [], o = Math.floor(a / 2); let r = Math.max(1, n - o), s = Math.min(e, n + o); s - r + 1 < a && (r === 1 ? s = Math.min(e, r + a - 1) : s === e && (r = Math.max(1, s - a + 1))); for (let i = r; i <= s; i++) t.push(i); return { pages: t, showFirst: r > 1, showLast: s < e, showPrevious: n > 1, showNext: n < e }; } function S(n, e, a, t) { const o = {}, r = (s) => { const i = new URL(n); return i.searchParams.set("page", s.toString()), i.searchParams.set("pageSize", a.toString()), i.toString(); }; return e > 1 && (o.first = r(1), o.previous = r(e - 1)), e < t && (o.next = r(e + 1), o.last = r(t)), o; } function w(n, e, a, t, o, r) { let s = n; if (e) { const c = e.toLowerCase(); s = s.filter( (d) => a.some((g) => { const h = d[g]; return h && String(h).toLowerCase().includes(c); }) ); } Object.keys(t).length > 0 && (s = s.filter( (c) => Object.entries(t).every(([d, g]) => { const h = c[d]; return Array.isArray(g) ? g.includes(h) : h === g; }) )); const i = s.length; return { data: p({ items: s, page: o, pageSize: r }).data, filteredCount: i, totalCount: n.length }; } export { x as a, P as b, p as c, m as d, M as e, v as f, S as g, w as h, I as p, R as v }; //# sourceMappingURL=pagination-DLG5J9NY.js.map