Skip to content

Commit

Permalink
remove some lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
tbo47 committed Sep 12, 2024
1 parent 185de49 commit 591f3fe
Show file tree
Hide file tree
Showing 37 changed files with 639 additions and 721 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

# dagre-d3-es

The [dagre-d3](https://github.com/dagrejs) library is not maintained anymore.
[dagre-d3-es](https://www.npmjs.com/package/dagre-d3-es) is a fork of [dagre-d3](https://github.com/dagrejs) using the more modern ES6 javascript syntax.

[dagre-d3-es](https://www.npmjs.com/package/dagre-d3-es) is a fork using the more modern ES6 javascript syntax.
It uses [ES](https://262.ecma-international.org/6.0/) modules, thus the name [dagre-d3-es](https://www.npmjs.com/package/dagre-d3-es).

[dagre-d3-es](https://www.npmjs.com/package/dagre-d3-es) follows [d3](https://www.npmjs.com/package/d3) versions. Ex: dagre-d3-es version 7 depends on [d3](https://www.npmjs.com/package/d3) version 7.
Expand Down
16 changes: 8 additions & 8 deletions src/dagre/acyclic.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as _ from 'lodash-es';
import { greedyFAS } from './greedy-fas.js';
import { uniqueId } from './util.js';

export { run, undo };

function run(g) {
var fas = g.graph().acyclicer === 'greedy' ? greedyFAS(g, weightFn(g)) : dfsFAS(g);
_.forEach(fas, function (e) {
fas.forEach(e => {
var label = g.edge(e);
g.removeEdge(e);
label.forwardName = e.name;
label.reversed = true;
g.setEdge(e.w, e.v, label, _.uniqueId('rev'));
g.setEdge(e.w, e.v, label, uniqueId('rev'));
});

function weightFn(g) {
Expand All @@ -26,13 +26,13 @@ function dfsFAS(g) {
var visited = {};

function dfs(v) {
if (_.has(visited, v)) {
if (visited.hasOwnProperty(v)) {

Check failure on line 29 in src/dagre/acyclic.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Do not access Object.prototype method 'hasOwnProperty' from target object

Check failure on line 29 in src/dagre/acyclic.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Do not access Object.prototype method 'hasOwnProperty' from target object
return;
}
visited[v] = true;
stack[v] = true;
_.forEach(g.outEdges(v), function (e) {
if (_.has(stack, e.w)) {
g.outEdges(v).forEach(e => {
if (stack.hasOwnProperty(e.w)) {

Check failure on line 35 in src/dagre/acyclic.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Do not access Object.prototype method 'hasOwnProperty' from target object

Check failure on line 35 in src/dagre/acyclic.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Do not access Object.prototype method 'hasOwnProperty' from target object
fas.push(e);
} else {
dfs(e.w);
Expand All @@ -41,12 +41,12 @@ function dfsFAS(g) {
delete stack[v];
}

_.forEach(g.nodes(), dfs);
g.nodes().forEach(dfs);
return fas;
}

function undo(g) {
_.forEach(g.edges(), function (e) {
g.edges().forEach(e => {
var label = g.edge(e);
if (label.reversed) {
g.removeEdge(e);
Expand Down
7 changes: 3 additions & 4 deletions src/dagre/add-border-segments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as _ from 'lodash-es';
import * as util from './util.js';

export { addBorderSegments };
Expand All @@ -8,10 +7,10 @@ function addBorderSegments(g) {
var children = g.children(v);
var node = g.node(v);
if (children.length) {
_.forEach(children, dfs);
children.forEach(dfs);
}

if (_.has(node, 'minRank')) {
if (node.hasOwnProperty("minRank")) {

Check failure on line 13 in src/dagre/add-border-segments.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Do not access Object.prototype method 'hasOwnProperty' from target object

Check failure on line 13 in src/dagre/add-border-segments.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Do not access Object.prototype method 'hasOwnProperty' from target object
node.borderLeft = [];
node.borderRight = [];
for (var rank = node.minRank, maxRank = node.maxRank + 1; rank < maxRank; ++rank) {
Expand All @@ -21,7 +20,7 @@ function addBorderSegments(g) {
}
}

_.forEach(g.children(), dfs);
g.children().forEach(dfs);
}

function addBorderNode(g, prop, prefix, sg, sgNode, rank) {
Expand Down
30 changes: 10 additions & 20 deletions src/dagre/coordinate-system.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as _ from 'lodash-es';

export { adjust, undo };

function adjust(g) {
Expand All @@ -22,12 +20,8 @@ function undo(g) {
}

function swapWidthHeight(g) {
_.forEach(g.nodes(), function (v) {
swapWidthHeightOne(g.node(v));
});
_.forEach(g.edges(), function (e) {
swapWidthHeightOne(g.edge(e));
});
g.nodes().forEach(v => swapWidthHeightOne(g.node(v)));
g.edges().forEach(e => swapWidthHeightOne(g.edge(e)));
}

function swapWidthHeightOne(attrs) {
Expand All @@ -37,14 +31,12 @@ function swapWidthHeightOne(attrs) {
}

function reverseY(g) {
_.forEach(g.nodes(), function (v) {
reverseYOne(g.node(v));
});
g.nodes().forEach(v => reverseYOne(g.node(v)));

_.forEach(g.edges(), function (e) {
g.edges().forEach(e => {
var edge = g.edge(e);
_.forEach(edge.points, reverseYOne);
if (_.has(edge, 'y')) {
edge.points.forEach(reverseYOne);
if (edge.hasOwnProperty("y")) {

Check failure on line 39 in src/dagre/coordinate-system.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Do not access Object.prototype method 'hasOwnProperty' from target object

Check failure on line 39 in src/dagre/coordinate-system.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Do not access Object.prototype method 'hasOwnProperty' from target object
reverseYOne(edge);
}
});
Expand All @@ -55,14 +47,12 @@ function reverseYOne(attrs) {
}

function swapXY(g) {
_.forEach(g.nodes(), function (v) {
swapXYOne(g.node(v));
});
g.nodes().forEach(v => swapXYOne(g.node(v)));

_.forEach(g.edges(), function (e) {
g.edges().forEach(e => {
var edge = g.edge(e);
_.forEach(edge.points, swapXYOne);
if (_.has(edge, 'x')) {
edge.points.forEach(swapXYOne);
if (edge.hasOwnProperty("x")) {

Check failure on line 55 in src/dagre/coordinate-system.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Do not access Object.prototype method 'hasOwnProperty' from target object

Check failure on line 55 in src/dagre/coordinate-system.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Do not access Object.prototype method 'hasOwnProperty' from target object
swapXYOne(edge);
}
});
Expand Down
11 changes: 4 additions & 7 deletions src/dagre/debug.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as _ from 'lodash-es';
import { Graph } from '../graphlib/index.js';
import * as util from './util.js';

Expand All @@ -10,19 +9,17 @@ function debugOrdering(g) {

var h = new Graph({ compound: true, multigraph: true }).setGraph({});

_.forEach(g.nodes(), function (v) {
g.nodes().forEach(v => {
h.setNode(v, { label: v });
h.setParent(v, 'layer' + g.node(v).rank);
});

_.forEach(g.edges(), function (e) {
h.setEdge(e.v, e.w, {}, e.name);
});
g.edges().forEach(e => h.setEdge(e.v, e.w, {}, e.name));

_.forEach(layerMatrix, function (layer, i) {
layerMatrix.forEach((layer, i) => {
var layerV = 'layer' + i;
h.setNode(layerV, { rank: 'same' });
_.reduce(layer, function (u, v) {
layer.reduce((u, v) => {
h.setEdge(u, v, { style: 'invis' });
return v;
});
Expand Down
26 changes: 9 additions & 17 deletions src/dagre/greedy-fas.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as _ from 'lodash-es';
import { range } from './util.js';
import { Graph } from '../graphlib/index.js';
import { List } from './data/list.js';

Expand All @@ -11,7 +11,7 @@ import { List } from './data/list.js';
*/
export { greedyFAS };

var DEFAULT_WEIGHT_FN = _.constant(1);
var DEFAULT_WEIGHT_FN = () => 1;

function greedyFAS(g, weightFn) {
if (g.nodeCount() <= 1) {
Expand All @@ -21,11 +21,7 @@ function greedyFAS(g, weightFn) {
var results = doGreedyFAS(state.graph, state.buckets, state.zeroIdx);

// Expand multi-edges
return _.flatten(
_.map(results, function (e) {
return g.outEdges(e.v, e.w);
})
);
return results.flatMap(e => g.outEdges(e.v, e.w));
}

function doGreedyFAS(g, buckets, zeroIdx) {
Expand Down Expand Up @@ -58,7 +54,7 @@ function doGreedyFAS(g, buckets, zeroIdx) {
function removeNode(g, buckets, zeroIdx, entry, collectPredecessors) {
var results = collectPredecessors ? [] : undefined;

_.forEach(g.inEdges(entry.v), function (edge) {
g.inEdges(entry.v).forEach(edge => {
var weight = g.edge(edge);
var uEntry = g.node(edge.v);

Expand All @@ -70,7 +66,7 @@ function removeNode(g, buckets, zeroIdx, entry, collectPredecessors) {
assignBucket(buckets, zeroIdx, uEntry);
});

_.forEach(g.outEdges(entry.v), function (edge) {
g.outEdges(entry.v).forEach(edge => {
var weight = g.edge(edge);
var w = edge.w;
var wEntry = g.node(w);
Expand All @@ -88,13 +84,11 @@ function buildState(g, weightFn) {
var maxIn = 0;
var maxOut = 0;

_.forEach(g.nodes(), function (v) {
fasGraph.setNode(v, { v: v, in: 0, out: 0 });
});
g.nodes().forEach(v => fasGraph.setNode(v, { v: v, in: 0, out: 0 }));

// Aggregate weights on nodes, but also sum the weights across multi-edges
// into a single edge for the fasGraph.
_.forEach(g.edges(), function (e) {
g.edges().forEach(e => {
var prevWeight = fasGraph.edge(e.v, e.w) || 0;
var weight = weightFn(e);
var edgeWeight = prevWeight + weight;
Expand All @@ -103,12 +97,10 @@ function buildState(g, weightFn) {
maxIn = Math.max(maxIn, (fasGraph.node(e.w)['in'] += weight));
});

var buckets = _.range(maxOut + maxIn + 3).map(function () {
return new List();
});
var buckets = range(maxOut + maxIn + 3).map(() => new List());
var zeroIdx = maxIn + 1;

_.forEach(fasGraph.nodes(), function (v) {
fasGraph.nodes().forEach(v => {
assignBucket(buckets, zeroIdx, fasGraph.node(v));
});

Expand Down
Loading

0 comments on commit 591f3fe

Please sign in to comment.