Router core
wybthon.router_core¶
router_core
¶
Core, browser-agnostic path matching and route resolution helpers.
This module is the algorithmic heart of Wybthon's router. It compiles route patterns to regular expressions, matches them against a pathname, and resolves the most specific match. Because it has no browser dependencies, it can be used in tests, in tooling, and on the server side.
Public surface:
RouteSpec: minimal dataclass used to describe routes for pure-Python tests.resolve: resolve a pathname to the best matching route and params.
See Also
Classes:
| Name | Description |
|---|---|
RouteSpec |
Minimal route spec used for pure-Python resolution in tests and tools. |
Functions:
| Name | Description |
|---|---|
resolve |
Resolve a pathname to the best matching route and params. |
RouteSpec
dataclass
¶
resolve
¶
resolve(routes: List[Any], pathname: str, base_path: str = '') -> Optional[Tuple[Any, Dict[str, Any]]]
Resolve a pathname to the best matching route and params.
The router prefers the most specific (longest) match and honors a
base_path prefix when provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
routes
|
List[Any]
|
Flat or nested route specs (any object exposing
|
required |
pathname
|
str
|
The current URL pathname. |
required |
base_path
|
str
|
Optional base path stripped from |
''
|
Returns:
| Type | Description |
|---|---|
Optional[Tuple[Any, Dict[str, Any]]]
|
A tuple |
Optional[Tuple[Any, Dict[str, Any]]]
|
|
What's in this module¶
router_core contains the browser-agnostic path matching engine used
by wybthon.router. It exposes:
RouteSpec: a normalized route description used internally byRoute.resolve: match a path against a list of route specs and return the matched chain plus extracted params.
You don't usually call this module directly. Write Route
declarations and let Router resolve them. Use
router_core if you need to test routing logic outside a browser
environment or build custom navigation tooling.
Path patterns¶
| Pattern | Matches | Notes |
|---|---|---|
/users |
/users |
Static segment. |
/users/:id |
/users/42 |
:id becomes a string param. |
/files/*splat |
/files/a/b/c |
Splat captures the remainder as a single string. |
/(public|private) |
/public or /private |
Alternation works in the underlying regex. |
Param values are URL-decoded before being passed to components.
See also¶
router: the browser-side router and components.- Concepts: Router
- Examples: Router