-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbool-logic-validator.js
More file actions
102 lines (87 loc) · 3.21 KB
/
bool-logic-validator.js
File metadata and controls
102 lines (87 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
(function (self) {
var getIdentifiers = function (expression) {
if (!expression) {
return [];
}
if (expression.type === 'Identifier') {
return [expression.name];
} else {
return getIdentifiers(expression.left)
.concat(getIdentifiers(expression.right))
.concat(getIdentifiers(expression.argument));
}
}
var removeDuplicateIdentifiers = function (identifiers) {
var uniqueSet = [];
var uniqueValues = [];
identifiers.forEach(value => {
if (uniqueSet[value] !== true) {
uniqueSet[value] = true;
uniqueValues.push(value);
}
});
return uniqueValues;
}
var identifierListsAreEqual = function (listA, listB) {
if (listA.length !== listB.length) {
return false;
}
for (indexA in listA) {
if (listB.indexOf(listA[indexA]) === -1) {
return false;
}
}
return true;
}
var createIdentifierCombinations = function (identifiers, combination) {
if (identifiers.length === 0) {
return [combination];
}
var current = identifiers[0];
var remaining = identifiers.slice(1);
return createIdentifierCombinations(remaining, combination + current + " = true; ")
.concat(createIdentifierCombinations(remaining, combination + current + " = false; "));
}
var validateExpressions = function (identifierCombinations, exprA, exprB) {
var failures = [];
identifierCombinations.forEach(combination => {
var fullExprA = combination + "Boolean(" + exprA + ")";
var fullExprB = combination + "Boolean(" + exprB + ")";
var resultA = eval(fullExprA);
var resultB = eval(fullExprB);
if (resultA !== resultB) {
failures.push({ combination: combination, resultA: resultA, resultB: resultB });
}
});
return failures;
}
var validate = function (expressionA, expressionB) {
var exprA, exprB;
try {
exprA = jsep(expressionA);
} catch (e) {
throw ("Expression A: " + e.message);
return;
}
try {
exprB = jsep(expressionB);
} catch (e) {
throw ("Expression B: " + e.message);
return;
}
var identifiersA = getIdentifiers(exprA);
var identifiersB = getIdentifiers(exprB);
var uniqueIdentifiersA = removeDuplicateIdentifiers(identifiersA);
var uniqueIdentifiersB = removeDuplicateIdentifiers(identifiersB);
if (identifierListsAreEqual(uniqueIdentifiersA, uniqueIdentifiersB) === false) {
throw ("Expression arguments doesn't match: Expression A (" + uniqueIdentifiersA.join() + "), Expression B (" + uniqueIdentifiersB.join() + ")");
return;
}
var combinations = createIdentifierCombinations(uniqueIdentifiersA, "");
var errors = validateExpressions(combinations, expressionA, expressionB);
return errors;
}
self.boolLogicValidator = {
validate
};
})(this);