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/meta.js b/dev/js/src/match/meta.js
index c4cd63f..78b1430 100644
--- a/dev/js/src/match/meta.js
+++ b/dev/js/src/match/meta.js
@@ -56,12 +56,9 @@
};
// TODO: Meta fields should be separated
- const keys = Object.keys(posInMetaArray);
-
// Sort all meta keys alphabetically
- for (let i in keys.sort()) {
- let k = keys[i]; // This is the title
- let field = fields[posInMetaArray[keys[i]]]; // This is the object
+ Object.keys(posInMetaArray).sort().forEach(function(k) {
+ let field = fields[posInMetaArray[k]]; // This is the object
// Ignore internal IDs
if (k !== "UID" &&
@@ -111,7 +108,7 @@
metaDL.appendChild(metaL);
};
- };
+ });
// Add corpusByMatch assistant
this._corpusByMatch = cbmClass.create(this._element);
diff --git a/dev/js/src/match/querycreator.js b/dev/js/src/match/querycreator.js
index 8c60ff8..fa4917b 100644
--- a/dev/js/src/match/querycreator.js
+++ b/dev/js/src/match/querycreator.js
@@ -405,13 +405,9 @@
};
// Set query language field
- var qlf = this._ql.options;
- for (var i in qlf) {
- if (qlf[i].value == 'poliqarp') {
- qlf[i].selected = true;
- break;
- };
- };
+ const ql = this._ql.options.find(e => e.value == 'poliqarp');
+ if (ql)
+ ql.selected = true;
// Insert to query bar
this._q.value = this.toString();
diff --git a/dev/js/src/match/table.js b/dev/js/src/match/table.js
index 3ad2731..9078780 100644
--- a/dev/js/src/match/table.js
+++ b/dev/js/src/match/table.js
@@ -104,8 +104,7 @@
_parse : function (children, mark) {
// Get all children
- for (var i in children) {
- var c = children[i];
+ children.forEach(function(c) {
// Create object on position unless it exists
if (this._info[this._pos] === undefined) {
@@ -180,7 +179,7 @@
this._token[this._pos++] = c.nodeValue;
};
};
- };
+ }, this);
delete this._info[this._pos];
},
@@ -250,7 +249,7 @@
tr.addCell('th', undefined, 'Layer');
// Add tokens
- for (var i in this._token) {
+ Object.keys(this._token).forEach(function(i) {
let surface = this.getToken(i);
var c = tr.addCell('th', undefined, surface);
if (this._mark[i]) {
@@ -267,7 +266,7 @@
if (surface.length > 20) {
c.setAttribute("title", surface)
}
- };
+ }, this);
var tbody = table.addE('tbody');
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;
diff --git a/dev/js/src/match/treehierarchy.js b/dev/js/src/match/treehierarchy.js
index e9c6122..0a7bf49 100644
--- a/dev/js/src/match/treehierarchy.js
+++ b/dev/js/src/match/treehierarchy.js
@@ -109,8 +109,7 @@
// Parse the snippet
_parse : function (parent, children, mark) {
- for (var i in children) {
- var c = children[i];
+ children.forEach(function(c) {
// Element node
if (c.nodeType == 1) {
@@ -164,7 +163,7 @@
this._addEdge(parent, id);
};
- };
+ }, this);
return this;
},