polen
Version:
A framework for delightful GraphQL developer portals
78 lines (77 loc) • 2.13 kB
JavaScript
import { arrayEquals } from '#lib/kit-temp';
import {} from '@wollybeard/kit';
export const sep = `/`;
export const pathToExpression = (path) => {
return sep + path.join(sep);
};
/**
* Route is top level meaning exists directly under the root.
*
* It excludes the root level route.
*/
export const routeIsTopLevel = (route) => {
return route.logical.path.length === 1;
};
/**
* Route is not top or root level
*/
export const routeIsSubLevel = (route) => {
return route.logical.path.length > 1;
};
//
// ━━ Root Level
//
//
//
//
//
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ • Root Level Route
//
//
/**
* Route is the singular root route.
* This is the case of index under root.
*/
export const routeIsRootLevel = (route) => {
// No need to check for name "index"
// Segments is uniquely empty for <root>/index
return route.logical.path.length === 0;
};
//
//
//
// ━━━━━━━━━━━━━━ • Route Functions
//
//
export const routeIsFromIndexFile = (route) => {
return route.file.path.relative.name === conventions.index.name;
};
export const routeIsSubOf = (route, potentialAncestorPath) => {
if (route.logical.path.length <= potentialAncestorPath.length) {
return false;
}
return arrayEquals(route.logical.path.slice(0, potentialAncestorPath.length), potentialAncestorPath);
};
/**
* You are responsible for ensuring given ancestor path is really an ancestor of given route's path.
*/
export const makeRelativeUnsafe = (route, assumedAncestorPath) => {
// We assume that we're working with paths where index is elided per our FileRouter system.
const newPath = route.logical.path.slice(assumedAncestorPath.length);
return {
...route,
logical: {
...route.logical,
path: newPath,
},
};
};
export const routeToPathExpression = (route) => {
return pathToExpression(route.logical.path);
};
const conventions = {
index: {
name: `index`,
},
};
//# sourceMappingURL=route.js.map