UNPKG

expo-router

Version:

Expo Router is a file-based router for React Native and web applications.

172 lines (170 loc) • 5.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 0 && (module.exports = { VALID_PLATFORMS: null, getContextKey: null, getNameFromFilePath: null, getPlatformFromFilePath: null, isTypedRoute: null, matchArrayGroupName: null, matchDynamicName: null, matchGroupName: null, matchLastGroupName: null, removeFileSystemDots: null, removeFileSystemExtensions: null, removeSupportedExtensions: null, stripGroupSegmentsFromPath: null, stripInvisibleSegmentsFromPath: null, testNotFound: null }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: Object.getOwnPropertyDescriptor(all, name).get }); } _export(exports, { get VALID_PLATFORMS () { return VALID_PLATFORMS; }, get getContextKey () { return getContextKey; }, get getNameFromFilePath () { return getNameFromFilePath; }, get getPlatformFromFilePath () { return getPlatformFromFilePath; }, get isTypedRoute () { return isTypedRoute; }, get matchArrayGroupName () { return matchArrayGroupName; }, get matchDynamicName () { return matchDynamicName; }, get matchGroupName () { return matchGroupName; }, get matchLastGroupName () { return matchLastGroupName; }, get removeFileSystemDots () { return removeFileSystemDots; }, get removeFileSystemExtensions () { return removeFileSystemExtensions; }, get removeSupportedExtensions () { return removeSupportedExtensions; }, get stripGroupSegmentsFromPath () { return stripGroupSegmentsFromPath; }, get stripInvisibleSegmentsFromPath () { return stripInvisibleSegmentsFromPath; }, get testNotFound () { return testNotFound; } }); /** Match `[page]` -> `page` or `[...group]` -> `...group` */ const dynamicNameRe = /^\[([^[\]]+?)\]$/; const VALID_PLATFORMS = new Set([ 'android', 'ios', 'native', 'web' ]); function matchDynamicName(name) { const paramName = name.match(dynamicNameRe)?.[1]; if (paramName == null) { return undefined; } else if (paramName.startsWith('...')) { return { name: paramName.slice(3), deep: true }; } else { return { name: paramName, deep: false }; } } function testNotFound(name) { return /\+not-found$/.test(name); } function matchGroupName(name) { return name.match(/^(?:[^\\()])*?\(([^\\/]+)\)/)?.[1]; } function matchLastGroupName(name) { return name.match(/.*(?:\/|^)\(([^\\/]+)\)[^\s]*$/)?.[1]; } function matchArrayGroupName(name) { return name.match(/(?:[^\\()])*?\(([^\\/]+,[^\\/]+)\)/)?.[1]; } function getNameFromFilePath(name) { return removeSupportedExtensions(removeFileSystemDots(name)); } function getPlatformFromFilePath(filePath) { const filename = getNameFromFilePath(filePath).split('/').pop(); const extensionIndex = filename.lastIndexOf('.'); if (extensionIndex < 0) { return undefined; } const platform = filename.slice(extensionIndex + 1); return VALID_PLATFORMS.has(platform) ? platform : undefined; } function getContextKey(name) { // The root path is `` (empty string) so always prepend `/` to ensure // there is some value. const contextKey = getNameFromFilePath(name); const platform = getPlatformFromFilePath(name); const contextKeyWithoutPlatform = platform ? contextKey.slice(0, -(platform.length + 1)) : contextKey; const normal = '/' + contextKeyWithoutPlatform; if (!normal.endsWith('_layout')) { return normal; } return normal.replace(/\/?_layout$/, ''); } function removeSupportedExtensions(name) { return name.replace(/(\+api)?\.[jt]sx?$/g, ''); } function removeFileSystemExtensions(name) { return name.replace(/\.[jt]sx?$/g, ''); } function removeFileSystemDots(filePath) { return filePath.replace(/^(?:\.\.?\/)+/g, ''); } function stripGroupSegmentsFromPath(path) { // Only the pathname can contain groups. The search and hash may contain parentheses, e.g. `?a=(a)` const suffixIndex = path.search(/[?#]/); if (suffixIndex !== -1) { const pathname = stripGroupSegmentsFromPath(path.slice(0, suffixIndex)); const queryAndHash = path.slice(suffixIndex); if (pathname) { return pathname + queryAndHash; } if (path.startsWith('/')) { return '/' + queryAndHash; } return queryAndHash; } 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.match(/(_layout|[^/]*?\+[^/]*?)\.[tj]sx?$/) === null; } //# sourceMappingURL=matchers.js.map