Match any value of a key
Give a pattern the value '*' to match a query that has the key, whatever its value.
Use this when a pattern needs a key to be there but does not care what it holds. A pattern value of '*' takes any value, and the query must still have one:
const patrun = require('patrun')
const pm = patrun()
.add({ role: 'math', cmd: 'sum' }, 'SUM')
.add({ role: 'math', cmd: '*' }, 'ANY')
pm.find({ role: 'math', cmd: 'sum' }) // → 'SUM' an exact value comes first
pm.find({ role: 'math', cmd: 'product' }) // → 'ANY'
pm.find({ role: 'math', cmd: '' }) // → 'ANY' the empty string is a value
pm.find({ role: 'math' }) // → null no cmd, so no match
In Go #
pm := patrun.New()
pm.Add(map[string]string{"role": "math", "cmd": "sum"}, s("SUM"))
pm.Add(map[string]string{"role": "math", "cmd": "*"}, s("ANY"))
pm.Find(map[string]string{"role": "math", "cmd": "product"}) // → "ANY"
pm.Find(map[string]string{"role": "math"}) // → not found
What to watch for #
The star needs no option, and means the same with globs turned on. It counts as a matched property, so a pattern with a star and another property beats one with a single exact property. Between two patterns with the same keys, the one with an exact value at the first key where their values differ beats the one with a star there. A query value that is not a string, a number, or a boolean is no value, and a star does not match it: see What patterns and queries hold.