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

resolved-graph

v1.3.4

Published

Generates and updates a graph of nodes and links with resolved relationships for ease of traversal and extraction

Downloads

7

Readme

Resolved Graph

Also check out Resolved Graph Query, a fledgling query language for this package available here

Build NPM Version NPM Downloads

Introduction

This is a tiny package that takes a more or less standard graph and make its entities directly accesible (and iterable with standard methods) through memory, like so:

import { Graph } from 'resolved-graph'

const graph: Graph = {
  nodes: [
    {
      id: 'Tommy ',
    },
    {
      id: 'Viktoria ',
    },
  ],
  links: [
    {
      id: 'is in love with ',
      from: 'Tommy ',
      to: 'Viktoria ',
    },
    {
      id: 'is head over heels for ',
      from: 'Viktoria ',
      to: 'Tommy ',
    },
  ],
}

Now, lets resolve it:

import { Graph, ResolvedGraph } from 'resolved-graph'
//...
const resolvedGraph = new ResolvedGraph(graph)

...And try it out for size:

//...
for (const node of resolvedGraph.nodes) {
  for (const link of node.from) {
    const nextNode = link.to
    console.log(node.id + link.id + nextNode.id)
  }
}

Expected output:

Tommy is in love with Viktoria
Viktoria is head over heels for Tommy

Defining node & link types

You can define the type of the 'data' prop on both nodes and links. This will preserve intellisense even after resolving the graph, making it easy to traverse your data in code. However - as with any self respecting JS application - there is no runtime typechecking ;)

//...

interface NodeData {
  name: string
}

interface LinkData {
  label: string
}

const graph: Graph<NodeData, LinkData> = {
  nodes: [
    {
      id: '1',
      data: { name: 'Tommy' },
    },
    {
      id: '2',
      data: { name: 'Viktoria' },
    },
  ],
  links: [
    {
      id: '3',
      from: '1',
      to: '2',
      data: { label: 'is in love with' },
    },
    {
      id: '4',
      from: '2',
      to: '1',
      data: { label: 'is head over heels for' },
    },
  ],
}

Searching for nodes & links

You can use the .findNode, .findNodes, .findLink or .findLinks functions to easily query for certain entities. For example, lets find all nodes with name Tommy in the data prop and that has any links to a node with id '2':

//...
console.log(resolvedGraph.findNodes({ data: { name: 'Tommy' }, from: [{ to: { id: '2' } }] }))

Expected output:

[
  {
    id: '1',
    data: { name: 'Tommy' },
    to: [[Object]],
    from: [[Object]],
  },
]

As mentioned above, you can use Resolved Graph Query for more advanced use cases such as recursive queries

Disclaimer

As long as you use the standard methods found on the ResolvedGraph class, it should keep up with all resolutions for you. You can chuck in whatever properties you like on the nodes & links, as long as you stay away from 'id', 'to' and 'from'. The dissolve() method breaks the circular references and returns a simpler Graph object safe for JSON. No checks are made on other properties so that's up to you.

This project is currently only for my amusement. With that said, I'm glad you found it and welcome any requests!