npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graph-stringify

v1.1.0

Published

Compact string representation of an object graph, one node per line

Downloads

26

Readme

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:

{x: 1, y: ["x", false], z: null}
   1 | Object x=1 y=['x',false] z=null
new Program([
  new Assignment("x", 3),
  new Break()
])
   1 | Program statements=[#2,#3]
   2 | Assignment target='x' source=3
   3 | Break
new Program([
  new Assignment(
    "y",
    new BinaryExpression(
      "*",
      new Call("hypot", [3, 5]), "x"
     )
  ),
])
1 | Program statements=[#2]
2 | Assignment target='y' source=#3
3 | BinaryExpression op='\*' left=#4 right='x'
4 | Call callee='hypot' args=[3,5]
let [a, b, c, d] = [
  new Node("A"),
  new Node("B"),
  new Node("C"),
  new Node("D")
];
a.successors = [b, c, d];
b.successors = [c];
c.successors = [b, c];
d.successors = [a, d];
   1 | Node name='A' successors=[#2,#3,#4]
   2 | Node name='B' successors=[#3]
   3 | Node name='C' successors=[#2,#3]
   4 | Node name='D' successors=[#1,#4]

With the keyProperty argument, e.g. stringify(obj, "kind"):

{
  kind: "Program",
  statements: [
    { kind: "Assignment", target: "x", source: 3 },
    { kind: "Break" },
  ],
}
   1 | Program statements=[#2,#3]
   2 | Assignment target='x' source=3
   3 | Break
{
  kind: "Program",
  statements: [
    {
      kind: "Assignment",
      target: "y",
      source: {
        kind: "BinaryExpression",
        op: "*",
        left: { kind: "Call", callee: "hypot", args: [3, 5] },
        right: "x",
      },
    },
  ],
}
1 | Program statements=[#2]
2 | Assignment target='y' source=#3
3 | BinaryExpression op='\*' left=#4 right='x'
4 | Call callee='hypot' args=[3,5]

Additional examples are found in the test folder.