Graphology.js 1.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
'use strict';

import Graph from 'graphology';

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

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

13 14 15 16
export function _updateNode(g, name, updater) {
  return g.updateNode(name, updater);
}

17 18 19 20
export function _mergeNodeAttributes(g, name, attrs) {
  return g.mergeNodeAttributes(name, attrs);
}

21 22 23 24 25 26 27 28 29 30 31 32
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});
  });
}

33 34 35 36 37 38 39
export function _filterNodes(g, fn) {
  return g.filterNodes(function(name, attrs) {
    return fn({id: name, ...attrs});
  })
}


40 41 42 43 44 45 46 47 48
export function _forEachEdge(g, fn) {
  return g.forEachEdge(function(name, attrs, source, target, sourceAttributes, targetAttributes, undirected) {
    return fn({id: name,
               source,
               target,
               ...attrs});
  });
}

49 50 51 52 53 54 55 56 57
export function _updateEachEdgeAttributes(g, fn) {
  return g.updateEachEdgeAttributes(function(name, attrs, source, target) {
    return fn({id: name,
               source,
               target,
               ...attrs});
  });
}

58 59
// Almost the same as graphology.mapNodes but with a change that only
// 1 argument is passed: the whole node structure
60
// https://graphology.github.io/iteration.html#mapedges
61
export function _mapEdges(g, fn) {
62 63 64 65 66
  return g.mapEdges(function(name, attrs, source, target, sourceAttributes, targetAttributes, undirected) {
    return fn({id: name,
               source,
               target,
               ...attrs});
67 68
  });
}
69 70 71 72 73 74

export function _filterEdges(g, fn) {
  return g.filterEdges(function(name, attrs) {
    return fn({id: name, ...attrs});
  })
}