JavaScript API
The patrun function, its options, and every method of a router, in JavaScript.
The package exports one function, patrun, which makes a router. Load it with require('patrun'), or import patrun from 'patrun'.
patrun(custom?) #
Makes a router. It is also exported by name as Patrun, and both can be called with new. Gex, the glob library the gex option uses, is exported too.
custom is one of:
- omitted: a plain router. A pattern value of
'*'takes any value, here and with every option. - an object of options, any of:
gex: trueturns on glob matching for values that have a*or a?in them. A*on its own is not a glob.interval: trueturns on matching of numbers by interval, for values such as>10or[10,20].order: ['srv', 'role']names the keys that come first in the router's key order, in the order given. Every other key comes after them, in the order of its text. The option is an array of strings, none empty and none twice, orpatrunthrows aTypeError.
- a function, the customiser, which
addcalls with each pattern and its data. It may change the pattern, and may return a function thatfindcalls on the data, or an object withfindandremovefunctions. Set options on the function itself to have both:custom.order = ['srv']. See Customise stored data.
.add(pattern, data) #
Stores data under pattern, and returns the router. Adding a pattern again replaces its data.
pattern is a map whose keys are not empty and whose values are strings, numbers, or booleans. A key whose value is null or undefined is left out. For anything else add throws a TypeError and changes nothing: see What patterns and queries hold.
data may be anything but null and undefined. With either, add leaves the pattern holding nothing, as remove does.
.find(query, exact?, collect?) #
Gives the data of the pattern that best matches query, or null if none matches. The best match is the first in the order of How a match is chosen. Keys of query that no pattern has are ignored.
exact(defaultfalse): take only a pattern that has every keyqueryhas a value for.collect(defaultfalse): give an array of the data of every pattern that matches, empty if none does, in place of the data of the best one.
.findExact(query) #
The same as .find(query, true). .findexact(query) is its earlier spelling.
.collect(query) #
The same as .find(query, false, true): an array of the data of every pattern that matches query, in the order of a list.
.collectExact(query) #
The same as .find(query, true, true): an array of the data of every pattern that matches query and has every key it has a value for, in the order find would choose them.
.get(pattern) #
Gives the data stored at exactly pattern, or null if it holds none. A glob and the star are had as themselves, and an interval by any of its spellings. The data is given as it was added, and a customiser's function is not called. For a pattern add would refuse, gives null.
.list(part?, exact?) #
Gives an array of { pattern, data, find }, one for each pattern that has every property of the partial pattern part. With no part, or null, lists every pattern. A value of part is had exactly, or is '*' for any value. A key part leaves out is not a wildcard. pattern has its values as strings, and find is the customiser's function, if there is one.
exact(defaultfalse): list only the patterns that have no keys other than those ofpart.
A part that is not a map, or that has a key or value no pattern can have, lists nothing. The order is in How a match is chosen.
.remove(pattern) #
Clears the data stored at exactly pattern. A pattern that was never added changes nothing. A customiser's remove function keeps the data unless it returns a true value. Throws a TypeError for a pattern add would refuse.
.toString(formatter?, tree?) #
Gives the patterns and their data as text, one pattern to a line: its pairs as key=value, then ->, then its data.
formatter(data): formats each piece of data. Without one, data is written in angle brackets.tree(defaultfalse): withtrue, writes the tree indented, in place of one line for each pattern.
.inspect is another name for .toString.
.toJSON(indent?) #
Gives the router's tree as JSON text, indented as JSON.stringify indents with indent: a number of spaces, or a string of spaces and tabs.
A node has, in this order, any of:
| Member | Holds |
|---|---|
k |
the node's key |
v |
the node of each exact value of the key, and of the star |
g |
the globs and intervals of the key, each with its node as keymap |
s |
the skip path, to the patterns that do not have the key |
d |
the data |
Values come in the order list gives them, so the text depends only on the patterns and their data. An interval written in more than one way shows as it was last written, and a function as "[Function]". JSON.stringify calls toJSON with the key of the member the router is the value of; a key is not an indent, so the router is written as its text with no indent, as a string.