one
Version:
One is a new React Framework that makes Vite serve both native and web.
137 lines (136 loc) • 4.3 kB
JavaScript
const dynamicNameRe = /^\[([^[\]]+?)\]$/;
function matchDynamicName(name) {
const paramName = name.match(dynamicNameRe)?.[1];
if (paramName == null) {
return void 0;
} else if (paramName.startsWith("...")) {
return {
name: paramName.slice(3),
deep: true
};
} else {
return {
name: paramName,
deep: false
};
}
}
function matchRoutePattern(pattern, path) {
const patternSegments = pattern.split("/").filter(Boolean);
if (patternSegments[patternSegments.length - 1] === "index") {
patternSegments.pop();
}
const pathSegments = path.split("/").filter(Boolean);
let specificity = 0;
let pi = 0;
for (let ui = 0; ui < patternSegments.length; ui++) {
const seg = patternSegments[ui];
if (seg.startsWith("(") && seg.endsWith(")")) continue;
if (seg.startsWith("[...") && seg.endsWith("]")) {
return {
specificity: specificity + (pathSegments.length - pi)
};
}
if (pi >= pathSegments.length) return null;
if (seg.startsWith("[") && seg.endsWith("]")) {
specificity += 1;
pi += 1;
continue;
}
if (seg !== pathSegments[pi]) return null;
specificity += 2;
pi += 1;
}
return {
specificity
};
}
function matchDeepDynamicRouteName(name) {
return name.match(/^\[\.\.\.([^/]+?)\]$/)?.[1];
}
function testNotFound(name) {
return name.endsWith("+not-found");
}
function matchGroupName(name) {
return name.match(/^(?:[^\\(\\)])*?\(([^\\/]+)\).*?$/)?.[1];
}
function matchArrayGroupName(name) {
return name.match(/(?:[^\\(\\)])*?\(?([^\\/()]+,[^\\/()]+)\)?.*?$/)?.[1];
}
function getNameFromFilePath(name) {
return removeSupportedExtensions(removeFileSystemDots(name));
}
function getContextKey(name) {
const normal = "/" + getNameFromFilePath(name);
if (!normal.endsWith("_layout")) {
return normal;
}
return normal.replace(/\/?_layout$/, "");
}
function removeSupportedExtensions(name) {
return name.replace(/(\+(api|spa|ssg|ssr))?\.[jt]sx?$/g, "");
}
function removeFileSystemDots(filePath) {
return filePath.replace(/^(?:\.\.?\/)+/g, "");
}
function stripGroupSegmentsFromPath(path) {
return path.split("/").reduce((acc, v) => {
if (matchGroupName(v) == null) {
acc.push(v);
}
return acc;
}, []).join("/");
}
function stripInvisibleSegmentsFromPath(path) {
return stripGroupSegmentsFromPath(path).replace(/\/?index$/, "");
}
function isTypedRoute(name) {
return !name.startsWith("+") && !name.endsWith(".d.ts") && name.match(/(_layout|[^/]*?\+[^/]*?)\.[tj]sx?$/) === null;
}
const directoryRenderModeRe = /^(.+)\+(api|ssg|ssr|spa)$/;
function matchDirectoryRenderMode(name) {
const match = name.match(directoryRenderModeRe);
if (!match) return void 0;
return {
name: match[1],
renderMode: match[2]
};
}
const slotPrefixRe = /^@([a-zA-Z][a-zA-Z0-9_-]*)$/;
function matchSlotName(name) {
return name.match(slotPrefixRe)?.[1];
}
function isSlotDirectory(name) {
return slotPrefixRe.test(name);
}
function matchInterceptPrefix(segment) {
const match = segment.match(/^((?:\(\.{1,3}\))+)(.+)$/);
if (!match) return void 0;
const [, prefixes, targetPath] = match;
if (prefixes.includes("(...)")) {
return {
levels: Infinity,
targetPath,
originalSegment: segment
};
}
const doubleDotMatches = prefixes.match(/\(\.{2}\)/g) || [];
const levels = doubleDotMatches.length;
return {
levels,
targetPath,
originalSegment: segment
};
}
function stripInterceptPrefix(segment) {
const match = matchInterceptPrefix(segment);
return match ? match.targetPath : segment;
}
function hasInterceptPrefix(segment) {
return /^\(\.{1,3}\)/.test(segment);
}
function stripSlotSegmentsFromPath(path) {
return path.split("/").filter(segment => !isSlotDirectory(segment)).join("/");
}
export { getContextKey, getNameFromFilePath, hasInterceptPrefix, isSlotDirectory, isTypedRoute, matchArrayGroupName, matchDeepDynamicRouteName, matchDirectoryRenderMode, matchDynamicName, matchGroupName, matchInterceptPrefix, matchRoutePattern, matchSlotName, removeFileSystemDots, removeSupportedExtensions, stripGroupSegmentsFromPath, stripInterceptPrefix, stripInvisibleSegmentsFromPath, stripSlotSegmentsFromPath, testNotFound };
//# sourceMappingURL=matchers.mjs.map