How-to guides
Recipes for one task each, in JavaScript and in Go.
Each guide does one thing, and assumes you have a router to do it with. A guide shows the JavaScript first and the Go after it, where the Go port does the same. The reference states each function in full.
Choosing the match #
Decide which pattern answers a query, and how strictly.
- Pick the most specific matchAdd a general pattern and its special cases, and let find answer with the most specific one that matches.
- Choose which keys come firstName the keys that decide first between patterns that match as many properties, with the order option.
- Require every key of the queryFind only a pattern that has every property of the query, with findExact.
- Collect every matchGet the data of every pattern that matches a query, in the router's key order, with collect and collectExact.
Matching values #
Patterns whose values take more than one value.
- Match any value of a keyGive a pattern the value '*' to match a query that has the key, whatever its value.
- Match values with glob expressionsTurn on the gex option to let a pattern value use * and ? to match many values.
- Match numbers by intervalTurn on the interval option to let a pattern value match a range of numbers, such as >10 or [10,20].
Managing patterns #
Read back, list, change and remove what a router holds.
- Get the data of one patternRead back the data stored at exactly one pattern with get, not the data of the pattern that best matches.
- List the patterns a router holdsList every pattern and its data with list, or only those that have the properties of a partial pattern.
- Remove a patternClear the data of exactly one pattern with remove, leaving every other pattern as it was.
- Inspect a routerPrint a router's patterns and data with toString, or its tree as JSON with toJSON, to debug it or compare two routers.
Using it in a program #
Fit a router into the code around it.
- Install patrunAdd patrun to a JavaScript, TypeScript, or Go project, and check which version you have.
- Build a table of business rulesReplace a ladder of if statements with a router of rules, general and special, each added on its own.
- Customise stored dataGive patrun a function that changes each pattern as it is added, and that changes what find gives back.
- Use patrun from TypeScriptType a router's data, so that add takes only data of that type and find answers with it.
- Share a Go router between goroutinesRead a Go router from many goroutines at once, and guard it with a lock when its patterns change while it is in use.