paginate.ts — astro Source File
Architecture documentation for paginate.ts, a typescript file in the astro codebase. 6 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR f9b241f0_1a09_8ec8_0d08_713d9b433a71["paginate.ts"] 135a8084_d596_67c2_9209_cca6693604e6["../types/public/common.js"] f9b241f0_1a09_8ec8_0d08_713d9b433a71 --> 135a8084_d596_67c2_9209_cca6693604e6 baa53824_73a3_1e03_2043_4d0c058ecca5["../types/public/index.js"] f9b241f0_1a09_8ec8_0d08_713d9b433a71 --> baa53824_73a3_1e03_2043_4d0c058ecca5 10d4e39f_edb6_3e34_aa93_ae1211e7da05["../types/public/internal.js"] f9b241f0_1a09_8ec8_0d08_713d9b433a71 --> 10d4e39f_edb6_3e34_aa93_ae1211e7da05 ef8a1e3f_e350_75a6_b92d_62a8566d8db9["../core/errors/index.js"] f9b241f0_1a09_8ec8_0d08_713d9b433a71 --> ef8a1e3f_e350_75a6_b92d_62a8566d8db9 7e4494c0_5563_4329_1bff_a84be66e1bc2["../core/path.js"] f9b241f0_1a09_8ec8_0d08_713d9b433a71 --> 7e4494c0_5563_4329_1bff_a84be66e1bc2 162316ad_87a9_ee43_993e_48696ea31a8d["../core/routing/manifest/generator.js"] f9b241f0_1a09_8ec8_0d08_713d9b433a71 --> 162316ad_87a9_ee43_993e_48696ea31a8d style f9b241f0_1a09_8ec8_0d08_713d9b433a71 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import type {
Page,
PaginateFunction,
PaginateOptions,
Params,
Props,
} from '../../types/public/common.js';
import type { AstroConfig } from '../../types/public/index.js';
import type { RouteData } from '../../types/public/internal.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { joinPaths } from '../path.js';
import { getRouteGenerator } from '../routing/manifest/generator.js';
export function generatePaginateFunction(
routeMatch: RouteData,
base: AstroConfig['base'],
trailingSlash: AstroConfig['trailingSlash'],
): (...args: Parameters<PaginateFunction>) => ReturnType<PaginateFunction> {
return function paginateUtility(
data: readonly any[],
args: PaginateOptions<Props, Params> = {},
): ReturnType<PaginateFunction> {
const generate = getRouteGenerator(routeMatch.segments, trailingSlash);
let { pageSize: _pageSize, params: _params, props: _props } = args;
const pageSize = _pageSize || 10;
const paramName = 'page';
const additionalParams = _params || {};
const additionalProps = _props || {};
let includesFirstPageNumber: boolean;
if (routeMatch.params.includes(`...${paramName}`)) {
includesFirstPageNumber = false;
} else if (routeMatch.params.includes(`${paramName}`)) {
includesFirstPageNumber = true;
} else {
throw new AstroError({
...AstroErrorData.PageNumberParamNotFound,
message: AstroErrorData.PageNumberParamNotFound.message(paramName),
});
}
const lastPage = Math.max(1, Math.ceil(data.length / pageSize));
const result = [...Array(lastPage).keys()].map((num) => {
const pageNum = num + 1;
const start = pageSize === Infinity ? 0 : (pageNum - 1) * pageSize; // currentPage is 1-indexed
const end = Math.min(start + pageSize, data.length);
const params = {
...additionalParams,
[paramName]: includesFirstPageNumber || pageNum > 1 ? String(pageNum) : undefined,
};
const current = addRouteBase(generate({ ...params }), base);
const next =
pageNum === lastPage
? undefined
: addRouteBase(generate({ ...params, page: String(pageNum + 1) }), base);
const prev =
pageNum === 1
? undefined
: addRouteBase(
generate({
...params,
page:
!includesFirstPageNumber && pageNum - 1 === 1 ? undefined : String(pageNum - 1),
}),
base,
);
const first =
pageNum === 1
? undefined
: addRouteBase(
generate({
...params,
page: includesFirstPageNumber ? '1' : undefined,
}),
base,
);
const last =
pageNum === lastPage
? undefined
: addRouteBase(generate({ ...params, page: String(lastPage) }), base);
return {
params,
props: {
...additionalProps,
page: {
data: data.slice(start, end),
start,
end: end - 1,
size: pageSize,
total: data.length,
currentPage: pageNum,
lastPage: lastPage,
url: { current, next, prev, first, last },
} as Page,
},
};
});
return result;
};
}
function addRouteBase(route: string, base: AstroConfig['base']) {
// `routeMatch.generate` avoids appending `/`
// unless `trailingSlash: 'always'` is configured.
// This means an empty string is possible for the index route.
let routeWithBase = joinPaths(base, route);
if (routeWithBase === '') routeWithBase = '/';
return routeWithBase;
}
Domain
Subdomains
Functions
Dependencies
- ../core/errors/index.js
- ../core/path.js
- ../core/routing/manifest/generator.js
- ../types/public/common.js
- ../types/public/index.js
- ../types/public/internal.js
Source
Frequently Asked Questions
What does paginate.ts do?
paginate.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in paginate.ts?
paginate.ts defines 2 function(s): addRouteBase, generatePaginateFunction.
What does paginate.ts depend on?
paginate.ts imports 6 module(s): ../core/errors/index.js, ../core/path.js, ../core/routing/manifest/generator.js, ../types/public/common.js, ../types/public/index.js, ../types/public/internal.js.
Where is paginate.ts in the architecture?
paginate.ts is located at packages/astro/src/core/render/paginate.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/core/render).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free