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

graphs-all

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphs-all - npm Package Compare versions

Comparing version 0.1.0 to 1.0.0

.travis.yml

31

package.json
{
"name": "graphs-all",
"version": "0.1.0",
"version": "1.0.0",
"description": "Graph Data Structure Library",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"commit": "git-cz",
"test": "mocha src/index.test.js -w",
"test:single": "mocha src/index.test.js",
"coverage": "istanbul cover -x *.test.js _mocha -- -R spec src/index.test.js",
"report-coverage": "cat ./coverage/lcov.info | codecov",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Bryukh/graphs.git"
"url": "https://github.com/Bryukh/graphs.git"
},

@@ -22,3 +27,21 @@ "keywords": [

},
"homepage": "https://github.com/Bryukh/graphs#readme"
"homepage": "https://github.com/Bryukh/graphs#readme",
"devDependencies": {
"chai": "^3.3.0",
"codecov.io": "^0.1.6",
"commitizen": "^1.0.4",
"cz-conventional-changelog": "^1.1.2",
"ghooks": "^0.3.2",
"istanbul": "^0.3.21",
"mocha": "^2.3.3",
"semantic-release": "^4.3.5"
},
"czConfig": {
"path": "node_modules/cz-conventional-changelog"
},
"config": {
"ghooks": {
"pre-commit": "npm run coverage"
}
}
}

83

src/graph.js

@@ -1,6 +0,1 @@

function Node(key, value) {
this.key = key;
this.value = value;
}
function Graph(isDirected, isWeighted) {

@@ -10,12 +5,17 @@ isDirected = isDirected || false;

var counterId = 0,
keyToIdTable = {};
this.nodes = {};
this.links = {};
keyToIdTable = {},
links = {};
this.addNode = function(key, value) {
this.type = function () {
return {
weighted: isWeighted,
directed: isDirected
}
};
this.addNode = function (key) {
if (keyToIdTable[key] === undefined) {
var id = counterId++;
keyToIdTable[key] = id;
this.nodes[id] = new Node(key, value);
this.links[id] = {};
links[id] = {};
return true;

@@ -26,29 +26,60 @@ }

this.addLink = function(key1, key2, weight) {
this.addLink = function (key1, key2, weight) {
if (key1 === key2) {
return false;
}
if (this.hasLink(key1, key2)) {
return false;
}
if (!isWeighted || weight === undefined) {
weight = 1;
}
if (isNaN(weight)) {
throw "Weight must be a number";
if (isNaN(weight) || typeof(weight) !== "number" || !isFinite(weight)) {
throw new TypeError("Weight must be a finite number");
}
if (!this.hasNode(key1)) {
this.addNode(key1);
}
if (!this.hasNode(key2)) {
this.addNode(key2);
}
var id1 = keyToIdTable[key1],
id2 = keyToIdTable[key2];
if (id1 === undefined) {
throw "'" + key1 + "' is not found."
}
if (id2 === undefined) {
throw "'" + key2 + "' is not found."
}
this.links[id1][id2] = weight;
links[id1][id2] = weight;
if (!isDirected) {
this.links[id2][id1] = weight;
links[id2][id1] = weight;
}
return true;
};
this.hasNode = function(key) {
this.hasNode = function (key) {
return keyToIdTable[key] !== undefined;
};
this.hasLink = function(key1, key2) {
return this.hasNode(key1) && this.hasNode(key2) && this.links[key1][key2] !== undefined;
this.hasLink = function (key1, key2) {
return this.hasNode(key1) && this.hasNode(key2) &&
links[keyToIdTable[key1]][keyToIdTable[key2]] !== undefined;
};
this.removeLink = function(key1, key2) {
if (!this.hasLink(key1, key2)) {
return false;
}
var id1 = keyToIdTable[key1],
id2 = keyToIdTable[key2];
delete links[id1][id2];
if (!isDirected) {
delete links[id2][id1];
}
return true;
};
this.linkWeight = function (key1, key2) {
if (!this.hasLink(key1, key2)) {
return undefined;
}
else {
return links[keyToIdTable[key1]][keyToIdTable[key2]];
}
}

@@ -60,3 +91,3 @@

function createGraph(isDirected, isWeighted) {
return function(){
return function () {
return new Graph(isDirected, isWeighted);

@@ -63,0 +94,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