Route patterns, parameters, queries, link resolution and a back and forward history, shared by web, native and server code.
import nav
Path matching and history for apps that move between screens. A route pattern such as /users/:id names the parts of a path, Match and Param read them, Rank orders patterns from the most specific, and History is a back and forward stack. The package has no state and no platform code, so the same rules hold in the browser (it compiles to JavaScript), in a native window and on a server.
Patterns
A pattern is a path whose segments are literal text, :name for one segment, :name? for a segment that may be missing, or *name (or *) at the end for the rest of the path. Literal segments are compared as written, so case matters, and a trailing / never does. Paths in a Route are decoded text, so Persian segments match as written; only the escapes the syntax reserves (%2F, %25, %26, %3D, %23, %3F, %2B) stay escaped until Param or Query decodes them.
Paths and links
Resolve follows the rule most app routers use: a relative link is resolved under the current path, not next to it. Href and StripBase turn a path into a link and back for an app served under a base path or in hash mode (#/path).
History
Push drops everything after the current position, like a browser does after you go back and follow a new link. Replace swaps the current entry, which suits a redirect.
Decodes only the escapes that the path and query syntax reserves: %2F, %25, %26, %3D, %23, %3F and %2B. Other text, and other escapes, stay as they are.
The example for this item is shown with the ones above: nav-Match.salam
Resolves a link against the current path. An absolute link replaces it, a link starting with ? keeps the path, and a relative link is resolved under the current path, so edit from /users/3 is /users/3/edit and .. is /users.
The example for this item is shown with the ones above: nav-Resolve.salam
Tells whether a path matches a pattern. A segment :name matches one segment, :name? may be missing, and *name or * at the end matches the rest. Literal segments are compared as written, and a trailing / does not matter.
The example for this item is shown with the ones above: nav-Match.salam
funcParamInt(pattern: str, path: str, name: str): int
The value of an integer parameter, such as the 42 in /users/42 for /users/:id; 0 when the path does not match or the value is not a whole number that fits in an int. Check it with IsInt first when 0 is a real value.
The example for this item is shown with the ones above: nav-Match.salam
A number that grows with how specific a pattern is: literal segments over parameters, parameters over optional ones, and a * last. Sort routes by it so /users/new wins over /users/:id, and /users wins over /users/:tab? for the path /users.
The example for this item is shown with the ones above: nav-Match.salam
Tells whether a text is a whole number that fits in an int (-2147483648 to 2147483647), with an optional minus sign. Use it to check a parameter before converting it.
The example for this item is shown with the ones above: nav-Match.salam