expo-router
Version:
Expo Router is a file-based router for React Native and web applications.
96 lines (94 loc) • 3.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
0 && (module.exports = {
resolveHref: null,
resolveHrefStringWithSegments: 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 resolveHref () {
return resolveHref;
},
get resolveHrefStringWithSegments () {
return resolveHrefStringWithSegments;
}
});
const resolveHref = (href)=>{
if (typeof href === 'string') {
return href;
}
const path = href.pathname ?? '';
if (!href?.params) {
return path;
}
const { pathname, params } = createQualifiedPathname(path, {
...href.params
});
const paramsString = createQueryParams(params);
return pathname + (paramsString ? `?${paramsString}` : '');
};
function resolveHrefStringWithSegments(href, { segments = [], params = {} } = {}, { relativeToDirectory } = {}) {
if (href.startsWith('.')) {
// Resolve base path by merging the current segments with the params
let base = segments?.map((segment)=>{
if (!segment.startsWith('[')) return segment;
if (segment.startsWith('[...')) {
segment = segment.slice(4, -1);
const param = params[segment];
if (Array.isArray(param)) {
return param.join('/');
} else {
return param?.split(',')?.join('/') ?? '';
}
} else {
segment = segment.slice(1, -1);
return params[segment];
}
}).filter(Boolean).join('/') ?? '/';
if (relativeToDirectory) {
base = `${base}/`;
}
const url = new URL(href, `http://hostname/${base}`);
href = `${url.pathname}${url.search}`;
}
// Collapse leading slashes so a scheme-relative `//host/...` href — whether
// smuggled in directly or produced by URL resolution above — cannot route
// navigation cross-origin.
return href.replace(/^\/+/, '/');
}
function createQualifiedPathname(pathname, params) {
for (const [key, value = ''] of Object.entries(params)){
const dynamicKey = `[${key}]`;
const deepDynamicKey = `[...${key}]`;
if (pathname.includes(dynamicKey)) {
pathname = pathname.replace(dynamicKey, encodeParam(value));
} else if (pathname.includes(deepDynamicKey)) {
pathname = pathname.replace(deepDynamicKey, encodeParam(value));
} else {
continue;
}
delete params[key];
}
return {
pathname,
params
};
}
function encodeParam(param) {
if (Array.isArray(param)) {
return param.map((p)=>encodeParam(p)).join('/');
}
return encodeURIComponent(param.toString());
}
function createQueryParams(params) {
return Object.entries(params)// Allow nullish params
.filter(([, value])=>value != null).map(([key, value])=>`${key}=${encodeURIComponent(value.toString())}`).join('&');
}
//# sourceMappingURL=href.js.map