forked from igaln/graphcommons-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.js
50 lines (36 loc) · 882 Bytes
/
graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Constructor
// Build a graph
var Graph = function(_graph) {
this.edges = _graph.edges;
this.nodes = _graph.nodes;
this.properties = _graph;
}
// Get node from graph
Graph.prototype.get_node = function(_id) {
if(!this.nodes) return;
var foundnode = {};
this.nodes.forEach(function(node) {
if(node.id == _id)
foundnode = node;
});
return foundnode;
}
// Get edges for a node
Graph.prototype.edges_for = function(_node, _direction) {
if(!this.edges) return;
var found_edges = [];
this.edges.forEach(function(edge) {
if(edge[_direction] == _node.id)
found_edges.push(edge);
});
return found_edges;
}
// Get edges from a node
Graph.prototype.edges_from = function(_node) {
return this.edges_for(_node, 'from')
}
// Get edges to a node
Graph.prototype.edges_to = function(_node) {
return this.edges_for(_node, 'to')
}
module.exports = Graph;