# 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](https://rjrodger.github.io/patrun/reference/) states each function in full.

## Choosing the match

Decide which pattern answers a query, and how strictly.

- [Pick the most specific match](https://rjrodger.github.io/patrun/how-to/pick-most-specific.html): Add a general pattern and its special cases, and let find answer with the most specific one that matches.
- [Choose which keys come first](https://rjrodger.github.io/patrun/how-to/choose-key-order.html): Name the keys that decide first between patterns that match as many properties, with the order option.
- [Require every key of the query](https://rjrodger.github.io/patrun/how-to/require-all-keys.html): Find only a pattern that has every property of the query, with findExact.
- [Collect every match](https://rjrodger.github.io/patrun/how-to/collect-matches.html): Get 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 key](https://rjrodger.github.io/patrun/how-to/require-a-key.html): Give a pattern the value '*' to match a query that has the key, whatever its value.
- [Match values with glob expressions](https://rjrodger.github.io/patrun/how-to/match-globs.html): Turn on the gex option to let a pattern value use * and ? to match many values.
- [Match numbers by interval](https://rjrodger.github.io/patrun/how-to/match-intervals.html): Turn 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 pattern](https://rjrodger.github.io/patrun/how-to/get-one-pattern.html): Read back the data stored at exactly one pattern with get, not the data of the pattern that best matches.
- [List the patterns a router holds](https://rjrodger.github.io/patrun/how-to/list-patterns.html): List every pattern and its data with list, or only those that have the properties of a partial pattern.
- [Remove a pattern](https://rjrodger.github.io/patrun/how-to/remove-a-pattern.html): Clear the data of exactly one pattern with remove, leaving every other pattern as it was.
- [Inspect a router](https://rjrodger.github.io/patrun/how-to/inspect-the-tree.html): Print 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 patrun](https://rjrodger.github.io/patrun/how-to/install.html): Add patrun to a JavaScript, TypeScript, or Go project, and check which version you have.
- [Build a table of business rules](https://rjrodger.github.io/patrun/how-to/business-rules.html): Replace a ladder of if statements with a router of rules, general and special, each added on its own.
- [Customise stored data](https://rjrodger.github.io/patrun/how-to/customise-data.html): Give patrun a function that changes each pattern as it is added, and that changes what find gives back.
- [Use patrun from TypeScript](https://rjrodger.github.io/patrun/how-to/typescript.html): Type a router's data, so that add takes only data of that type and find answers with it.
- [Share a Go router between goroutines](https://rjrodger.github.io/patrun/how-to/goroutines.html): Read a Go router from many goroutines at once, and guard it with a lock when its patterns change while it is in use.
