Graphology.js 1.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
'use strict';

import Graph from 'graphology';

export function _newGraph() {
  return new Graph();
}

export function _addNode(g, name, n) {
  return g.addNode(name, n);
}

export function _addEdge(g, source, target, e) {
  return g.addEdge(source, target, e);
}

// Almost the same as graphology.mapNodes but with a change that only
// 1 argument is passed: the whole node structure
export function _mapNodes(g, fn) {
  return g.mapNodes(function(name, attrs) {
    return fn({id: name, ...attrs});
  });
}

25 26 27 28 29 30 31 32 33
export function _forEachEdge(g, fn) {
  return g.forEachEdge(function(name, attrs, source, target, sourceAttributes, targetAttributes, undirected) {
    return fn({id: name,
               source,
               target,
               ...attrs});
  });
}

34 35
// Almost the same as graphology.mapNodes but with a change that only
// 1 argument is passed: the whole node structure
36
// https://graphology.github.io/iteration.html#mapedges
37
export function _mapEdges(g, fn) {
38 39 40 41 42
  return g.mapEdges(function(name, attrs, source, target, sourceAttributes, targetAttributes, undirected) {
    return fn({id: name,
               source,
               target,
               ...attrs});
43 44
  });
}