UNPKG

expo-router

Version:

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

155 lines (153 loc) • 5.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 0 && (module.exports = { appendBaseUrl: null, appendQueryAndHash: null, fixCurrentParams: null, getParamName: null, getPathWithConventionsCollapsed: null, isDynamicPart: 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 appendBaseUrl () { return appendBaseUrl; }, get appendQueryAndHash () { return appendQueryAndHash; }, get fixCurrentParams () { return fixCurrentParams; }, get getParamName () { return getParamName; }, get getPathWithConventionsCollapsed () { return getPathWithConventionsCollapsed; }, get isDynamicPart () { return isDynamicPart; } }); const _matchers = require("../matchers"); const _queryParams = require("../utils/queryParams"); function fixCurrentParams(allParams, route, stringify) { // Better handle array params const currentParams = Object.fromEntries(Object.entries(route.params).flatMap(([key, value])=>{ if (value === undefined) { return []; } return [ [ key, stringify?.[key] ? stringify[key](value) : Array.isArray(value) ? value.map(String) : String(value) ] ]; })); // We always assign params, as non pattern routes may still have query params Object.assign(allParams, currentParams); return currentParams; } function appendQueryAndHash(path, { '#': hash, ...focusedParams }) { const query = (0, _queryParams.stringifySearchParams)(focusedParams); if (query) { path += `?${query}`; } if (hash) { path += `#${hash}`; } return path; } function appendBaseUrl(path, baseUrl = process.env.EXPO_BASE_URL) { if (process.env.NODE_ENV !== 'development') { if (baseUrl) { return `/${baseUrl.replace(/^\/+/, '').replace(/\/$/, '')}${path}`; } } return path; } function getPathWithConventionsCollapsed({ pattern, route, params, preserveGroups, preserveDynamicRoutes, shouldEncodeURISegment = true, initialRouteName }) { const segments = pattern.split('/'); return segments.map((p, i)=>{ const name = getParamName(p); // Showing the route name seems ok, though whatever we show here will be incorrect // Since the page doesn't actually exist if (p.startsWith('*')) { if (preserveDynamicRoutes) { if (name === 'not-found') { return '+not-found'; } return `[...${name}]`; } else if (params[name]) { if (Array.isArray(params[name])) { return params[name].join('/'); } return params[name]; } else if (route.name.startsWith('[') && route.name.endsWith(']')) { return ''; } else if (p === '*not-found') { return ''; } else { return route.name; } } // If the path has a pattern for a param, put the param in the path if (p.startsWith(':')) { if (preserveDynamicRoutes) { return `[${name}]`; } // Optional params without value assigned in route.params should be ignored const value = params[name]; if (value === undefined && p.endsWith('?')) { return; } return (shouldEncodeURISegment ? encodeURISegment(value) : value) ?? 'undefined'; } if (!preserveGroups && (0, _matchers.matchGroupName)(p) != null) { // When the last part is a group it could be a shared URL // if the route has an initialRouteName defined, then we should // use that as the component path as we can assume it will be shown. if (segments.length - 1 === i) { if (initialRouteName) { // Return an empty string if the init route is ambiguous. if (segmentMatchesConvention(initialRouteName)) { return ''; } return shouldEncodeURISegment ? encodeURISegment(initialRouteName, { preserveBrackets: true }) : initialRouteName; } } return ''; } // Preserve dynamic syntax so the path can be parsed back into state return shouldEncodeURISegment ? encodeURISegment(p, { preserveBrackets: true }) : p; }).map((v)=>v ?? '').join('/'); } const getParamName = (pattern)=>pattern.replace(/^[:*]/, '').replace(/\?$/, ''); function isDynamicPart(p) { return p.startsWith(':') || p.startsWith('*'); } function segmentMatchesConvention(segment) { return segment === 'index' || (0, _matchers.matchGroupName)(segment) != null || (0, _matchers.matchDynamicName)(segment) != null; } function encodeURISegment(str, { preserveBrackets = false } = {}) { // Valid characters according to // https://datatracker.ietf.org/doc/html/rfc3986#section-3.3 (see pchar definition) str = String(str).replace(/[^A-Za-z0-9\-._~!$&'()*+,;=:@]/g, (char)=>encodeURIComponent(char)); if (preserveBrackets) { // Preserve brackets str = str.replace(/%5B/g, '[').replace(/%5D/g, ']'); } return str; } //# sourceMappingURL=getPathFromState-forks.js.map