graph-stringify
A simple way to produce a compact string representation of an object graph, one node per line. Nodes are tagged with a unique id to properly handle nodes that appear multiple times, as well as cycles.
Installation
Install via:
npm install graph-stringify
The sole function is available as the default:
import stringify from "graph-stringify";
Usage
stringify(obj, keyProperty)
obj
is the object to stringify. keyProperty
is the property of the object that serves as its “type” for pretty printing. If omitted, the object’s constructor’s name is used.
Basic primitives such as undefined
, null
, numbers, bigints, booleans, strings, and symbols are ignored when passed directly to this function.
If an object is received, its object graph is written with one node per line. Each line contains the node’s type followed by a list of its properties in name=value
format. Non-object values are output in place with util.inspect
, function values are output as <Function>
, and object values (including arrays) are written in subsequent lines and referred to by a reference number.
Examples
When invoked without the keyProperty
argument:
Source string | Stringified |
---|---|
|
|
|
|
|
|
|
|
With the keyProperty
argument, e.g. stringify(obj, "kind")
:
Source string | Stringified |
---|---|
|
|
|
|
Additional examples are found in the test folder.