Skip to content

Commit

Permalink
Merge pull request #400 from RohitPaul0007/patch-43
Browse files Browse the repository at this point in the history
Update util.js
  • Loading branch information
rustedgrail committed Sep 11, 2023
2 parents ca2740a + 8cf812f commit e4911b8
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

"use strict";

var Graph = require("@dagrejs/graphlib").Graph;
let Graph = require("@dagrejs/graphlib").Graph;

module.exports = {
addBorderNode,
Expand Down Expand Up @@ -30,7 +30,7 @@ module.exports = {
* Adds a dummy node to the graph and return v.
*/
function addDummyNode(g, type, attrs, name) {
var v;
let v;
do {
v = uniqueId(name);
} while (g.hasNode(v));
Expand All @@ -45,11 +45,11 @@ function addDummyNode(g, type, attrs, name) {
* associated with multi-edges.
*/
function simplify(g) {
var simplified = new Graph().setGraph(g.graph());
let simplified = new Graph().setGraph(g.graph());
g.nodes().forEach(v => simplified.setNode(v, g.node(v)));
g.edges().forEach(e => {
var simpleLabel = simplified.edge(e.v, e.w) || { weight: 0, minlen: 1 };
var label = g.edge(e);
let simpleLabel = simplified.edge(e.v, e.w) || { weight: 0, minlen: 1 };
let label = g.edge(e);
simplified.setEdge(e.v, e.w, {
weight: simpleLabel.weight + label.weight,
minlen: Math.max(simpleLabel.minlen, label.minlen)
Expand All @@ -59,7 +59,7 @@ function simplify(g) {
}

function asNonCompoundGraph(g) {
var simplified = new Graph({ multigraph: g.isMultigraph() }).setGraph(g.graph());
let simplified = new Graph({ multigraph: g.isMultigraph() }).setGraph(g.graph());
g.nodes().forEach(v => {
if (!g.children(v).length) {
simplified.setNode(v, g.node(v));
Expand All @@ -72,8 +72,8 @@ function asNonCompoundGraph(g) {
}

function successorWeights(g) {
var weightMap = g.nodes().map(v => {
var sucs = {};
let weightMap = g.nodes().map(v => {
let sucs = {};
g.outEdges(v).forEach(e => {
sucs[e.w] = (sucs[e.w] || 0) + g.edge(e).weight;
});
Expand All @@ -83,8 +83,8 @@ function successorWeights(g) {
}

function predecessorWeights(g) {
var weightMap = g.nodes().map(v => {
var preds = {};
let weightMap = g.nodes().map(v => {
let preds = {};
g.inEdges(v).forEach(e => {
preds[e.v] = (preds[e.v] || 0) + g.edge(e).weight;
});
Expand All @@ -98,21 +98,21 @@ function predecessorWeights(g) {
* ({x, y, width, height}) if it were pointing at the rectangle's center.
*/
function intersectRect(rect, point) {
var x = rect.x;
var y = rect.y;
let x = rect.x;
let y = rect.y;

// Rectangle intersection algorithm from:
// http://math.stackexchange.com/questions/108113/find-edge-between-two-boxes
var dx = point.x - x;
var dy = point.y - y;
var w = rect.width / 2;
var h = rect.height / 2;
let dx = point.x - x;
let dy = point.y - y;
let w = rect.width / 2;
let h = rect.height / 2;

if (!dx && !dy) {
throw new Error("Not possible to find intersection inside of the rectangle");
}

var sx, sy;
let sx, sy;
if (Math.abs(dy) * w > Math.abs(dx) * h) {
// Intersection is top or bottom of rect.
if (dy < 0) {
Expand All @@ -137,10 +137,10 @@ function intersectRect(rect, point) {
* function will produce a matrix with the ids of each node.
*/
function buildLayerMatrix(g) {
var layering = range(maxRank(g) + 1).map(() => []);
let layering = range(maxRank(g) + 1).map(() => []);
g.nodes().forEach(v => {
var node = g.node(v);
var rank = node.rank;
let node = g.node(v);
let rank = node.rank;
if (rank !== undefined) {
layering[rank][node.order] = v;
}
Expand All @@ -153,16 +153,16 @@ function buildLayerMatrix(g) {
* rank(v) >= 0 and at least one node w has rank(w) = 0.
*/
function normalizeRanks(g) {
var min = Math.min(...g.nodes().map(v => {
var rank = g.node(v).rank;
let min = Math.min(...g.nodes().map(v => {
let rank = g.node(v).rank;
if (rank === undefined) {
return Number.MAX_VALUE;
}

return rank;
}));
g.nodes().forEach(v => {
var node = g.node(v);
let node = g.node(v);
if (node.hasOwnProperty("rank")) {
node.rank -= min;
}
Expand Down

0 comments on commit e4911b8

Please sign in to comment.