one
Version:
One is a new React Framework that makes Vite serve both native and web.
148 lines • 4.67 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var findRouteNode_exports = {};
__export(findRouteNode_exports, {
extractParamsFromState: () => extractParamsFromState,
extractPathnameFromHref: () => extractPathnameFromHref,
extractSearchFromHref: () => extractSearchFromHref,
findAllRouteNodesFromState: () => findAllRouteNodesFromState,
findRouteNodeFromState: () => findRouteNodeFromState
});
module.exports = __toCommonJS(findRouteNode_exports);
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;
}