one
Version:
One is a new React Framework that makes Vite serve both native and web.
193 lines (191 loc) • 6.02 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;
}
var currentRoute = state.routes[state.routes.length - 1];
if (!currentRoute) {
return null;
}
var matchingNode = findNodeByRouteName(rootNode, currentRoute.name);
if (!matchingNode) {
return null;
}
if (currentRoute.state && currentRoute.state.routes) {
var nestedResult = findRouteNodeFromState(currentRoute.state, matchingNode);
if (nestedResult) {
return nestedResult;
}
}
return matchingNode;
}
function findNodeByRouteName(node, routeName) {
if (node.route === routeName) {
return node;
}
var _iteratorNormalCompletion = true,
_didIteratorError = false,
_iteratorError = void 0;
try {
for (var _iterator = node.children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var child = _step.value;
var found = findNodeByRouteName(child, routeName);
if (found) {
return found;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return null;
}
function extractParamsFromState(state) {
if (!state || !state.routes) {
return {};
}
var params = {};
var _iteratorNormalCompletion = true,
_didIteratorError = false,
_iteratorError = void 0;
try {
for (var _iterator = state.routes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var route = _step.value;
if (route.params) {
Object.assign(params, route.params);
}
if (route.state) {
Object.assign(params, extractParamsFromState(route.state));
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return params;
}
function extractSearchFromHref(href) {
var searchIndex = href.indexOf("?");
if (searchIndex === -1) {
return {};
}
var searchString = href.slice(searchIndex + 1);
var params = {};
var searchParams = new URLSearchParams(searchString);
searchParams.forEach(function (value, key) {
var 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) {
var searchIndex = href.indexOf("?");
var hashIndex = href.indexOf("#");
var 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 [];
}
var nodes = [];
function collectNodes(currentState, parentNode) {
var _currentRoute_params;
if (!currentState || !currentState.routes || !parentNode) {
return;
}
var currentRoute = currentState.routes[currentState.routes.length - 1];
if (!currentRoute) {
return;
}
var 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 = currentRoute.params) === null || _currentRoute_params === void 0 ? void 0 : _currentRoute_params.screen) {
var childRouteName = currentRoute.params.screen;
var childNode = matchingNode.children.find(function (c) {
return 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;
}
//# sourceMappingURL=findRouteNode.native.js.map