-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathconditions.js
More file actions
191 lines (183 loc) · 5.58 KB
/
conditions.js
File metadata and controls
191 lines (183 loc) · 5.58 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use strict';
const ipaddr = require('ipaddr.js');
const {
isString,
isBoolean,
isNumber,
isArray,
isUndefined,
isEmpty,
forEach,
every,
} = require('lodash/fp');
const conditions = {
NumericEquals(a, b) {
if (!isNumber(a) || !isNumber(b)) return false;
return a === b;
},
NumericNotEquals(a, b) {
if (!isNumber(a) || !isNumber(b)) return false;
return !this.conditions.NumericEquals.apply(this, arguments);
},
NumericLessThan(a, b) {
if (!isNumber(a) || !isNumber(b)) return false;
return a < b;
},
NumericGreaterThanEquals(a, b) {
if (!isNumber(a) || !isNumber(b)) return false;
return !this.conditions.NumericLessThan.apply(this, arguments);
},
NumericGreaterThan(a, b) {
if (!isNumber(a) || !isNumber(b)) return false;
return a > b;
},
NumericLessThanEquals(a, b) {
if (!isNumber(a) || !isNumber(b)) return false;
return !this.conditions.NumericGreaterThan.apply(this, arguments);
},
DateEquals(a, b) {
a = new Date(a), b = new Date(b);
if (a == 'Invalid Date' || b == 'Invalid Date') return false;
return a >= b && a <= b;
},
DateNotEquals(a, b) {
a = new Date(a), b = new Date(b);
if (a == 'Invalid Date' || b == 'Invalid Date') return false;
return !this.conditions.DateEquals.apply(this, arguments);
},
DateLessThan(a, b) {
a = new Date(a), b = new Date(b);
if (a == 'Invalid Date' || b == 'Invalid Date') return false;
return a < b;
},
DateGreaterThanEquals(a, b) {
a = new Date(a), b = new Date(b);
if (a == 'Invalid Date' || b == 'Invalid Date') return false;
return !this.conditions.DateLessThan.apply(this, arguments);
},
DateGreaterThan(a, b) {
a = new Date(a), b = new Date(b);
if (a == 'Invalid Date' || b == 'Invalid Date') return false;
return a > b;
},
DateLessThanEquals(a, b) {
a = new Date(a), b = new Date(b);
if (a == 'Invalid Date' || b == 'Invalid Date') return false;
return !this.conditions.DateGreaterThan.apply(this, arguments);
},
BinaryEquals(a, b) {
if (process.env.BROWSER) {
if (!isString(b) || !(a instanceof Uint8Array)) return false;
const buf = new Uint8Array(atob(b).split('').map(function(s) { return s.charCodeAt(0); }));
return a.every(function(x, i) { return x === buf[i]; });
} else {
if (!isString(b) || !(a instanceof Buffer)) return false;
return a.equals(new Buffer(b, 'base64'));
}
},
BinaryNotEquals(a, b) {
if (process.env.BROWSER) {
if (!isString(b) || !(a instanceof Uint8Array)) return false;
const buf = new Uint8Array(atob(b).split('').map(function(s) { return s.charCodeAt(0); }));
return !a.every(function(x, i) { return x === buf[i]; });
} else {
if (!isString(b) || !(a instanceof Buffer)) return false;
return !a.equals(new Buffer(b, 'base64'));
}
},
ArnLike: function ArnLike(a, b) {
if (!isString(b)) return false;
return new RegExp('^' +
b.replace(/[\-\[\]\/\{\}\(\)\+\.\\\^\$\|]/g, "\\$&")
.replace(/\*/g, '[^:]*') // TODO: check if last part of ARN can contain ':'
.replace(/\?/g, '.') + '$')
.test(a);
},
ArnNotLike: function StringNotLike(a, b) {
if (!isString(b)) return false;
return !this.conditions.ArnLike.apply(this, arguments);
},
ArnEquals: function ArnEquals(a, b) {
return this.conditions.ArnLike(a, b);
},
ArnNotEquals: function ArnEquals(a, b) {
return this.conditions.ArnNotLike(a, b);
},
Null(a, b) {
if (!isBoolean(b)) return false;
return b ? isUndefined(a) : !isUndefined(a);
},
IpAddress(a, b) {
try {
if (!a || !b) return false;
const addr = ipaddr.parse(a);
if (b.indexOf('/') !== -1) {
const range = ipaddr.parseCIDR(b);
return addr.match(range);
}
const bddr = ipaddr.parse(b);
return addr.toString() === bddr.toString();
} catch(error) {
return false;
}
},
NotIpAddress() {
return !this.conditions.IpAddress.apply(this, arguments);
},
StringEquals(a, b) {
if (!isString(a) || !isString(b)) return false;
return a === b;
},
StringNotEquals(a, b) {
if (!isString(a) || !isString(b)) return false;
return a !== b;
},
StringEqualsIgnoreCase(a, b) {
if (!isString(a) || !isString(b)) return false;
return a.toLowerCase() === b.toLowerCase();
},
StringNotEqualsIgnoreCase(a, b) {
if (!isString(a) || !isString(b)) return false;
return a.toLowerCase() !== b.toLowerCase();
},
StringLike(a, b) {
if (!isString(b)) return false;
return new RegExp('^' +
b.replace(/[\-\[\]\/\{\}\(\)\+\.\\\^\$\|]/g, "\\$&")
.replace(/\*/g, '.*')
.replace(/\?/g, '.') + '$')
.test(a);
},
StringNotLike(a, b) {
if (!isString(b)) return false;
return !this.conditions.StringLike.apply(this, arguments);
},
Bool(a, b) {
if (!isBoolean(a) || !isBoolean(b)) return false;
return a === b;
},
};
forEach(function (condition) {
const fn = conditions[condition];
conditions[condition + 'IfExists'] = function (a, b) {
if (isUndefined(a)) return true;
else return fn.apply(this, arguments);
};
conditions['ForAllValues:' + condition] = function (a, b) {
if (!isArray(a)) a = [a];
return every(value => {
return b.find(key => {
return fn.call(this, value, key);
});
}, a);
};
conditions['ForAnyValue:' + condition] = function (a, b) {
if (!isArray(a)) a = [a];
return a.find(value => {
return b.find(key => {
return fn.call(this, value, key);
});
});
};
}, Object.keys(conditions));
module.exports = conditions;