# Changes What each release of the npm package changed. The Go module has its own list in [go/CHANGELOG.md](go/CHANGELOG.md). ## 8.0.0 `find` now answers by its documented rules in every case. A pattern value of `*` needs no option, a matcher can be told which keys come first, and the package has TypeScript types. What a matcher takes, keeps and answers is written down for every input, and for exact values and the `*` it is defined and proved in [`spec/`](https://github.com/rjrodger/patrun/tree/master/spec). Some answers of 7.3.0 broke its own rules, or depended on the order in which patterns were added. Those answers are different now, which is why this is a major version. The test suite of Seneca, the main user of patrun, passes with it unchanged. ### Hardened Upgrade if patterns or subjects in your application can come from callers that you do not trust. - Keys and values that name members of `Object.prototype`, such as `__proto__`, `constructor` and `toString`, are text like any other, and are kept inside the matcher. In 7.3.0 a pattern with such a value could be written onto objects that all code shares, one matcher could answer with the data of another, and a glob on such a key made `add` throw. - A subject's pairs are its own keys that hold a string, a number or a boolean. 7.3.0 also read the keys that a subject inherits, and matched an array or an object as the text it converts to, so `{ a: ['x'] }` matched the pattern `a:x`. - Only a map is a pattern or a subject. 7.3.0 matched a string or an array by its `length`, and took `add(null, data)` as the empty pattern, which matches every subject. - Options are read from the options given, and nothing that the matcher keeps inherits from `Object.prototype`. In 7.3.0 a member that other code added to `Object.prototype` could turn an option on for every matcher, or be read as part of the tree. - With `gex` on, matching a glob takes time in proportion to the length of the value, whatever the glob. This comes with gex 4.1.3. ### Changed: which pattern `find` picks - The pattern with the most matching pairs always wins. 7.3.0 took the first key, in alphabetical order, whose value matched, so a short pattern on an early key hid a longer one on later keys. With `a:1 -> A` and `b:2,c:3 -> B`, the subject `a:1,b:2,c:3` gave `A`. It gives `B`. - With `gex` or `interval` on, the narrower of two globs, or of two intervals, comes first: the one that takes only values that the other takes too. 7.3.0 took the one that was added first. Every glob comes before every interval, an exact value before both, and the `*` after them. The README gives the tests that decide it. - With `gex` or `interval` on, every pattern that matches is considered. 7.3.0 followed only the first alternative for a key, so it could miss a glob pattern beside a pattern with the exact value, and answer `null`. - `findexact` asks for a pattern that has every pair of the subject. A key of the subject with no value, such as `undefined`, is not a pair. In 7.3.0 it made `findexact` answer `null`. - collect, `find(subject, false, true)`, gives the data of the patterns that match by a glob, an interval or the `*`, as well as by exact values. With `exact` too, it gives every pattern that matches and has every pair of the subject. 7.3.0 followed one trail through the tree. - Keys are ordered by code point. 7.3.0 ordered them as JavaScript sorts strings, which differs only for characters outside the Basic Multilingual Plane. ### Changed: the `*` - A pattern value of `*` takes any value, and asks only that the subject has a value for the key. It needs no option. In 7.3.0, without `gex`, it was the exact text `*`. To ask for that text itself, turn `gex` on and write `**`. - With `gex` on it means the same, and is no longer a glob. It is tried after the exact value, the globs and the intervals of its key. In 7.3.0 it ranked among the globs by when it was added. - `toString` shows it as `a=*`, where 7.3.0 with `gex` on showed `a~*`. `list` gives the patterns that have one, and `toJSON` shows it among the values of its key. ### Changed: what `add`, `remove` and `list` take - `add` and `remove` throw a `TypeError`, and change nothing, for a pattern that is not a map, that has the empty string as a key, or that has a value other than a string, a number or a boolean. A key whose value is `null` or `undefined` is still left out. 7.3.0 stored an array or an object as its text, and lost a pattern with an empty key when another pattern was added. Give the text you mean as the value. - `find`, `findexact` and collect match nothing for a subject that is not a map, and `list` lists nothing for a partial pattern that is not one. - `null` and `undefined` are no data. `add(pattern, null)` leaves the pattern holding nothing, as `remove` does. Every other value is data: `list` now gives the patterns whose data is `0`, `false` or `''`, and collect gives no `null`. - `remove` clears the pattern it is given. It can clear the empty pattern, and a pattern that was never added changes nothing. In 7.3.0, `remove({ a: 1, b: 2 })` could clear `a:1`, and `remove({})` did nothing. - A value of the partial pattern that `list` takes is had exactly, or is `*` for any value. 7.3.0 read it as a glob, so `list({ a: 'x*' })` gave the patterns with `a:x1` and `a:x2`. To list by a test of your own, filter what `list()` gives. - `list` gives the patterns that have a glob or an interval, which 7.3.0 left out. The values of a key come whole numbers first, in numeric order, then the other exact values as text, then the globs, then the intervals, then the `*`. The empty pattern is listed only when the partial pattern has no pairs. ### Changed: intervals - A number is the whole of a value, written in decimal: an optional sign, digits with an optional fraction, and an optional exponent. 7.3.0 read the number at the start of a value and ignored the rest, so `11abc` and `Infinity` matched `>10`, and it read hexadecimal in a pattern. - A value that gives no working interval is an exact value. In 7.3.0, with `interval` on, a pattern value such as `1.0` or `>abc` could never be found. - An interval added twice is one pattern, as is one written in two ways, and it is kept as it was last written. In 7.3.0, `=12` added twice was two patterns, and `find` gave the first. `=10&=10` is the test `=10`, where it matched nothing. ### Changed: `toJSON` - The text is the same for the same patterns and data, in whatever order they were added. A node's members are written as `k`, `v`, `g`, `s`, `d`, the values of a key in the order of `list`, and a `g` with nothing in it is left out. In 7.3.0 the order of members and of values followed the order of adding. - `JSON.stringify` calls `toJSON` itself for a matcher inside another value, with the matcher's key. A string that is not blank is no longer taken as the indent, so the matcher is written as its text with no indent, as a string. In 7.3.0, `JSON.stringify({ r: matcher })` gave text indented with the letter `r`. An indent given as a string is spaces and tabs. ### Added - The `order` option names the keys that come first in the matcher's order of keys: `patrun({ order: ['srv'] })`. Of two patterns with as many pairs, the one that has `srv` is then preferred. Every other key comes after, in alphabetical order, and a pattern with more pairs still wins. See "Choose which keys come first" in the README. - TypeScript types: `PatrunRouter`, `PatrunOptions`, `PatrunPattern` and the rest. `patrun()` makes a matcher whose `find` answers `number | null`. In 7.3.0 a matcher was `any`, so code that used one in a way the types do not allow will no longer compile. See "Use it from TypeScript" in the README. - The module is its own `default` member, so a default import finds it when compiled without `esModuleInterop` too. - [`spec/`](https://github.com/rjrodger/patrun/tree/master/spec): a definition in Lean 4 of what a matcher answers for exact values and the `*`, with proofs that `find` has one answer whatever the order of adding, and that the narrower of two patterns wins. It checks the rows in `test/data/` that both ports share, and writes more of them. ### Other - A lookup of exact values takes about as long as in 7.3.0: within a tenth, measured on Node 24. - `gex` is at 4.1.3, where 7.3.0 had 4.1.2. - Issues closed: #30, #31, #32, #34, #40, #41, #43 to #49, and #52.