UNPKG

783 BJavaScriptView Raw
1/**
2 * Compute adjacent nodes
3 *
4 * @param {Object} edges
5 * @param {String} nodeId
6 * @returns {Array} adjacentNodeIds
7 */
8
9const getAdjacentNodeIds = (edges, nodeId) => {
10 let adjacentNodeIds = []
11
12 const givenNodeId = (id) => {
13 return id !== nodeId
14 }
15
16 const foundNodeIds = (id) => {
17 return adjacentNodeIds.indexOf(id) === -1
18 }
19
20 for (let edgeId in edges) {
21 let edge = edges[edgeId]
22
23 // Nothing to do if edge does not contain nodeId.
24 if (edge.indexOf(nodeId) === -1) continue
25
26 // Take all nodeIds except given nodeId, avoid repetitions.
27 let nodeIds = edge.filter(givenNodeId)
28 .filter(foundNodeIds)
29
30 adjacentNodeIds = adjacentNodeIds.concat(nodeIds)
31 }
32
33 return adjacentNodeIds
34}
35
36module.exports = getAdjacentNodeIds