one
Version:
One is a new React Framework that makes Vite serve both native and web.
343 lines (341 loc) • 15.3 kB
JavaScript
import escape from "escape-string-regexp";
import { getParamValue, isDynamicPart, replacePart } from "./_shared.native.js";
import { findFocusedRoute } from "./findFocusedRoute.native.js";
import { appendIsInitial, createConfigItemAdditionalProperties, decodeURIComponentSafe, formatRegexPattern, getRouteConfigSorter, getUrlWithReactNavigationConcessions, matchForEmptyPath, parseQueryParamsExtended, populateParams } from "./getStateFromPath-mods.native.js";
import { validatePathConfig } from "./validatePathConfig.native.js";
function _type_of(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol < "u" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
function getStateFromPath(path, options) {
var _options_path,
{
initialRoutes,
configs,
configWithRegexes
} = getConfigResources(options),
screens = options?.screens,
pathData = getUrlWithReactNavigationConcessions(path),
remaining = pathData.nonstandardPathname.replace(/\/+/g, "/").replace(/^\//, "").replace(/\?.*$/, "");
remaining = remaining.endsWith("/") ? remaining : `${remaining}/`;
var prefix = options == null || (_options_path = options.path) === null || _options_path === void 0 ? void 0 : _options_path.replace(/^\//, "");
if (prefix) {
var normalizedPrefix = prefix.endsWith("/") ? prefix : `${prefix}/`;
if (!remaining.startsWith(normalizedPrefix)) return;
remaining = remaining.replace(normalizedPrefix, "");
}
if (screens === void 0) {
var routes = remaining.split("/").filter(Boolean).map(function (segment) {
var name = decodeURIComponent(segment);
return {
name
};
});
return routes.length ? createNestedStateObject(pathData, routes, initialRoutes, []) : void 0;
}
if (remaining === "/") {
var match = matchForEmptyPath(configWithRegexes);
return match ? createNestedStateObject(pathData,
// @modified: pass pathData instead of path
match.routeNames.map(function (name) {
return {
name
};
}), initialRoutes, configs) : void 0;
}
var result,
current,
{
routes: routes1,
remainingPath
} = matchAgainstConfigs(remaining, configWithRegexes);
if (routes1 !== void 0 && (current = createNestedStateObject(pathData, routes1, initialRoutes, configs), remaining = remainingPath, result = current), !(current == null || result == null)) return result;
}
var cachedConfigResources = /* @__PURE__ */new WeakMap();
function getConfigResources(options) {
if (!options) return prepareConfigResources();
var cached = cachedConfigResources.get(options);
if (cached) return cached;
var resources = prepareConfigResources(options);
return cachedConfigResources.set(options, resources), resources;
}
function prepareConfigResources(options, previousSegments) {
options && validatePathConfig(options);
var initialRoutes = getInitialRoutes(options),
configs = getNormalizedConfigs(initialRoutes, options?.screens, previousSegments);
checkForDuplicatedConfigs(configs);
var configWithRegexes = getConfigsWithRegexes(configs);
return {
initialRoutes,
configs,
configWithRegexes
};
}
function getInitialRoutes(options) {
var initialRoutes = [];
return options?.initialRouteName && initialRoutes.push({
initialRouteName: options.initialRouteName,
parentScreens: []
}), initialRoutes;
}
function getNormalizedConfigs(initialRoutes) {
var screens = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {},
previousSegments = arguments.length > 2 ? arguments[2] : void 0;
return [].concat(...Object.keys(screens).map(function (key) {
return createNormalizedConfigs(key, screens, [], initialRoutes, []);
})).map(appendIsInitial(initialRoutes)).sort(getRouteConfigSorter(previousSegments));
}
function checkForDuplicatedConfigs(configs) {
configs.reduce(function (acc, config) {
if (acc[config.pattern]) {
var a = acc[config.pattern].routeNames,
b = config.routeNames,
intersects = a.length > b.length ? b.every(function (it, i) {
return a[i] === it;
}) : a.every(function (it, i) {
return b[i] === it;
});
if (!intersects) throw new Error(`Found conflicting screens with the same pattern. The pattern '${config.pattern}' resolves to both '${a.join(" > ")}' and '${b.join(" > ")}'. Patterns must be unique and cannot resolve to more than one screen.`);
}
return Object.assign(acc, {
[config.pattern]: config
});
}, {});
}
function getConfigsWithRegexes(configs) {
return configs.map(function (c) {
return {
...c,
// Add `$` to the regex to make sure it matches till end of the path and not just beginning
// @modified - start
// regex: c.regex ? new RegExp(c.regex.source + '$') : undefined,
regex: c.pattern ? new RegExp(`^(${c.pattern.split("/").map(formatRegexPattern).join("")})$`) : void 0
};
});
}
var joinPaths = function () {
for (var _len = arguments.length, paths = new Array(_len), _key = 0; _key < _len; _key++) paths[_key] = arguments[_key];
return [].concat(...paths.map(function (p) {
return p.split("/");
})).filter(Boolean).join("/");
},
matchAgainstConfigs = function (remaining, configs) {
var routes,
remainingPath = remaining,
allParams = /* @__PURE__ */Object.create(null),
_iteratorNormalCompletion = !0,
_didIteratorError = !1,
_iteratorError = void 0;
try {
for (var _loop = function () {
var config = _step.value;
if (!config.regex) return "continue";
var match = remainingPath.match(config.regex);
if (match) {
var _config_pattern,
matchResult = (_config_pattern = config.pattern) === null || _config_pattern === void 0 ? void 0 : _config_pattern.split("/").reduce(
// Position of the current path param segment in the path (e.g in pattern `a/:b/:c`, `:a` is 0 and `:b` is 1)
// The extracted params
function (acc, p, index) {
if (!isDynamicPart(p)) return acc;
acc.pos += 1;
var decodedParamSegment = decodeURIComponentSafe(
// @modified: use decodeURIComponent**Safe**
// The param segments appear every second item starting from 2 in the regex match result
match[(acc.pos + 1) * 2].replace(/\/$/, ""));
return Object.assign(acc.matchedParams, {
[p]: Object.assign(acc.matchedParams[p] || {}, {
[index]: decodedParamSegment
})
}), acc;
}, {
pos: -1,
matchedParams: {}
}),
matchedParams = matchResult.matchedParams || {};
return routes = config.routeNames.map(function (name) {
var _routeConfig_pattern_replace,
routeConfig = configs.find(function (c) {
return c.screen === name && config.pattern.startsWith(c.pattern);
}),
normalizedPath = routeConfig?.path.split("/").filter(Boolean).join("/"),
numInitialSegments = routeConfig == null || (_routeConfig_pattern_replace = routeConfig.pattern.replace(new RegExp(`${escape(normalizedPath)}$`), "")) === null || _routeConfig_pattern_replace === void 0 ? void 0 : _routeConfig_pattern_replace.split("/").length,
params = normalizedPath?.split("/").reduce(function (acc, p, index) {
var _matchedParams_p;
if (!isDynamicPart(p)) return acc;
var offset = numInitialSegments ? numInitialSegments - 1 : 0,
value = getParamValue(p, (_matchedParams_p = matchedParams[p]) === null || _matchedParams_p === void 0 ? void 0 : _matchedParams_p[index + offset]);
if (value) {
var _routeConfig_parse,
key = replacePart(p);
acc[key] = !(routeConfig == null || (_routeConfig_parse = routeConfig.parse) === null || _routeConfig_parse === void 0) && _routeConfig_parse[key] ? routeConfig.parse[key](value) : value;
}
return acc;
}, {});
return params && Object.keys(params).length ? (Object.assign(allParams, params), {
name,
params
}) : {
name
};
}), remainingPath = remainingPath.replace(match[1], ""), "break";
}
}, _iterator = configs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
var _ret = _loop();
if (_ret === "break") break;
}
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
} finally {
try {
!_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
} finally {
if (_didIteratorError) throw _iteratorError;
}
}
return populateParams(routes, allParams), {
routes,
remainingPath
};
},
createNormalizedConfigs = function (screen, routeConfig) {
var routeNames = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : [],
initials = arguments.length > 3 ? arguments[3] : void 0,
parentScreens = arguments.length > 4 ? arguments[4] : void 0,
parentPattern = arguments.length > 5 ? arguments[5] : void 0,
configs = [];
routeNames.push(screen), parentScreens.push(screen);
var config = routeConfig[screen];
if (typeof config == "string") {
var pattern = parentPattern ? joinPaths(parentPattern, config) : config;
configs.push(createConfigItem(screen, routeNames, pattern, config));
} else if ((typeof config > "u" ? "undefined" : _type_of(config)) === "object") {
var pattern1;
if (typeof config.path == "string") {
if (config.exact && config.path === void 0) throw new Error("A 'path' needs to be specified when specifying 'exact: true'. If you don't want this screen in the URL, specify it as empty string, e.g. `path: ''`.");
pattern1 = config.exact !== !0 ? joinPaths(parentPattern || "", config.path || "") : config.path || "", configs.push(createConfigItem(screen, routeNames, pattern1, config.path, config.parse, config));
}
config.screens && (config.initialRouteName && initials.push({
initialRouteName: config.initialRouteName,
parentScreens
}), Object.keys(config.screens).forEach(function (nestedConfig) {
var result = createNormalizedConfigs(nestedConfig, config.screens, routeNames, initials, [...parentScreens], pattern1 ?? parentPattern);
configs.push(...result);
}));
}
return routeNames.pop(), configs;
},
createConfigItem = function (screen, routeNames, pattern, path) {
var parse = arguments.length > 4 && arguments[4] !== void 0 ? arguments[4] : void 0,
config = arguments.length > 5 && arguments[5] !== void 0 ? arguments[5] : {};
pattern = pattern.split("/").filter(Boolean).join("/");
var regex = pattern ? new RegExp(`^(${pattern.split("/").map(function (it) {
return it.startsWith(":") ? `(([^/]+\\/)${it.endsWith("?") ? "?" : ""})` : `${it === "*" ? ".*" : escape(it)}\\/`;
}).join("")})`) : void 0;
return {
screen,
regex,
pattern,
path,
// The routeNames array is mutated, so copy it to keep the current state
routeNames: [...routeNames],
parse,
// @modified - start
...createConfigItemAdditionalProperties(screen, pattern, routeNames, config)
};
},
findParseConfigForRoute = function (routeName, flatConfig) {
var _iteratorNormalCompletion = !0,
_didIteratorError = !1,
_iteratorError = void 0;
try {
for (var _iterator = flatConfig[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
var config = _step.value;
if (routeName === config.routeNames[config.routeNames.length - 1]) return config.parse;
}
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
} finally {
try {
!_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
} finally {
if (_didIteratorError) throw _iteratorError;
}
}
},
findInitialRoute = function (routeName, parentScreens, initialRoutes) {
var _iteratorNormalCompletion = !0,
_didIteratorError = !1,
_iteratorError = void 0;
try {
for (var _iterator = initialRoutes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
var config = _step.value;
if (parentScreens.length === config.parentScreens.length) {
for (var sameParents = !0, i = 0; i < parentScreens.length; i++) if (parentScreens[i].localeCompare(config.parentScreens[i]) !== 0) {
sameParents = !1;
break;
}
if (sameParents) return routeName !== config.initialRouteName ? config.initialRouteName : void 0;
}
}
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
} finally {
try {
!_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
} finally {
if (_didIteratorError) throw _iteratorError;
}
}
},
createStateObject = function (initialRoute, route, isEmpty) {
return isEmpty ? initialRoute ? {
index: 1,
routes: [{
name: initialRoute
}, route]
} : {
routes: [route]
} : initialRoute ? {
index: 1,
routes: [{
name: initialRoute
}, {
...route,
state: {
routes: []
}
}]
} : {
routes: [{
...route,
state: {
routes: []
}
}]
};
},
createNestedStateObject = function (param, routes, initialRoutes, flatConfig) {
var {
path,
...restPathData
} = param,
route = routes.shift(),
parentScreens = [],
initialRoute = findInitialRoute(route.name, parentScreens, initialRoutes);
parentScreens.push(route.name);
var state = createStateObject(initialRoute, route, routes.length === 0);
if (routes.length > 0) for (var nestedState = state; route = routes.shift();) {
initialRoute = findInitialRoute(route.name, parentScreens, initialRoutes);
var nestedStateIndex = nestedState.index || nestedState.routes.length - 1;
nestedState.routes[nestedStateIndex].state = createStateObject(initialRoute, route, routes.length === 0), routes.length > 0 && (nestedState = nestedState.routes[nestedStateIndex].state), parentScreens.push(route.name);
}
route = findFocusedRoute(state), route.path = restPathData.pathWithoutGroups;
var params = parseQueryParamsExtended(path, route, flatConfig ? findParseConfigForRoute(route.name, flatConfig) : void 0, restPathData.hash);
return params && (route.params = {
...route.params,
...params
}), state;
};
export { getStateFromPath };
//# sourceMappingURL=getStateFromPath.native.js.map