Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graph-route-finder

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graph-route-finder - npm Package Compare versions

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 @@ ==================

2

package.json
{
"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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc