List the patterns a router holds

List every pattern and its data with list, or only those that have the properties of a partial pattern.

Use this to see what a router holds, all of it or a part. list with no argument gives every pattern, each entry its pattern and its data:

const patrun = require('patrun')

const pm = patrun()
  .add({ a: 1, b: 1 }, 'B1')
  .add({ a: 1, b: 2 }, 'B2')
  .add({ c: 3 }, 'C')

pm.list().length // → 3
pm.list({ a: 1 }) // → [{ pattern: { a: '1', b: '1' }, data: 'B1' }, { pattern: { a: '1', b: '2' }, data: 'B2' }]

Given a partial pattern, list gives the patterns that have every property of it. The values of a pattern come back as strings. A value of '*' in the partial pattern asks only that a pattern has the key:

pm.list({ b: '*' }).map((entry) => entry.data) // → ['B1', 'B2']
pm.list({ a: 1, c: 1 }) // → []

Pass true as the second argument to list only the patterns that have no other keys than the partial pattern's:

pm.list({ c: 3 }, true).map((entry) => entry.data) // → ['C']
pm.list({ a: 1 }, true) // → []

In Go #

List takes a partial pattern, or nil for every pattern, and gives a slice of ListEntry:

pm := patrun.New()
pm.Add(map[string]string{"a": "1", "b": "1"}, s("B1"))
pm.Add(map[string]string{"a": "1", "b": "2"}, s("B2"))

data := []string{}
for _, e := range pm.List(map[string]string{"a": "1"}) {
	data = append(data, e.Data)
}
data // → [B1 B2]

What to watch for #

A key the partial pattern leaves out is not a wildcard: name each key you want to filter on. A value is matched exactly, so with gex on, { a: 'x*' } lists the patterns that have the glob x* itself, not those with values the glob would take.

The order of the list does not depend on the order of adding. It is stated in How a match is chosen.

This page describes patrun 9.0.0 for JavaScript and 0.4.0 for Go. Edit this page · This page as Markdown