-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathOperators.js
More file actions
127 lines (112 loc) · 3.81 KB
/
Copy pathOperators.js
File metadata and controls
127 lines (112 loc) · 3.81 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Created by Hao on 2017/3/6.
*/
let moment = require("moment");
class Operators {
// ====================================== logic operations ===================================
allOf(params) {
/**
* @param a list of boolean expressions
* @return boolean: true if all true, false if any false
* */
if (!(params instanceof Array)) throw new Error(`${params} is not an array.`);
return params.every(b=>{return b});
}
anyOf(params) {
/**
* @param a list of boolean expressions
* @return boolean: true if any true, false if all false
* */
if (!(params instanceof Array)) throw new Error(`${params} is not an array.`);
return params.some(b=>{return b});
}
not(param) {
/**
* @param a key to be negated
* */
return Array.isArray(param)? !param[0] : !param;
}
// ==================================== comparative operations ====================================
between(value, param, attribute = null) {
/**
* @param [string] a string containg two parameters separated by a space.
* @return boolean: true if the value is in section (start, end).
* */
let start = param.split(" ")[0];
let end = param.split(" ")[1];
switch (attribute) {
case "time":
// in case only time is specified
value = moment(value, "HH:mm:ss");
start = moment(start, "HH:mm:ss");
end = moment(end, "HH:mm:ss");
if (end < start) end.add(1, "d");
break;
case "date":
// in case date is specified
value = moment(value, "YYYY-MM-DD");
start = moment(start, "YYYY-MM-DD");
end = moment(end, "YYYY-MM-DD");
break;
case "weekday":
// in case only weekday is specified
value = moment(value, "dddd");
start = moment(start, "dddd");
end = moment(start, "dddd");
break;
default:
value = parseInt(value);
start = parseInt(start);
end = parseInt(end);
console.log(`${attribute} is not explicitly specified for between operation.`);
}
return (value > start && value < end);
}
equals(value, param, attribute = null) {
/**
* @param [number/string] a parameter for comparison
* */
return String(param)==='*' || String(param)===String(value);
}
moreThan(value, param, attribute = null) {
/**
* @param [number/string] a parameter for comparison
* */
return parseInt(value) > parseInt(param);
}
lessThan(value, param, attribute = null) {
/**
* @param [number/string] a parameter for comparison
* */
return parseInt(value) < parseInt(param);
}
contains(value, param, attribute = null){
/**
* @param [string] a key to be detected
* */
return value.includes(param);
}
like(value, param, attribute = null){
/**
* @param [string] a pattern with wildcards (one or more "*")
* */
if (value === undefined) return false;
let keys = param.split("*");
for (let i in keys){
if (!(keys.hasOwnProperty(i))) continue;
let key = keys[i], index = value.indexOf(key);
if (index<0){
return false;
}
value = value.slice(index+key.length);
}
return true;
}
in(value, param, attribute = null) {
/**
* @param [array] a list
* */
return param.includes(value);
}
}
module.exports = Operators;