ngraph.tojson
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

ngraph.tojson

Library to store graph into simple json format

build status

usage

var graph = require('ngraph.graph')();
graph.addLink('hello', 'world');

var toJSON = require('ngraph.tojson');
console.log(toJSON(graph));

// Produces output:
// {"nodes":[{"id":"hello"},{"id":"world"}],"links":[{"fromId":"hello","toId":"world"}]}

You can also provide a custom transform function to transform nodes/edges before they are pushed to the output:

var g = require('ngraph.graph')();
g.addNode(1, { name: 'John' });
g.addNode(2, { name: 'Jim' });

graph.addLink(1, 2, 'Father');

var json = toJSON(g,
  function nodeTransform(node) {
    return [node.id, node.data];
  },
  function linkTransform(link) {
    return [link.fromId, link.toId, link.data];
  });

console.log(json);
// This will produce array of arrays for nodes:
// {
//   "nodes": [
//             [1, {"name": "John"}],
//             [2, {"name": "Jim"}]
//            ],
//   "links": [[1, 2, "Father"]]
// }

install

With npm do:

npm install ngraph.tojson

license

MIT

Package Sidebar

Install

npm i ngraph.tojson

Weekly Downloads

1,316

Version

1.2.0

License

MIT

Unpacked Size

7.14 kB

Total Files

7

Last publish

Collaborators

  • anvaka