Modernize for;;-loops
Change-Id: Ic6f86af0d674cc6643fc9eef2aa7431cfdf514f2
diff --git a/dev/js/src/init.js b/dev/js/src/init.js
index 4482316..4b19b7e 100644
--- a/dev/js/src/init.js
+++ b/dev/js/src/init.js
@@ -96,14 +96,13 @@
* Release notifications
*/
if (KorAP.Notifications !== undefined) {
- var n = KorAP.Notifications;
- for (var i = 0; i < n.length; i++) {
- var msg = n[i][1];
- if (n[i][2]) {
- msg += '<code class="src">'+n[i][2]+'</code>';
+ KorAP.Notifications.forEach(function(n) {
+ var msg = n[1];
+ if (n[2]) {
+ msg += '<code class="src">'+n[2]+'</code>';
};
- alertifyClass.log(msg, n[i][0], 10000);
- };
+ alertifyClass.log(msg, n[0], 10000);
+ });
};
/**
@@ -145,14 +144,11 @@
/**
* Add actions to match entries
*/
- var li = d.querySelectorAll(
+ var matchElements = d.querySelectorAll(
'#search > ol > li'
);
- var matchCount = 0;
- for (matchCount = 0; matchCount < li.length; matchCount++) {
-
- let e = li[matchCount];
+ matchElements.forEach(function(e) {
// Define class for active elements
if (e.classList.contains('active')) {
@@ -190,7 +186,7 @@
};
});
};
- };
+ }, this);
// Add focus listener to aside
var aside = d.getElementsByTagName('aside')[0];
@@ -284,7 +280,7 @@
* There is more than 0 matches, so allow for
* alignment toggling (left <=> right)
*/
- if (matchCount > 0)
+ if (matchElements.length > 0)
resultPanel.addAlignAction();
KorAP.Panel['result'] = resultPanel;
@@ -441,9 +437,7 @@
try {
// Register all plugins
- for (var i = 0; i < p.length; i++) {
- KorAP.Plugin.register(p[i]);
- }
+ p.forEach(i => KorAP.Plugin.register(i));
}
catch (e) {
KorAP.log(0, e);
diff --git a/dev/js/src/match.js b/dev/js/src/match.js
index 7ce2652..4f5aedf 100644
--- a/dev/js/src/match.js
+++ b/dev/js/src/match.js
@@ -97,18 +97,18 @@
};
// Iterate over info layers
- for (var i = 0; i < this.available.length; i++) {
- var term = this.available[i];
+ let layer;
+ this.available.forEach(function(term){
// Create info layer objects
try {
- var layer = require('match/infolayer').create(term);
+ layer = require('match/infolayer').create(term);
this._avail[layer.type].push(layer);
}
catch (e) {
- continue;
+ return;
};
- };
+ }, this);
return this;
},
diff --git a/dev/js/src/match/corpusByMatch.js b/dev/js/src/match/corpusByMatch.js
index 43048a3..92d219a 100644
--- a/dev/js/src/match/corpusByMatch.js
+++ b/dev/js/src/match/corpusByMatch.js
@@ -66,9 +66,10 @@
return;
};
- for (let doc of this._fragment.documents()) {
+
+ for (const doc of this._fragment.documents()) {
vc.addRequired(doc);
- console.log("Add " + doc.toQuery());
+ // console.log("Add " + doc.toQuery());
};
if (!vc.isOpen()) {
diff --git a/dev/js/src/match/meta.js b/dev/js/src/match/meta.js
index 78b1430..3cc7512 100644
--- a/dev/js/src/match/meta.js
+++ b/dev/js/src/match/meta.js
@@ -50,10 +50,8 @@
// Copy original array position to object
// before sorting by key title
let posInMetaArray = {};
- for (let i = 0; i < fields.length; i++) {
- posInMetaArray[fields[i]["key"]] = i;
+ fields.forEach((f,i) => posInMetaArray[f["key"]] = i);
- };
// TODO: Meta fields should be separated
// Sort all meta keys alphabetically
@@ -82,18 +80,17 @@
metaDD.setAttribute('data-type', field["type"]);
if(metaDescr instanceof Array){
- metaDD.classList.add("metakeyvalues");
- for (i = 0; i < metaDescr.length; i++){
-
+ metaDD.classList.add("metakeyvalues");
+ metaDescr.forEach(function(md) {
if (field["type"] === 'type:attachement') {
- let att = attClass.create(metaDescr[i]);
+ let att = attClass.create(md);
if (att)
metaDD.addE('div').appendChild(att.inline());
}
else {
- metaDD.addE('div').addT(metaDescr[i]);
+ metaDD.addE('div').addT(md);
}
- }
+ });
}
else{
if (field["type"] === 'type:attachement') {
diff --git a/dev/js/src/match/querycreator.js b/dev/js/src/match/querycreator.js
index fa4917b..f3c533f 100644
--- a/dev/js/src/match/querycreator.js
+++ b/dev/js/src/match/querycreator.js
@@ -229,9 +229,10 @@
// Is a key-value-cell
if (sib.classList.contains('matchkeyvalues')) {
- var divs = sib.getElementsByTagName('div');
- for (var j = 0; j < divs.length; j++) {
- var keyvaluepair = divs[j];
+
+ Array.from(
+ sib.getElementsByTagName('div')
+ ).forEach(function(keyvaluepair){
// Get annotation value from cell
var annotation = _getAnnotation(prefix, keyvaluepair);
@@ -242,7 +243,7 @@
this._addToToken(i, annotation);
keyvaluepair.classList.add('chosen');
};
- };
+ }, this);
}
// Normal cell
@@ -371,6 +372,8 @@
var str = '';
var distance = 0;
+ // This needs to take undefined tokens into account, therefore
+ // forEach() is not an option
for (var i = 0; i < this._query.length; i++) {
var token = this._query[i];
diff --git a/dev/js/src/match/table.js b/dev/js/src/match/table.js
index 9078780..92d4e5a 100644
--- a/dev/js/src/match/table.js
+++ b/dev/js/src/match/table.js
@@ -215,15 +215,17 @@
// There are multiple values to add
c.classList.add('matchkeyvalues');
- for (var n = 0; n < value.length; n++) {
- var e = c.addE('div');
- e.addT(value[n]);
- var anno = ah.getDesc(key, value[n]);
+ let e, anno;
+ value.forEach(function(v) {
+ e = c.addE('div');
+ e.addT(v);
+
+ anno = ah.getDesc(key, v);
if (anno)
e.setAttribute("title", anno);
- };
+ });
}
else {
@@ -272,29 +274,29 @@
var foundryList = Object.keys(this._foundry).sort();
- for (var f = 0; f < foundryList.length; f++) {
- var foundry = foundryList[f];
- var layerList =
+ let layerList, key, v, value, cell;
+
+ foundryList.forEach(function(foundry) {
+ let layerList =
Object.keys(this._foundry[foundry]).sort();
- for (var l = 0; l < layerList.length; l++) {
- var layer = layerList[l];
+ layerList.forEach(function(layer) {
+
tr = tbody.addE('tr');
tr.setAttribute('tabindex', 0);
tr.addCell = addCell;
-
tr.addCell('th', undefined, foundry);
tr.addCell('th', undefined, layer);
- var key = foundry + '/' + layer + '=';
+ key = foundry + '/' + layer + '=';
- for (var v = 0; v < this.length(); v++) {
+ for (v = 0; v < this.length(); v++) {
// Get the cell value
- var value = this.getValue(v, foundry, layer);
+ value = this.getValue(v, foundry, layer);
// Add cell to row
- var cell = tr.addCell(
+ cell = tr.addCell(
'td',
key,
value
@@ -304,8 +306,8 @@
cell.classList.add('mark');
};
};
- };
- };
+ }, this);
+ }, this);
// Add query creator
this._matchCreator = matchQueryCreator.create(this._element);
diff --git a/dev/js/src/match/treearc.js b/dev/js/src/match/treearc.js
index 2acda9b..e7d7ac3 100644
--- a/dev/js/src/match/treearc.js
+++ b/dev/js/src/match/treearc.js
@@ -99,7 +99,7 @@
_parse : function (parent, children, mark) {
// Iterate over all child nodes
- children.forEach(function(c) {
+ Array.from(children).forEach(function(c) {
// Element node
if (c.nodeType == 1) {
@@ -723,8 +723,7 @@
var stack = [];
// Iterate over all definitions
- for (var i = 0; i < list.length; i++) {
- var current = list[i];
+ list.forEach(function(current) {
// Check the stack order
var overlaps = 0;
@@ -766,7 +765,7 @@
stack.sort(function (a,b) {
b.overlaps - a.overlaps
});
- };
+ });
return stack;
};
diff --git a/dev/js/src/match/treehierarchy.js b/dev/js/src/match/treehierarchy.js
index 0a7bf49..9fc2480 100644
--- a/dev/js/src/match/treehierarchy.js
+++ b/dev/js/src/match/treehierarchy.js
@@ -287,14 +287,14 @@
if (v.class === "leaf") {
text.setAttribute('title', vLabel);
- var labelPart = vLabel.split(" ");
- var n = 0;
- for (var i = 0; i < labelPart.length; i++) {
- if (labelPart[i].length === 0)
- continue;
+ let n = 0;
+ let tspan;
+ vLabel.split(" ").forEach(function(p) {
+ if (p.length === 0)
+ return;
- var tspan = that._c('tspan');
- tspan.appendChild(d.createTextNode(labelPart[i]));
+ tspan = that._c('tspan');
+ tspan.appendChild(d.createTextNode(p));
if (n !== 0)
tspan.setAttribute('dy', LINEHEIGHT + 'pt');
else
@@ -302,7 +302,7 @@
tspan.setAttribute('x', v.x - v.width / 2);
y += LINEHEIGHT;
text.appendChild(tspan);
- };
+ });
y += LINEHEIGHT;
diff --git a/dev/js/src/menu.js b/dev/js/src/menu.js
index 98204ea..4d80419 100644
--- a/dev/js/src/menu.js
+++ b/dev/js/src/menu.js
@@ -206,7 +206,7 @@
if (this.prefix().length <= 0) {
// add all items to the list and lowlight
- var i = 0;
+ let i = 0;
for (; i < this._items.length; i++) {
this._list.push(i);
this._items[i].lowlight();
@@ -228,25 +228,22 @@
// Iterate over all items and choose preferred matching items
// i.e. the matching happens at the word start
- for (pos = 0; pos < this._items.length; pos++) {
+ this._items.forEach(function(it, pos){
- var points = 0;
+ let points = 0;
- for (pref = 0; pref < prefixList.length; pref++) {
- var prefix = " " + prefixList[pref];
+ prefixList.forEach(function(p) {
// Check if it matches at the beginning
- // if ((this.item(pos).lcField().indexOf(prefix)) >= 0) {
- if ((this.item(pos).lcField().includes(prefix))) {
+ if ((it.lcField().includes(" " + p))) {
points += 5;
}
// Check if it matches anywhere
- // else if ((this.item(pos).lcField().indexOf(prefix.substring(1))) >= 0) {
- else if ((this.item(pos).lcField().includes(prefix.substring(1)))) {
+ else if (it.lcField().includes(p)) {
points += 1;
};
- };
+ });
if (points > maxPoints) {
this._list = [pos];
@@ -255,7 +252,7 @@
else if (points == maxPoints) {
this._list.push(pos);
}
- };
+ },this);
// The list is empty - so lower your expectations
// Iterate over all items and choose matching items
diff --git a/dev/js/src/menu/item.js b/dev/js/src/menu/item.js
index e06de1e..f0c531f 100644
--- a/dev/js/src/menu/item.js
+++ b/dev/js/src/menu/item.js
@@ -194,21 +194,22 @@
var len = 0;
// Iterate over all prefixes and get the best one
- for (var i = 0; i < prefixes.length; i++) {
+ // for (var i = 0; i < prefixes.length; i++) {
+ prefixes.forEach(function(i) {
// Get first pos of a matching prefix
- testPos = textlc.indexOf(prefixes[i]);
+ testPos = textlc.indexOf(i);
if (testPos < 0)
- continue;
+ return;
if (pos === -1 || testPos < pos) {
pos = testPos;
- len = prefixes[i].length;
+ len = i.length;
}
- else if (testPos === pos && prefixes[i].length > len) {
- len = prefixes[i].length;
+ else if (testPos === pos && i.length > len) {
+ len = i.length;
};
- };
+ });
// Matches!
if (pos >= 0) {
diff --git a/dev/js/src/panel/match.js b/dev/js/src/panel/match.js
index a6c50db..99f7366 100644
--- a/dev/js/src/panel/match.js
+++ b/dev/js/src/panel/match.js
@@ -164,17 +164,15 @@
var menuList = [];
// Show tree views
- for (var i = 0; i < treeLayers.length; i++) {
- var span = treeLayers[i];
-
- // Add foundry/layer to menu list
- menuList.push([
- span.foundry + '/' + span.layer,
- span.foundry,
- span.layer,
- span.type
- ]);
- };
+ treeLayers.forEach(
+ s =>
+ // Add foundry/layer to menu list
+ menuList.push([
+ s.foundry + '/' + s.layer,
+ s.foundry,
+ s.layer,
+ s.type
+ ]));
// Create tree menu
this._menuList = menuList;
diff --git a/dev/js/src/selectMenu.js b/dev/js/src/selectMenu.js
index 4db1dcd..dc2c8e6 100644
--- a/dev/js/src/selectMenu.js
+++ b/dev/js/src/selectMenu.js
@@ -12,13 +12,13 @@
// Prepare list before object upgras
var list = [];
- var options = select.getElementsByTagName('option');
// Iterate through options list
- for (var i = 0; i < options.length; i++) {
-
+ Array.from(
+ select.getElementsByTagName('option')
// Get option item and add to list
- var item = options.item(i);
+ ).forEach(function(item) {
+
var opt = [
item.textContent,
item.getAttribute('value')
@@ -29,7 +29,7 @@
opt.push(item.getAttribute('desc'));
list.push(opt);
- };
+ });
// Create object with list
var obj = Object.create(menuClass).upgradeTo(this)
diff --git a/dev/js/src/session.js b/dev/js/src/session.js
index 2211fe6..178b83c 100644
--- a/dev/js/src/session.js
+++ b/dev/js/src/session.js
@@ -54,16 +54,17 @@
/* Parse cookie */
_parse : function () {
var c = document.cookie;
- var part = document.cookie.split(';');
- for(var i = 0; i < part.length; i++) {
- var pair = part[i].split('=');
- var name = pair[0].trim().toLowerCase();
- if (name === this._name) {
- if (pair.length === 1 || pair[1].length === 0)
+ document.cookie.split(';').forEach(
+ function(i) {
+ var pair = i.split('=');
+ var name = pair[0].trim().toLowerCase();
+ if (name === this._name) {
+ if (pair.length === 1 || pair[1].length === 0)
+ return;
+ this._hash = JSON.parse(decodeURIComponent(pair[1]));
return;
- this._hash = JSON.parse(decodeURIComponent(pair[1]));
- return;
- };
- };
+ };
+ }
+ );
}
});
diff --git a/dev/js/src/tutorial.js b/dev/js/src/tutorial.js
index 2420707..c2f7c69 100644
--- a/dev/js/src/tutorial.js
+++ b/dev/js/src/tutorial.js
@@ -95,27 +95,27 @@
* Decorate a page with query event handler.
*/
initQueries : function (d) {
- var qs = d.querySelectorAll('pre.query.tutorial:not(.unsupported)');
- var that = this;
- for (var i = 0; i < qs.length; i++) {
- qs[i].onclick = function (e) {
- that.useQuery(this,e);
- };
- };
+ let that = this;
+ Array.from(d.querySelectorAll('pre.query.tutorial:not(.unsupported)')).forEach(
+ i =>
+ i.onclick = function (e) {
+ that.useQuery(this,e);
+ }
+ );
},
/**
* Decorate a page with documentation links
*/
initDocLinks : function (d) {
- var dl = d.getElementsByClassName('doc-link');
- var that = this;
- for (var i = 0; i < dl.length; i++) {
- dl[i].onclick = function (e) {
- that.setPage(this.getAttribute('href'));
- return true;
- };
- };
+ let that = this;
+ Array.from(d.getElementsByClassName('doc-link')).forEach(
+ i =>
+ i.onclick = function (e) {
+ that.setPage(this.getAttribute('href'));
+ return true;
+ }
+ );
},
diff --git a/dev/js/src/vc/docgroup.js b/dev/js/src/vc/docgroup.js
index af49191..47f3a96 100644
--- a/dev/js/src/vc/docgroup.js
+++ b/dev/js/src/vc/docgroup.js
@@ -30,13 +30,13 @@
},
newAfter : function (obj) {
- for (var i = 0; i < this._operands.length; i++) {
- if (this._operands[i] === obj) {
+ this._operands.forEach(function (op, i) {
+ if (op === obj) {
var operand = unspecClass.create(this);
this._operands.splice(i + 1, 0, operand);
return this.update();
};
- };
+ }, this);
},
// The doc is already set in the group
@@ -48,15 +48,17 @@
if (operand.ldType() !== 'doc')
return null;
- for (var i = 0; i < this._operands.length; i++) {
- var op = this.getOperand(i);
- if (op.ldType() === 'doc'
- && operand.key() === op.key()
- && operand.matchop() === op.matchop()
- && operand.value() === op.value()) {
- return op;
- };
- };
+ const f = this._operands.find(
+ op =>
+ op.ldType() === 'doc'
+ && operand.key() === op.key()
+ && operand.matchop() === op.matchop()
+ && operand.value() === op.value()
+ );
+
+ if (f)
+ return f;
+
return null;
},
@@ -194,11 +196,9 @@
_removeChildren(group);
// Append operands
- for (var i = 0; i < this._operands.length; i++) {
- group.appendChild(
- this.getOperand(i).element()
- );
- };
+ this._operands.forEach(
+ op => group.appendChild(op.element())
+ );
// Set operators
var op = this.operators(
@@ -342,10 +342,10 @@
toJson : function () {
var opArray = new Array();
- for (var i = 0; i < this._operands.length; i++) {
- if (this._operands[i].ldType() !== 'non')
- opArray.push(this._operands[i].toJson());
- };
+ this._operands.forEach(function(op) {
+ if (op.ldType() !== 'non')
+ opArray.push(op.toJson());
+ });
return {
"@type" : "koral:" + this.ldType(),
"operation" : "operation:" + this.operation(),
diff --git a/dev/js/src/vc/jsonld.js b/dev/js/src/vc/jsonld.js
index 21260d4..cfa0acc 100644
--- a/dev/js/src/vc/jsonld.js
+++ b/dev/js/src/vc/jsonld.js
@@ -52,8 +52,7 @@
// In case of a group, destroy all operands
if (this._operands !== undefined) {
- for (var i = 0; i < this._operands.length; i++)
- this.getOperand(i).destroy();
+ this._operands.forEach(i => i.destroy());
this._operands = [];
};
},
diff --git a/dev/js/src/vc/rewritelist.js b/dev/js/src/vc/rewritelist.js
index 75f3d27..d6885c9 100644
--- a/dev/js/src/vc/rewritelist.js
+++ b/dev/js/src/vc/rewritelist.js
@@ -15,11 +15,12 @@
*/
fromJson : function (json) {
this._list = new Array();
- for (var i = 0; i < json.length; i++) {
- this._list.push(
- rewriteClass.create(json[i])
- );
- };
+ json.forEach(
+ i =>
+ this._list.push(
+ rewriteClass.create(i)
+ )
+ );
return this;
},
diff --git a/dev/js/src/vc/statistic.js b/dev/js/src/vc/statistic.js
index cd8d0ce..0c3c6ca 100644
--- a/dev/js/src/vc/statistic.js
+++ b/dev/js/src/vc/statistic.js
@@ -44,16 +44,14 @@
statDL.classList.add("flex");
var statistic = this._statistic;
- var keys = Object.keys(statistic);
- for (i = 0; i < keys.length; i++) {
+ Object.keys(statistic).forEach(function(k) {
statSp = statDL.addE('div')
statDT = statSp.addE('dt');
- var k = keys[i];
statDT.addT(k);
statDT.setAttribute('title', k);
statDD = statSp.addE('dd');
statDD.addT(new Number(statistic[k]).toLocaleString());
- }
+ });
this._element = statDL;
return this._element;
diff --git a/dev/js/src/view/match/tokentable.js b/dev/js/src/view/match/tokentable.js
index 2ac361d..e954068 100644
--- a/dev/js/src/view/match/tokentable.js
+++ b/dev/js/src/view/match/tokentable.js
@@ -84,8 +84,7 @@
else {
// Push newly to focus array
- for (var i = 0; i < tokens.length; i++) {
- var term = tokens[i];
+ tokens.forEach(function(term) {
try {
// Create info layer objects
var layer = infoLayerClass.create(term);
@@ -93,9 +92,9 @@
focus.push(layer);
}
catch (e) {
- continue;
+ return;
};
- };
+ });
};
// No tokens chosen