@snyk/dep-graph
Advanced tools
Comparing version 1.7.0 to 1.8.0
@@ -33,3 +33,7 @@ import * as graphlib from 'graphlib'; | ||
countPathsToRoot(pkg: types.Pkg): number; | ||
equals(other: types.DepGraph, { compareRoot }?: { | ||
compareRoot?: boolean; | ||
}): boolean; | ||
toJSON(): types.DepGraphData; | ||
private nodeEquals; | ||
private getGraphNode; | ||
@@ -36,0 +40,0 @@ private pathsFromNodeToRoot; |
@@ -5,2 +5,3 @@ "use strict"; | ||
var graphlib = require("graphlib"); | ||
var create_from_json_1 = require("./create-from-json"); | ||
var DepGraphImpl = /** @class */ (function () { | ||
@@ -104,2 +105,16 @@ function DepGraphImpl(graph, rootNodeId, pkgs, pkgNodes, pkgManager) { | ||
}; | ||
DepGraphImpl.prototype.equals = function (other, _a) { | ||
var _b = (_a === void 0 ? {} : _a).compareRoot, compareRoot = _b === void 0 ? true : _b; | ||
var otherDepGraph; | ||
if (other instanceof DepGraphImpl) { | ||
otherDepGraph = other; | ||
} | ||
else { | ||
// At runtime theoretically we can have multiple versions of | ||
// @snyk/dep-graph. If "other" is not an instance of the same class it is | ||
// safer to rebuild it from JSON. | ||
otherDepGraph = create_from_json_1.createFromJSON(other.toJSON()); | ||
} | ||
return this.nodeEquals(this, this.rootNodeId, otherDepGraph, otherDepGraph.rootNodeId, compareRoot); | ||
}; | ||
DepGraphImpl.prototype.toJSON = function () { | ||
@@ -138,2 +153,47 @@ var _this = this; | ||
}; | ||
DepGraphImpl.prototype.nodeEquals = function (graphA, nodeIdA, graphB, nodeIdB, compareRoot, traversedPairs) { | ||
if (traversedPairs === void 0) { traversedPairs = new Set(); } | ||
// Skip root nodes comparision if needed. | ||
if (compareRoot || (nodeIdA !== graphA.rootNodeId && nodeIdB !== graphB.rootNodeId)) { | ||
var pkgA = graphA.getNodePkg(nodeIdA); | ||
var pkgB = graphB.getNodePkg(nodeIdB); | ||
// Compare PkgInfo (name and version). | ||
if (!_.isEqual(pkgA, pkgB)) { | ||
return false; | ||
} | ||
var infoA = graphA.getNode(nodeIdA); | ||
var infoB = graphB.getNode(nodeIdB); | ||
// Compare NodeInfo (VersionProvenance and labels). | ||
if (!_.isEqual(infoA, infoB)) { | ||
return false; | ||
} | ||
} | ||
var depsA = graphA.getNodeDepsNodeIds(nodeIdA); | ||
var depsB = graphB.getNodeDepsNodeIds(nodeIdA); | ||
// Number of dependencies should be the same. | ||
if (depsA.length !== depsB.length) { | ||
return false; | ||
} | ||
// Sort dependencies by name@version string. | ||
var sortFn = function (graph) { return function (idA, idB) { | ||
var pkgA = graph.getNodePkg(idA); | ||
var pkgB = graph.getNodePkg(idB); | ||
return DepGraphImpl.getPkgId(pkgA).localeCompare(DepGraphImpl.getPkgId(pkgB)); | ||
}; }; | ||
depsA = depsA.sort(sortFn(graphA)); | ||
depsB = depsB.sort(sortFn(graphB)); | ||
// Compare Each dependency recursively. | ||
for (var i = 0; i < depsA.length; i++) { | ||
var pairKey = depsA[i] + "_" + depsB[i]; | ||
// Prevent cycles. | ||
if (traversedPairs.has(pairKey)) { | ||
continue; | ||
} | ||
traversedPairs.add(pairKey); | ||
if (!this.nodeEquals(graphA, depsA[i], graphB, depsB[i], compareRoot, traversedPairs)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
DepGraphImpl.prototype.getGraphNode = function (nodeId) { | ||
@@ -140,0 +200,0 @@ var node = this._graph.node(nodeId); |
@@ -56,2 +56,5 @@ export interface Pkg { | ||
countPathsToRoot(pkg: Pkg): number; | ||
equals(other: DepGraph, options?: { | ||
compareRoot?: boolean; | ||
}): boolean; | ||
} | ||
@@ -58,0 +61,0 @@ export interface DepGraphInternal extends DepGraph { |
@@ -50,3 +50,3 @@ { | ||
}, | ||
"version": "1.7.0" | ||
"version": "1.8.0" | ||
} |
@@ -63,2 +63,3 @@ ![Snyk logo](https://snyk.io/style/asset/logo/snyk-print.svg) | ||
toJSON(): DepGraphData; | ||
equals(other: DepGraph, options?: { compareRoot?: boolean }): boolean; | ||
} | ||
@@ -65,0 +66,0 @@ ``` |
Sorry, the diff of this file is not supported yet
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
69600
975
138