xref: /expo/packages/expo-router/build/matchers.js (revision 34a1b52d)
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.stripInvisibleSegmentsFromPath = exports.stripGroupSegmentsFromPath = exports.removeFileSystemDots = exports.removeSupportedExtensions = exports.getContextKey = exports.getNameFromFilePath = exports.matchGroupName = exports.matchDeepDynamicRouteName = exports.matchDynamicName = void 0;
4/** Match `[page]` -> `page` */
5function matchDynamicName(name) {
6    // Don't match `...` or `[` or `]` inside the brackets
7    // eslint-disable-next-line no-useless-escape
8    return name.match(/^\[([^[\](?:\.\.\.)]+?)\]$/)?.[1];
9}
10exports.matchDynamicName = matchDynamicName;
11/** Match `[...page]` -> `page` */
12function matchDeepDynamicRouteName(name) {
13    return name.match(/^\[\.\.\.([^/]+?)\]$/)?.[1];
14}
15exports.matchDeepDynamicRouteName = matchDeepDynamicRouteName;
16/** Match `(page)` -> `page` */
17function matchGroupName(name) {
18    return name.match(/^(?:[^\\(\\)])*?\(([^\\/]+)\).*?$/)?.[1];
19}
20exports.matchGroupName = matchGroupName;
21function getNameFromFilePath(name) {
22    return removeSupportedExtensions(removeFileSystemDots(name));
23}
24exports.getNameFromFilePath = getNameFromFilePath;
25function getContextKey(name) {
26    // The root path is `` (empty string) so always prepend `/` to ensure
27    // there is some value.
28    const normal = '/' + getNameFromFilePath(name);
29    if (!normal.endsWith('_layout')) {
30        return normal;
31    }
32    return normal.replace(/\/?_layout$/, '');
33}
34exports.getContextKey = getContextKey;
35/** Remove `.js`, `.ts`, `.jsx`, `.tsx` */
36function removeSupportedExtensions(name) {
37    return name.replace(/(\+api)?\.[jt]sx?$/g, '');
38}
39exports.removeSupportedExtensions = removeSupportedExtensions;
40// Remove any amount of `./` and `../` from the start of the string
41function removeFileSystemDots(filePath) {
42    return filePath.replace(/^(?:\.\.?\/)+/g, '');
43}
44exports.removeFileSystemDots = removeFileSystemDots;
45function stripGroupSegmentsFromPath(path) {
46    return path
47        .split('/')
48        .reduce((acc, v) => {
49        if (matchGroupName(v) == null) {
50            acc.push(v);
51        }
52        return acc;
53    }, [])
54        .join('/');
55}
56exports.stripGroupSegmentsFromPath = stripGroupSegmentsFromPath;
57function stripInvisibleSegmentsFromPath(path) {
58    return stripGroupSegmentsFromPath(path).replace(/\/?index$/, '');
59}
60exports.stripInvisibleSegmentsFromPath = stripInvisibleSegmentsFromPath;
61//# sourceMappingURL=matchers.js.map