Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

resolved-graph

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

resolved-graph

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

  • 1.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Resolved Graph

Check out the ResolvedGraph class for pretty self explanatory methods. Will update with some more js defs soon. Npm package is here: https://www.npmjs.com/package/resolved-graph

Tests 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

You can define the type of the 'data' prop on both nodes and links. This will preserve intellisense event 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' },
    },
  ],
}

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]],
  },
]

And yes, I am working on a (simple) query language for this ;)

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 makes it 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!

Keywords

FAQs

Package last updated on 11 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc