UNPKG

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