Skip to content

Commit 295e83d

Browse files
committed
maint(lib dependshandler): Modernize code.
1 parent 8bba671 commit 295e83d

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

src/lib/dependshandler.js

+36-20
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,60 @@ function DependsHandler($el, expression) {
1111

1212
DependsHandler.prototype = {
1313
_findInputs: function (name) {
14-
var $input = this.$context.find(":input[name='" + name + "']");
15-
if (!$input.length) $input = $("#" + name);
14+
let $input = this.$context.find(":input[name='" + name + "']"); // TODO input outside form
15+
if (!$input.length) {
16+
$input = $("#" + name);
17+
}
1618
return $input;
1719
},
1820

1921
_getValue: function (name) {
20-
var $input = this._findInputs(name);
21-
if (!$input.length) return null;
22-
23-
if ($input.attr("type") === "radio" || $input.attr("type") === "checkbox")
22+
const $input = this._findInputs(name);
23+
if (!$input.length) {
24+
return null;
25+
}
26+
if ($input.attr("type") === "radio" || $input.attr("type") === "checkbox") {
2427
return $input.filter(":checked").val() || null;
25-
else return $input.val();
28+
}
29+
return $input.val();
2630
},
2731

2832
getAllInputs: function () {
29-
var todo = [this.ast],
30-
$inputs = $(),
31-
node;
33+
const todo = [this.ast];
34+
let $inputs = $();
35+
let node;
3236

3337
while (todo.length) {
3438
node = todo.shift();
35-
if (node.input) $inputs = $inputs.add(this._findInputs(node.input));
36-
if (node.children && node.children.length)
39+
if (node.input) {
40+
$inputs = $inputs.add(this._findInputs(node.input));
41+
}
42+
if (node.children && node.children.length) {
3743
todo.push.apply(todo, node.children);
44+
}
3845
}
3946
return $inputs;
4047
},
4148

4249
_evaluate: function (node) {
43-
var value = node.input ? this._getValue(node.input) : null,
44-
i;
50+
const value = node.input ? this._getValue(node.input) : null;
4551

4652
switch (node.type) {
4753
case "NOT":
4854
return !this._evaluate(node.children[0]);
4955
case "AND":
50-
for (i = 0; i < node.children.length; i++)
51-
if (!this._evaluate(node.children[i])) return false;
56+
for (const child of node.children.length) {
57+
if (!this._evaluate(child)) {
58+
return false;
59+
}
60+
}
5261
return true;
5362
case "OR":
54-
for (i = 0; i < node.children.length; i++)
55-
if (this._evaluate(node.children[i])) return true;
63+
for (const child of node.children) {
64+
if (this._evaluate(child)) {
65+
return true;
66+
}
67+
}
5668
return false;
5769
case "comparison":
5870
switch (node.operator) {
@@ -69,10 +81,14 @@ DependsHandler.prototype = {
6981
case ">=":
7082
return value >= node.value;
7183
case "~=":
72-
if (value === null) return false;
84+
if (value === null) {
85+
return false;
86+
}
7387
return value.indexOf(node.value) != -1;
7488
case "=~":
75-
if (value === null || !node.value) return false;
89+
if (value === null || !node.value) {
90+
return false;
91+
}
7692
return node.value.indexOf(value) != -1;
7793
}
7894
break;

0 commit comments

Comments
 (0)