Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update util.js #400

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading