ziko
Version:
A versatile JavaScript library offering a rich set of Hyperscript Based UI components, advanced mathematical utilities, interactivity ,animations, client side routing and more ...
22 lines • 836 B
JavaScript
export function is_dynamic(path) {
const DynamicPattern = /(:\w+|\[\.\.\.\w+\]|\[\w+\]\+?)/;
return DynamicPattern.test(path);
}
export function routes_grouper(routeMap) {
const grouped = {
static: {},
dynamic: {},
};
for (const [path, value] of Object.entries(routeMap)) {
if (is_dynamic(path)) {
const segments = path.split("/").filter(Boolean);
const optionalIndex = segments.findIndex(seg => seg.endsWith("]+"));
const hasInvalidOptional =
optionalIndex !== -1 && optionalIndex !== segments.length - 1;
if (hasInvalidOptional) throw new Error(`Invalid optional param position in route: "${path}" — optional parameters can only appear at the end.`);
grouped.dynamic[path] = value;
}
else grouped.static[path] = value;
}
return grouped;
}