Require every key of the query
Find only a pattern that has every property of the query, with findExact.
Use this when a query's extra properties must not be ignored: findExact answers only with a pattern that has every key the query has a value for.
const patrun = require('patrun')
const pm = patrun()
.add({ a: 0 }, 'A')
.add({ a: 0, b: 1 }, 'AB')
pm.findExact({ a: 0, b: 1 }) // → 'AB'
pm.findExact({ a: 0, b: 9 }) // → null b does not match
pm.findExact({ a: 0, z: 9 }) // → null no pattern has both a and z
pm.find({ a: 0, z: 9 }) // → 'A' find ignores z
find(query, true) does the same. findexact, all in lower case, is an earlier spelling of findExact and still works.
In Go #
pm := patrun.New()
pm.Add(map[string]string{"a": "1"}, s("A"))
pm.Add(map[string]string{"a": "1", "b": "1"}, s("AB"))
pm.FindExact(map[string]string{"a": "1"}) // → "A"
pm.FindExact(map[string]string{"a": "1", "b": "1"}) // → "AB"
pm.FindExact(map[string]string{"a": "1", "z": "9"}) // → not found
What to watch for #
A pattern's '*' still takes any value, so findExact answers with a pattern that has a star for a key of the query. To get every pattern that has all the query's keys, not only the first, use collectExact: see Collect every match.