Build your first router
Install patrun, add patterns to a router, and find the data of the pattern that best matches each query, in JavaScript.
In this tutorial we build a router of sales-tax rates in JavaScript. We add a general rate and some special cases, ask the router for the rate of a sale, and see how it chooses between patterns that both match. It takes about 10 minutes.
You need Node.js 24 or later. Make a directory, and install patrun into it:
npm install patrun
Make a router #
A router holds patterns, each with its data. Make one, and add a pattern for Ireland's standard rate. Write this in tax.js:
const patrun = require('patrun')
const tax = patrun()
tax.add({ country: 'IE' }, 0.23)
tax.find({ country: 'IE' }) // → 0.23
A pattern is an object of properties. add stores the data 0.23 under the pattern { country: 'IE' }, and find gives back the data of the pattern that matches the query.
Ask about a sale #
A query can say more than a pattern does. Ask about a sale of books in Ireland:
tax.find({ country: 'IE', type: 'books' }) // → 0.23
The router has no pattern with type, so it ignores type and answers with the pattern for Ireland. A query matches a pattern when it has every property of the pattern, and whatever else it has makes no difference.
Add a special case #
Books in Ireland have a lower rate. Add a pattern for them:
tax.add({ country: 'IE', type: 'books' }, 0)
tax.find({ country: 'IE', type: 'books' }) // → 0
tax.find({ country: 'IE', type: 'food' }) // → 0.23
Both patterns match a sale of books, and the router chose the one with two properties. The more properties of a pattern a query matches, the more specific the pattern, and the most specific one wins. Food has no pattern of its own, so it still gets the standard rate.
Add another country #
Add Germany, with its own reduced rate:
tax.add({ country: 'DE' }, 0.19)
tax.add({ country: 'DE', type: 'books' }, 0.07)
tax.find({ country: 'DE', type: 'books' }) // → 0.07
tax.find({ country: 'FR' }) // → null
No pattern matches a sale in France, so find answers null. To give every query an answer, add the empty pattern, which every query matches:
tax.add({}, 0.2)
tax.find({ country: 'FR' }) // → 0.2
tax.find({ country: 'DE' }) // → 0.19
The empty pattern has no properties, so every other pattern that matches is more specific, and it answers only when no other pattern does.
See every pattern that matches #
collect gives the data of every pattern that matches a query:
tax.collect({ country: 'IE', type: 'books' }) // → [0.2, 0.23, 0]
That is the empty pattern, then Ireland, then books in Ireland. Each begins with the properties of the one before, so here they come from the least specific to the most. find answers with the one that has the most properties, books in Ireland.
List what the router holds #
list gives every pattern with its data:
tax.list().length // → 5
tax.list({ country: 'DE' }).map((entry) => entry.data) // → [0.19, 0.07]
Given a partial pattern, it lists only the patterns that have its properties. Each entry has the pattern and its data, and the values of a pattern come back as strings.
The order of adding does not matter #
Build the same router again, adding the patterns in the opposite order:
const again = patrun()
.add({}, 0.2)
.add({ country: 'DE', type: 'books' }, 0.07)
.add({ country: 'DE' }, 0.19)
.add({ country: 'IE', type: 'books' }, 0)
.add({ country: 'IE' }, 0.23)
again.find({ country: 'IE', type: 'books' }) // → 0
again.toString() === tax.toString() // → true
add returns the router, so the calls chain. The two routers hold the same patterns, and they give the same answers: a router's answer depends on its patterns alone. We have built a table of rules that we can add to one case at a time!
Next #
To decide which of two patterns wins when they match the same number of properties, see Choose which keys come first. The rules that order every match are in How a match is chosen.