graphs-all
Advanced tools
Comparing version 0.1.0 to 1.0.0
{ | ||
"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" | ||
} | ||
} | ||
} |
@@ -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 @@ }; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11706
7
227
0
8
1