one
Version:
One is a new React Framework that makes Vite serve both native and web.
70 lines (69 loc) • 3.17 kB
JavaScript
function findRouteNodeFromState(state, rootNode) {
if (!state || !rootNode) return null;
const currentRoute = state.routes[state.routes.length - 1];
if (!currentRoute) return null;
const matchingNode = findNodeByRouteName(rootNode, currentRoute.name);
if (!matchingNode) return null;
if (currentRoute.state && currentRoute.state.routes) {
const nestedResult = findRouteNodeFromState(currentRoute.state, matchingNode);
if (nestedResult) return nestedResult;
}
return matchingNode;
}
function findNodeByRouteName(node, routeName) {
if (node.route === routeName) return node;
for (const child of node.children) {
const found = findNodeByRouteName(child, routeName);
if (found) return found;
}
return null;
}
function extractParamsFromState(state) {
if (!state) return {};
const params = {};
for (const route of state.routes) route.params && Object.assign(params, route.params), route.state && Object.assign(params, extractParamsFromState(route.state));
return params;
}
function extractSearchFromHref(href) {
const searchIndex = href.indexOf("?");
if (searchIndex === -1) return {};
const searchString = href.slice(searchIndex + 1),
params = {};
return new URLSearchParams(searchString).forEach((value, key) => {
const existing = params[key];
existing === void 0 ? params[key] = value : Array.isArray(existing) ? existing.push(value) : params[key] = [existing, value];
}), params;
}
function extractPathnameFromHref(href) {
const searchIndex = href.indexOf("?"),
hashIndex = href.indexOf("#");
let endIndex = href.length;
return searchIndex !== -1 && (endIndex = Math.min(endIndex, searchIndex)), hashIndex !== -1 && (endIndex = Math.min(endIndex, hashIndex)), href.slice(0, endIndex);
}
function findAllRouteNodesFromState(state, rootNode) {
if (!state || !rootNode) return [];
const nodes = [];
function collectNodes(currentState, parentNode) {
if (!currentState || !parentNode) return;
const currentRoute = currentState.routes[currentState.routes.length - 1];
if (!currentRoute) return;
const matchingNode = findNodeByRouteName(parentNode, currentRoute.name);
if (!matchingNode) {
process.env.NODE_ENV === "development" && console.log("[one] findAllRouteNodesFromState: could not find node for", currentRoute.name, "in", parentNode.route);
return;
}
if (nodes.push(matchingNode), currentRoute.state && currentRoute.state.routes) collectNodes(currentRoute.state, matchingNode);else if (currentRoute.params?.screen) {
const childRouteName = currentRoute.params.screen,
childNode = matchingNode.children.find(c => c.route === childRouteName);
childNode && (nodes.push(childNode), currentRoute.params.params && currentRoute.params.params.screen && collectNodes({
routes: [{
name: childRouteName,
params: currentRoute.params.params
}]
}, childNode));
}
}
return collectNodes(state, rootNode), nodes;
}
export { extractParamsFromState, extractPathnameFromHref, extractSearchFromHref, findAllRouteNodesFromState, findRouteNodeFromState };
//# sourceMappingURL=findRouteNode.mjs.map