Inspect a router

Print a router's patterns and data with toString, or its tree as JSON with toJSON, to debug it or compare two routers.

Use this to see what a router holds while debugging, or to check that two routers hold the same patterns. toString prints each pattern and its data, one to a line:

const patrun = require('patrun')

const pm = patrun().add({ a: 1 }, 'A').add({ a: 1, b: 1 }, 'AB')

pm.toString() // → 'a=1 -> <A>\na=1, b=1 -> <AB>'
pm.toString((data) => data.toLowerCase()) // → 'a=1 -> a\na=1, b=1 -> ab'

A function given to toString formats the data. Pass true as a second argument to print the tree indented instead.

toJSON gives the router's tree as JSON text:

pm.toJSON() // → '{"k":"a","v":{"1":{"k":"b","v":{"1":{"d":"AB"}},"d":"A"}}}'

The tree's members are stated in the JavaScript reference.

In Go #

String and ToJSON, the second with the same members as in JavaScript:

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

pm.String() == "a=1 -> <A>\na=1, b=1 -> <AB>" // → true

What to watch for #

The text depends only on the patterns and their data, not on the order of adding, so two routers that hold the same are the same text. An interval written in more than one way shows as it was last written.

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