(items, query = {})
| 45 | } |
| 46 | |
| 47 | function paginate(items, query = {}) { |
| 48 | const limit = Math.min(toPositiveInt(query.limit, 50), 200); |
| 49 | const offset = Math.max(0, toPositiveInt(query.offset || query.cursor, 0)); |
| 50 | const data = items.slice(offset, offset + limit); |
| 51 | const nextOffset = offset + data.length; |
| 52 | return { |
| 53 | data, |
| 54 | pagination: { |
| 55 | limit, |
| 56 | offset, |
| 57 | totalItems: items.length, |
| 58 | nextCursor: nextOffset < items.length ? String(nextOffset) : null, |
| 59 | }, |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | function toPositiveInt(value, fallback) { |
| 64 | const n = Number(value); |
no test coverage detected