UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

120 lines (119 loc) 3.64 kB
function findRouteNodeFromState(state, rootNode) { if (!state || !state.routes || !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 || !state.routes) { return {}; } const params = {}; for (const route of state.routes) { if (route.params) { Object.assign(params, route.params); } if (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); const params = {}; const searchParams = new URLSearchParams(searchString); searchParams.forEach((value, key) => { const existing = params[key]; if (existing === void 0) { params[key] = value; } else if (Array.isArray(existing)) { existing.push(value); } else { params[key] = [existing, value]; } }); return params; } function extractPathnameFromHref(href) { const searchIndex = href.indexOf("?"); const hashIndex = href.indexOf("#"); let endIndex = href.length; if (searchIndex !== -1) endIndex = Math.min(endIndex, searchIndex); if (hashIndex !== -1) endIndex = Math.min(endIndex, hashIndex); return href.slice(0, endIndex); } function findAllRouteNodesFromState(state, rootNode) { if (!state || !state.routes || !rootNode) { return []; } const nodes = []; function collectNodes(currentState, parentNode) { if (!currentState || !currentState.routes || !parentNode) { return; } const currentRoute = currentState.routes[currentState.routes.length - 1]; if (!currentRoute) { return; } const matchingNode = findNodeByRouteName(parentNode, currentRoute.name); if (!matchingNode) { if (process.env.NODE_ENV === "development") { console.log("[one] findAllRouteNodesFromState: could not find node for", currentRoute.name, "in", parentNode.route); } return; } nodes.push(matchingNode); if (currentRoute.state && currentRoute.state.routes) { collectNodes(currentRoute.state, matchingNode); } else if (currentRoute.params?.screen) { const childRouteName = currentRoute.params.screen; const childNode = matchingNode.children.find(c => c.route === childRouteName); if (childNode) { nodes.push(childNode); if (currentRoute.params.params && currentRoute.params.params.screen) { collectNodes({ routes: [{ name: childRouteName, params: currentRoute.params.params }] }, childNode); } } } } collectNodes(state, rootNode); return nodes; } export { extractParamsFromState, extractPathnameFromHref, extractSearchFromHref, findAllRouteNodesFromState, findRouteNodeFromState }; //# sourceMappingURL=findRouteNode.mjs.map