# How a match is chosen

Which patterns match a query, the order that decides between them, the forms of globs and intervals, and the order of a list.

These rules hold in both ports. Where a rule names a JavaScript function, the Go method of the same name, capitalised, follows it too.

## What matches a query

A pattern matches a query when the query has a value for every key of the pattern, and each value matches the pattern's value for that key. A key of the query that the pattern does not have makes no difference, except to `findExact` and `collectExact`, which take only a pattern that has every key the query has a value for.

A pattern's value for a key is one of:

| Value | Matches a query value that |
|---|---|
| an exact value, such as `'list'` or `10` | is the same text |
| `'*'`, the star | is any value, the empty string included |
| a glob, such as `'l*'`, with `gex` on | the glob takes |
| an interval, such as `'>10'`, with `interval` on | is a number in the interval |

The empty pattern, `{}`, has no keys and matches every query that is a map.

## The order of the matches

The patterns that match a query come in one order, and `find` gives the first. The order does not depend on the order of adding. Two tests decide it, in turn:

1. The pattern with more properties comes first.
2. Of patterns with as many properties, the properties of each are compared one by one, with their keys in the router's key order. The first property at which the two differ decides. If the keys differ there, the pattern whose key comes first in the key order comes first. If the key is the same, the value decides: an exact value comes first, then a glob, then an interval, then the star.

Two different patterns that match the same query always differ in one of the two tests, so there is never a tie.

The router's key order is the keys of the `order` option, in the order given, and then every other key in the order of its text, as code points: `Z` comes before `a`.

Of two globs, or two intervals, the narrower comes first, in the orders below. A glob comes before an interval, whichever is the narrower.

An earlier property decides before a later one. These two patterns differ first at `a`, where the exact value comes before the star, so `b` coming before `c` makes no difference:

```js
const patrun = require('patrun')

const pm = patrun()
  .add({ a: '*', b: 'x' }, 'AB')
  .add({ a: 1, c: 'x' }, 'AC')

pm.find({ a: 1, b: 'x', c: 'x' }) // → 'AC'
```

One pattern is narrower than another when it has every property of the other, or, where the other has a star, the key: every query that matches it then matches the other. The order always puts the narrower pattern first.

## Globs

With `gex` on, a pattern value that has a `*` or a `?` in it is a glob, unless it is `*` alone. `*` takes any run of characters and `?` any one. `**` is a star itself and `*?` a question mark itself, each a fixed character.

The globs of a key come in this order:

1. The glob with more fixed characters.
2. Then the one with more `?`s.
3. Then the one with fewer `*`s.
4. Then the one whose text comes first.

## Intervals

With `interval` on, a pattern value written with an operator or as a range is an interval:

| Form | Matches |
|---|---|
| `>10`, `>=10`, `<10`, `<=10` | numbers on that side of 10 |
| `=10` | the number 10 |
| `[10,20]` | 10 to 20, both included |
| `(10,20)` | 10 to 20, both left out |
| `(10,20]`, `[10,20)` | one end included |
| `10..20` | 10 to 20, both included |
| `>=10&<20` | both tests |
| `<10\|\|>20` | either test |

A value is a number only when the whole of it is a decimal number: an optional sign, digits with an optional fraction, and an optional exponent, as in `15`, `-1.5`, or `2e3`. `Infinity`, `NaN`, hexadecimal, underscores, and a value with spaces or anything after the number are not numbers. The numbers in a pattern are read the same way, and a value with any other is an exact value. A number on its own, such as `10`, is an exact value.

The intervals of a key come in this order:

1. An interval with two ends, then one that goes on for ever one way, such as `>10`, then one with a gap, such as `<10||>20`.
2. With two ends, the shorter first, and one number before any. One way for ever, the one that starts further along: `>20` before `>10`. With a gap, the longer gap first.
3. Then the one with fewer of its ends included: `(10,20)`, then `[10,20)`, then `[10,20]`.
4. Then by their numbers, the lower first.

An interval is one pattern however it is written: `[10,20]`, `10..20`, and `>=10&<=20` are the same. `=1` and `[1,1]` take the same number by different tests, and are two patterns.

## The order of a list

`list` and `collect` give patterns in one order. Two patterns are compared property by property, as the second test of the order of the matches compares them: the first property at which they differ decides, by its key in the router's key order, or, at the same key, by its value. The number of properties does not decide. A pattern comes before every pattern that begins with its properties, so `{ a: 1 }` comes before `{ a: 1, b: 2 }`.

The values of a key come in this order: the exact values, whole numbers first in numeric order and then the others as text; then the globs; then the intervals; then the star.

```js
const patrun = require('patrun')

const pm = patrun()
  .add({ a: 'x' }, 'X')
  .add({ a: 10 }, 'TEN')
  .add({ a: '*' }, 'ANY')
  .add({ a: 9 }, 'NINE')

pm.list().map((entry) => entry.data) // → ['NINE', 'TEN', 'X', 'ANY']
```

A pattern with more properties can come before one with fewer. Here `{ a: 1, b: 2 }` comes before `{ b: 2 }`, because `a` comes before `b`:

```js
const patrun = require('patrun')

const pm = patrun()
  .add({ b: 2 }, 'B')
  .add({ a: 1, b: 2 }, 'AB')
  .add({ a: 1 }, 'A')
  .add({}, 'ROOT')
  .add({ b: 2, c: 3 }, 'BC')

pm.list().map((entry) => entry.data) // → ['ROOT', 'A', 'AB', 'B', 'BC']
pm.collect({ a: 1, b: 2, c: 3 }) // → ['ROOT', 'A', 'AB', 'B', 'BC']
```

## The definition

For exact values and the star, these rules are defined in Lean 4, with a proof that the order has no ties and puts the narrower pattern first: see [The definition](https://rjrodger.github.io/patrun/explanation/definition.html).
