nav
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.
Types
Route
struct Route| Name | Type | Description |
|---|---|---|
path | str = "/" | The path, starting with /, without the query. Text in it is already decoded. |
query | str = "" | The query without the leading ?, as written in the address. |
pattern | str = "" | The route pattern that matched the path, or an empty text when none did. |
import nav
func main:
println nav.Match("/users/:id", "/users/42")
println nav.Param("/users/:id", "/users/42", "id")
println nav.ParamInt("/users/:id", "/users/42", "id") + 1
println nav.Match("/users/:id?", "/users")
println nav.Param("/files/*path", "/files/a/b.txt", "path")
println nav.Query("q=hello+world&page=2", "q")
println nav.Unescape("a%2Fb")
println nav.Rank("/users/new") > nav.Rank("/users/:id")
println nav.IsInt("42")
r := nav.Route { path = "/users/42", query = "tab=posts", pattern = "/users/:id" }
println r.path + " " + r.query + " " + r.pattern
endParam
func Param(name: str): strThe example for this item is shown with the ones above: nav-Match.salam
Query
func Query(name: str): strThe example for this item is shown with the ones above: nav-Match.salam
History
struct History| Name | Type | Description |
|---|---|---|
entries | Vector | The visited paths, oldest first. |
index | int = 0 | The position of the current path in entries. |
import nav
func main:
mut h := nav.NewHistory("/")
nav.Push(h, "/users")
nav.Push(h, "/users/3")
println nav.Current(h) + " depth " + nav.Depth(h)
println nav.Back(h)
println nav.Current(h) + " can go forward " + nav.CanForward(h)
nav.Replace(h, "/teams")
println nav.Current(h) + " can go back " + nav.CanBack(h)
println nav.Forward(h)
nav.Reset(h, "/home")
println nav.Current(h) + " " + h.entries.Len() + " " + h.index
endFunctions
Unescape
func Unescape(s: str): strThe example for this item is shown with the ones above: nav-Match.salam
Normalize
func Normalize(path: str): strimport nav
func main:
println nav.Normalize("a//b/./c/../d/")
mut path := ""
mut query := ""
nav.SplitUrl("/search?q=salam#top", path, query)
println path + " | " + query
println nav.Resolve("/users/3", "edit")
println nav.Resolve("/users/3", "..")
println nav.Resolve("/users/3", "?page=2")
println nav.Href(true, "", "/about")
println nav.Href(false, "/app", "/about")
println nav.StripBase("/app", "/app/about")
endSplitUrl
func SplitUrl(url: str, path &: str, query &: str)The example for this item is shown with the ones above: nav-Resolve.salam
Resolve
func Resolve(current: str, target: str): strThe example for this item is shown with the ones above: nav-Resolve.salam
Match
func Match(pattern: str, path: str): boolThe example for this item is shown with the ones above: nav-Match.salam
Param
func Param(pattern: str, path: str, name: str): strThe example for this item is shown with the ones above: nav-Match.salam
ParamInt
func ParamInt(pattern: str, path: str, name: str): intThe example for this item is shown with the ones above: nav-Match.salam
Query
func Query(query: str, name: str): strThe example for this item is shown with the ones above: nav-Match.salam
Rank
func Rank(pattern: str): intThe example for this item is shown with the ones above: nav-Match.salam
IsInt
func IsInt(s: str): boolThe example for this item is shown with the ones above: nav-Match.salam
Href
func Href(hash: bool, base: str, path: str): strThe example for this item is shown with the ones above: nav-Resolve.salam
StripBase
func StripBase(base: str, path: str): strThe example for this item is shown with the ones above: nav-Resolve.salam
NewHistory
func NewHistory(start: str): HistoryThe example for this item is shown with the ones above: nav-History.salam
Current
func Current(h: History): strThe example for this item is shown with the ones above: nav-History.salam
Push
func Push(h &: History, path: str)The example for this item is shown with the ones above: nav-History.salam
Replace
func Replace(h &: History, path: str)The example for this item is shown with the ones above: nav-History.salam
CanBack
func CanBack(h: History): boolThe example for this item is shown with the ones above: nav-History.salam
CanForward
func CanForward(h: History): boolThe example for this item is shown with the ones above: nav-History.salam
Back
func Back(h &: History): boolThe example for this item is shown with the ones above: nav-History.salam
Forward
func Forward(h &: History): boolThe example for this item is shown with the ones above: nav-History.salam
Reset
func Reset(h &: History, path: str)The example for this item is shown with the ones above: nav-History.salam
Depth
func Depth(h: History): intThe example for this item is shown with the ones above: nav-History.salam