staticql
Version:
Type-safe query engine for static content including Markdown, YAML, JSON, and more.
82 lines (81 loc) • 2.15 kB
JavaScript
/**
* Create PageInfo.
*
* @param page
* @param pageSize
* @param startIndex
* @param matchedLen
* @param direction
* @param encodeCursor
* @returns page info for cursor pagination
*/
export function createPageInfo(page, pageSize, startIndex, matchedLen, direction, encodeCursor) {
let hasNextPage = false;
let hasPreviousPage = false;
if (direction === "after") {
hasNextPage = startIndex + (startIndex > 0 ? 1 : 0) + pageSize < matchedLen;
hasPreviousPage = startIndex + (startIndex > 0 ? 1 : 0) > 0;
}
else {
const endIdx = startIndex;
const beginIdx = Math.max(0, endIdx - pageSize);
hasNextPage = endIdx < matchedLen;
hasPreviousPage = beginIdx > 0;
}
return {
hasNextPage,
hasPreviousPage,
startCursor: page.length > 0 ? encodeCursor(page[0]) : undefined,
endCursor: page.length > 0 ? encodeCursor(page[page.length - 1]) : undefined,
};
}
/**
* Get sliced records.
*
* @param records
* @param startIndex
* @param pageSize
* @param direction
* @returns
*/
export function getPageSlice(records, startIndex, pageSize, direction) {
if (direction === "after") {
return records.slice(startIndex + (startIndex > 0 ? 1 : 0), startIndex + (startIndex > 0 ? 1 : 0) + pageSize);
}
else {
const endIdx = startIndex;
const beginIdx = Math.max(0, endIdx - pageSize);
return records.slice(beginIdx, endIdx);
}
}
/**
* Encode for cursor.
*
* @param slug
* @returns
*/
export function encodeCursor(obj) {
const str = JSON.stringify(obj);
const bytes = new TextEncoder().encode(str);
let binary = "";
for (let b of bytes)
binary += String.fromCharCode(b);
return btoa(binary);
}
/**
* Decode for cursor.
*
* @param cursor
* @returns
*/
export function decodeCursor(cursor) {
try {
const binary = atob(cursor);
const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
const obj = JSON.parse(new TextDecoder().decode(bytes));
return obj;
}
catch {
throw new Error("Invalid cursor");
}
}