graph-route-finder
Advanced tools
Comparing version 1.0.0 to 1.1.0
23
graph.js
@@ -115,2 +115,25 @@ 'use strict'; | ||
} | ||
/** | ||
* Calculate cost for given route | ||
* @param {array} nodesArr | ||
*/ | ||
calculate(nodesArr) { | ||
nodesArr = Array.isArray(nodesArr) ? nodesArr : [nodesArr]; | ||
// reform nodesArrpaths -> [[A,B],[B,C],...] | ||
const paths = nodesArr.reduce((acc, cur, index) => { | ||
if (nodesArr[index + 1] == null) return acc; | ||
return acc.concat([[cur, nodesArr[index + 1]]]); | ||
}, []); | ||
// calculate cost | ||
return paths.reduce((acc, path) => { | ||
const nodes = this.from(path[0]); | ||
if (nodes && nodes[path[1]]) return acc + parseInt(nodes[path[1]], 10); | ||
throw new Error('No Such Route'); | ||
}, | ||
0 | ||
); | ||
} | ||
}; |
v1.1.0 / 2020-10-07 | ||
================== | ||
* feat: add function to calculate cost | ||
v1.0.0 / 2020-10-07 | ||
@@ -3,0 +8,0 @@ ================== |
{ | ||
"name": "graph-route-finder", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Find all/least-cost routes in weighted directed graph with given limitations ", | ||
@@ -5,0 +5,0 @@ "main": "graph.js", |
@@ -115,2 +115,17 @@ # graph-route-finder | ||
### Calculate cost for given route(a array of nodes) | ||
#### graph.calculate([nodeName1, nodeName2, ...]) | ||
This function will go through each node to the next from given node array to calculate cost for the whole route, if a path is not found in the route, `No Such Route` error will be returned. | ||
```js | ||
graph.calculate(['E', 'A', 'C', 'F']) | ||
// 8 | ||
graph.calculate(['A', 'D', 'F']); | ||
// Error { message: 'No Such Route } | ||
``` | ||
### Find routes for given start node and end node | ||
@@ -117,0 +132,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12465
125
196