Modernize ES for-loops and remove problematic for-in loops
This slightly modifies the behaviour of errors (see init.js)
Change-Id: I1aab691d5b7e8167b6213378bdd9139c133202cd
diff --git a/dev/js/src/match/treearc.js b/dev/js/src/match/treearc.js
index 6f0690b..2acda9b 100644
--- a/dev/js/src/match/treearc.js
+++ b/dev/js/src/match/treearc.js
@@ -61,8 +61,7 @@
// Iterate over edge lists
// TODO:
// Support spans for anchors!
- for (var i in edges) {
- var edge = edges[i];
+ edges.forEach(function(edge) {
// Check the target identifier
var targetID = edge.targetID;
@@ -88,7 +87,7 @@
// console.log(relation);
this.addRel(relation);
};
- };
+ }, this);
// Reset parsing memory
this.temp = {};
@@ -100,8 +99,7 @@
_parse : function (parent, children, mark) {
// Iterate over all child nodes
- for (var i in children) {
- var c = children[i];
+ children.forEach(function(c) {
// Element node
if (c.nodeType == 1) {
@@ -230,8 +228,8 @@
this.temp['pos']++;
};
};
- }
- };
+ };
+ }, this);
// Todo: define edges here!
},
@@ -612,13 +610,7 @@
// Add sorted arcs and anchors
this._sortedArcs = lengthSort(sortedArcs, false);
-
- // Translate map to array (there is probably a better JS method)
- var sortedAnchors = [];
- for (var i in anchors) {
- sortedAnchors.push(anchors[i]);
- };
- this._sortedAnchors = lengthSort(sortedAnchors, true);
+ this._sortedAnchors = lengthSort(Object.keys(anchors), true);
},
/**
@@ -671,18 +663,18 @@
ws.style.textAnchor = "start";
var lastRight = 0;
- for (var node_i in this._tokens) {
+ this._tokens.forEach(function(node_i) {
// Append svg
// var x = text.appendChild(this._c("text"));
var tspan = text.appendChild(this._c("tspan"));
- tspan.appendChild(d.createTextNode(this._tokens[node_i]));
+ tspan.appendChild(d.createTextNode(node_i));
tspan.setAttribute("text-anchor", "middle");
this._tokenElements.push(tspan);
// Add whitespace!
tspan.setAttribute("dx", this.tokenSep);
- };
+ }, this);
// Get some global position data that may change on resize
var globalBoundingBox = this._rect(g);
@@ -702,15 +694,14 @@
this._sortArcs();
// 1. Draw all anchors
- var i;
- for (i in this._sortedAnchors) {
- this._drawAnchor(this._sortedAnchors[i]);
- };
+ this._sortedAnchors.forEach(
+ i => this._drawAnchor(i)
+ );
// 2. Draw all arcs
- for (i in this._sortedArcs) {
- this._drawArc(this._sortedArcs[i]);
- };
+ this._sortedArcs.forEach(
+ i => this._drawArc(i)
+ );
// Resize the svg with some reasonable margins
var width = this._rect(text).width;