-
Notifications
You must be signed in to change notification settings - Fork 11
/
knownFunctions.js
115 lines (115 loc) · 3.11 KB
/
knownFunctions.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
export const knownFunctions = {
and: function (x, y) {
return x && y;
},
or: function (x, y) {
return x || y;
},
isDefined: function (x, y) {
let tmpVal = !x ? y : x;
let isnum = /^[\d\.]+$/.test(tmpVal);
if (isnum) {
return tmpVal;
}
let tmpVal2 = document.getElementById(tmpVal);
return tmpVal2 ? tmpVal2.value : y; //used to be return tmpVal2 ? tmpVal2.value : tmpVal; but now it will return y if x is undefined
},
isNotDefined: function (x, y) {
return !x;
},
min: function (x, y) {
if (!x && !y) {
return "";
}
x = !isNaN(x) ? x : Number.POSITIVE_INFINITY;
y = !isNaN(y) ? y : Number.POSITIVE_INFINITY;
return Math.min(parseFloat(x), parseFloat(y));
},
max: function (x, y) {
if (!x && !y) {
return "";
}
x = !isNaN(x) ? x : Number.NEGATIVE_INFINITY;
y = !isNaN(y) ? y : Number.NEGATIVE_INFINITY;
return Math.max(parseFloat(x), parseFloat(y));
},
equals: function (x, y) {
if (x == undefined && y == "undefined") {
return true;
}
y = y.replace(/\"/g, ""); //handle string comparison
if (y === 'true') { //handles truthy comparison
y = true;
}
if (y === 'false') {
y = false;
}
if (y === '_TODAY_') {
var date = new Date();
var dateString = new Date(date.getTime() - (date.getTimezoneOffset() * 60000))
.toISOString()
.split("T")[0];
y = dateString;
}
return Array.isArray(x) ? x.includes(y) : x == y;
},
doesNotEqual: function (x, y) {
if (x == undefined && y == "undefined") {
return false;
}
y = y.replace(/\"/g, ""); //handle string comparison
if (y === 'true') { //handles truthy comparison
y = true;
}
if (y === 'false') {
y = false;
}
if (y === '_TODAY_') {
var date = new Date();
var dateString = new Date(date.getTime() - (date.getTimezoneOffset() * 60000))
.toISOString()
.split("T")[0];
y = dateString;
}
return Array.isArray(x) ? !x.includes(y) : x != y;
},
lessThan: function (x, y) {
return parseFloat(x) < parseFloat(y);
},
lessThanOrEqual: function (x, y) {
return parseFloat(x) <= parseFloat(y);
},
greaterThan: function (x, y) {
return parseFloat(x) > parseFloat(y);
},
greaterThanOrEqual: function (x, y) {
return parseFloat(x) >= parseFloat(y);
},
setFalse: function (x, y) {
return false;
},
difference: function (x, y) {
if (typeof y == "string" && document.getElementById(y)) {
y = document.getElementById(y).value;
}
return parseInt(x) - parseInt(y);
},
sum: function (x, y) {
if (typeof y == "string" && document.getElementById(y)) {
y = document.getElementById(y).value;
}
return parseInt(x) + parseInt(y);
},
percentDiff: function (x, y) {
if (x == "" || y == "") {
return false;
}
if (typeof y == "string" && document.getElementById(y)) {
y = document.getElementById(y).value;
}
return knownFunctions.difference(x, y) / x;
},
numberOfChoicesSelected: function (x) {
return x == undefined ? 0 : x.length;
},
};