Standard library · Web and services

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
The current place in an app: the path, the query and the pattern that matched it.
NameTypeDescription
pathstr = "/"The path, starting with /, without the query. Text in it is already decoded.
querystr = ""The query without the leading ?, as written in the address.
patternstr = ""The route pattern that matched the path, or an empty text when none did.
nav-Match.salam
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
end
Outputtrue 42 43 true a/b.txt hello world a/b true true /users/42 tab=posts /users/:id

Param

func Param(name: str): str
The value of the named parameter of the route pattern, unescaped; an empty text when the path does not match.

The example for this item is shown with the ones above: nav-Match.salam

Query

func Query(name: str): str
The value of the named query key, unescaped, with + read as a space; an empty text when the key is missing.

The example for this item is shown with the ones above: nav-Match.salam

History

struct History
A stack of visited paths with a position, the way a native window keeps its back and forward list.
NameTypeDescription
entriesVectorThe visited paths, oldest first.
indexint = 0The position of the current path in entries.
nav-History.salam
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
end
Output/users/3 depth 3 true /users can go forward true /teams can go back true true /home 1 0

Functions

Unescape

func Unescape(s: str): str
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

Normalize

func Normalize(path: str): str
Gives a path one shape: a leading /, no empty segments, . and .. applied, no trailing /.
nav-Resolve.salam
import 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")
end
Output/a/b/d /search | q=salam /users/3/edit /users /users/3?page=2 #/about /app/about /about

SplitUrl

func SplitUrl(url: str, path &: str, query &: str)
Splits an address into its path and its query, and drops the #fragment.

The example for this item is shown with the ones above: nav-Resolve.salam

Resolve

func Resolve(current: str, target: str): str
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

Match

func Match(pattern: str, path: str): bool
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

Param

func Param(pattern: str, path: str, name: str): str
The value of one parameter of a pattern in a path, unescaped; an empty text when the path does not match. A * parameter gives the rest of the path.

The example for this item is shown with the ones above: nav-Match.salam

ParamInt

func ParamInt(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

Query

func Query(query: str, name: str): str
The value of one query parameter, with + read as a space and reserved escapes decoded; an empty text when it is missing.

The example for this item is shown with the ones above: nav-Match.salam

Rank

func Rank(pattern: str): int
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

IsInt

func IsInt(s: str): bool
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

Href

func Href(hash: bool, base: str, path: str): str
The address of a path for a link: #/path in hash mode, base plus path otherwise.

The example for this item is shown with the ones above: nav-Resolve.salam

StripBase

func StripBase(base: str, path: str): str
Removes the base path of an app from an address and normalizes the rest; an address outside the base is only normalized.

The example for this item is shown with the ones above: nav-Resolve.salam

NewHistory

func NewHistory(start: str): History
A history that starts at one path.

The example for this item is shown with the ones above: nav-History.salam

Current

func Current(h: History): str
The path at the current position.

The example for this item is shown with the ones above: nav-History.salam

Push

func Push(h &: History, path: str)
Goes to a new path: everything after the current position is dropped and the path becomes the newest entry.

The example for this item is shown with the ones above: nav-History.salam

Replace

func Replace(h &: History, path: str)
Swaps the current path for another without adding an entry.

The example for this item is shown with the ones above: nav-History.salam

CanBack

func CanBack(h: History): bool
Tells whether there is a path before the current one.

The example for this item is shown with the ones above: nav-History.salam

CanForward

func CanForward(h: History): bool
Tells whether there is a path after the current one.

The example for this item is shown with the ones above: nav-History.salam

Back

func Back(h &: History): bool
Moves one entry back; false when already at the oldest.

The example for this item is shown with the ones above: nav-History.salam

Forward

func Forward(h &: History): bool
Moves one entry forward; false when already at the newest.

The example for this item is shown with the ones above: nav-History.salam

Reset

func Reset(h &: History, path: str)
Clears the history and starts again at one path.

The example for this item is shown with the ones above: nav-History.salam

Depth

func Depth(h: History): int
How many entries lie up to and including the current one.

The example for this item is shown with the ones above: nav-History.salam