Graphology.js 1.97 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
export function _forEachNode(g, fn) {
  return g.forEachNode(function(_name, attrs) {
    // NOTE: fn is an effectful function, it wraps `do` in a separate function
    return fn(attrs)();
  })
}

28
export function _addEdge(g, source, target, e) {
29 30 31 32 33
  //return g.addEdge(source, target, e);

  // NOTE: Our edge.id is the main key. We don't need sigma
  // auto-generated keys for edges
  return g.addEdgeWithKey(e.id, source, target, e);
34 35 36 37 38
}

// 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) {
39 40
  return g.mapNodes(function(_name, attrs) {
    return fn(attrs);
41 42 43
  });
}

44
export function _filterNodes(g, fn) {
45 46
  return g.filterNodes(function(_name, attrs) {
    return fn(attrs);
47 48 49 50
  })
}


51
export function _forEachEdge(g, fn) {
52 53
  return g.forEachEdge(function(_name, attrs, _source, _target, _sourceAttributes, _targetAttributes, _undirected) {
    return fn(attrs);
54 55 56
  });
}

57
export function _updateEachEdgeAttributes(g, fn) {
58 59
  return g.updateEachEdgeAttributes(function(_name, attrs, _source, _target) {
    return fn(attrs);
60 61 62
  });
}

63 64
// Almost the same as graphology.mapNodes but with a change that only
// 1 argument is passed: the whole node structure
65
// https://graphology.github.io/iteration.html#mapedges
66
export function _mapEdges(g, fn) {
67 68
  return g.mapEdges(function(_name, attrs, _source, _target, _sourceAttributes, _targetAttributes, _undirected) {
    return fn(attrs);
69 70
  });
}
71 72

export function _filterEdges(g, fn) {
73 74
  return g.filterEdges(function(_name, attrs) {
    return fn(attrs);
75 76
  })
}
77 78 79 80

export function _copy(g) {
  return g.copy();
}