# What patterns and queries hold

The keys and values a pattern and a query may have in each port, and what each function does with anything else.

## In JavaScript

A pattern and a query are each a map: an object written as a literal, such as `{ a: 1 }` or what `JSON.parse` returns, or an object made with `Object.create(null)`. Nothing else is a map: not `null`, an array, a string, a number, a function, or an instance of a class, a `Map` among them.

Only what a map holds itself counts. A pattern's properties are the keys `Object.keys` lists. A query has a value for a key when the key is its own and the value is a string, a number, or a boolean.

| | A pattern | A query |
|---|---|---|
| a key | a string that is not empty | any string |
| a value | a string, a number, or a boolean | a string, a number, or a boolean, or else no value |
| `null` or `undefined` | the key is left out | the key has no value |

A number or a boolean is stored and matched as the text JavaScript gives it: `1.0` as `'1'`, and `1e21` as `'1e+21'`. So `{ a: 1 }` matches the pattern `{ a: '1' }`. Keys such as `__proto__` or `constructor` are strings like any other, and a router never changes other objects for them.

For anything that is not a map, or a pattern with an empty key or a value of another type:

| Function | Does |
|---|---|
| `add`, `remove` | throws a `TypeError` and changes nothing |
| `find`, `findExact` | gives `null` |
| `collect`, `collectExact` | gives `[]` |
| `list` | gives `[]`; `list()` and `list(null)` still list everything |
| `get` | gives `null` |

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

const pm = patrun().add({}, 'ROOT').add({ length: 3 }, 'LEN3')

pm.add(null, 'X') // throws TypeError
pm.add({ '': 1 }, 'X') // throws TypeError
pm.find('abc') // → null
pm.find([1, 2, 3]) // → null
pm.find({ length: 3 }) // → 'LEN3'
pm.find({}) // → 'ROOT'
```

A query must give the same value each time one of its keys is read. `find` may read a key more than once, so a getter, or a `Proxy`, that answers differently each time has no defined match.

## In Go

A pattern is a `map[string]string` that is not `nil`, none of whose keys is the empty string. Data is a `*string`, and `nil` is no data.

| Given | Does |
|---|---|
| `Add` or `Remove` with a `nil` pattern, or one with an empty key | panics, and changes nothing |
| `Add` with `nil` data | clears the pattern, as `Remove` does |
| `Find`, `FindExact`, or `Get` with `nil` | gives `nil` and `false` |
| `Get` with an empty key | gives `nil` and `false` |
| `List` with `nil` | lists every pattern |

```go
pm := patrun.New()
pm.Add(nil, s("X"))                        // panics
pm.Add(map[string]string{"": "1"}, s("X")) // panics
pm.Add(map[string]string{}, s("ROOT"))
pm.Add(map[string]string{"a": ""}, s("EMPTY"))

pm.Find(map[string]string{"a": ""}) // → "EMPTY"
pm.Find(nil)                        // → not found
```

The empty pattern matches every query but `nil`. A query key that is the empty string is one no pattern has: `Find` passes over it, and `FindExact` finds nothing. Go's `Add` and `Remove` panic for a pattern they cannot take, as `http.ServeMux.Handle` does.
