This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpressions.js
419 lines (400 loc) · 12.5 KB
/
expressions.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
* MOST Web Framework
* A JavaScript Web Framework
* http://themost.io
*
* Copyright (c) 2014, Kyriakos Barbounakis k.barbounakis@gmail.com, Anthi Oikonomou anthioikonomou@gmail.com
*
* Released under the BSD3-Clause license
* Date: 2014-05-16
*/
var util = require('util');
/**
* @class ArithmeticExpression
* @param {*=} p0 The left operand
* @param {String=} oper The operator
* @param {*=} p1 The right operand
* @constructor
*/
function ArithmeticExpression(p0, oper, p1)
{
this.left = p0;
this.operator = oper || '$add';
this.right = p1;
}
ArithmeticExpression.OperatorRegEx = /^(\$add|\$sub|\$mul|\$div|\$mod)$/g;
ArithmeticExpression.prototype.exprOf = function()
{
var p;
if (typeof this.left === 'undefined' || this.left===null)
throw new Error('Expected left operand');
else if (typeof this.left.exprOf === 'function')
p = this.left.exprOf();
else
p = this.left;
if (typeof this.operator === 'undefined' || this.operator===null)
throw new Error('Expected arithmetic operator.');
if (this.operator.match(ArithmeticExpression.OperatorRegEx)===null)
throw new Error('Invalid arithmetic operator.');
//build right operand e.g. { $add:[ 5 ] }
var r = {};
if (typeof this.right === 'undefined' || this.right===null) {
r[this.operator]=[null];
}
else if (typeof this.right.exprOf === 'function') {
if (this.right instanceof MemberExpression) {
r[this.operator] = [{ "$name": this.right.exprOf() }];
}
else {
r[this.operator] = [this.right.exprOf()];
}
}
else {
r[this.operator]=[this.right];
}
//add left operand e.g { Price: { $add:[ 5 ] } }
var result = {};
result[p] = r;
//return query expression
return result;
};
/**
* @class MemberExpression
* @param {String} name The name of the current member
* @constructor
*/
function MemberExpression(name) {
this.name = name;
}
MemberExpression.prototype.exprOf = function() {
return this.name;
};
/**
* @class
* @param {string} oper
* @param {*} args
* @constructor
*/
function LogicalExpression(oper, args) {
this.operator = oper || '$and' ;
this.args = args || [];
}
LogicalExpression.OperatorRegEx = /^(\$and|\$or|\$not|\$nor)$/g;
LogicalExpression.prototype.exprOf = function() {
if (this.operator.match(LogicalExpression.OperatorRegEx)===null)
throw new Error('Invalid logical operator.');
if (!util.isArray(this.args))
throw new Error('Logical expression arguments cannot be null at this context.');
if (this.args.length===0)
throw new Error('Logical expression arguments cannot be empty.');
var p = {};
p[this.operator] = [];
for (var i = 0; i < this.args.length; i++) {
var arg = this.args[i];
if (typeof arg === 'undefined' || arg===null)
p[this.operator].push(null);
else if (typeof arg.exprOf === 'function')
p[this.operator].push(arg.exprOf());
else
p[this.operator].push(arg);
}
return p;
};
/**
* @class LiteralExpression
* @param {*} value The literal value
* @constructor
*/
function LiteralExpression(value) {
this.value = value;
}
LiteralExpression.prototype.exprOf = function() {
if (typeof this.value === 'undefined')
return null;
return this.value;
};
/**
*
* @param {*} left
* @param {String=} op
* @param {*=} right
* @constructor
*/
function ComparisonExpression(left, op, right)
{
this.left = left;
this.operator = op || '$eq';
this.right = right;
}
ComparisonExpression.OperatorRegEx = /^(\$eq|\$ne|\$lte|\$lt|\$gte|\$gt|\$in|\$nin)$/g;
ComparisonExpression.prototype.exprOf = function()
{
if (typeof this.operator === 'undefined' || this.operator===null)
throw new Error('Expected comparison operator.');
var p, expr, name;
if (this.left instanceof MethodCallExpression)
{
p = {};
if (typeof this.right === 'undefined' || this.right===null)
p[this.operator]=null;
else if (typeof this.right.exprOf === 'function')
p[this.operator] = (this.right instanceof MemberExpression) ? { $name:this.right.exprOf() } : this.right.exprOf();
else
p[this.operator]=this.right;
if (this.operator==='$eq')
this.left.args.push(p.$eq);
else
this.left.args.push(p);
//return query expression
return this.left.exprOf();
}
else if (this.left instanceof ArithmeticExpression)
{
p = {};
//build comparison expression e.g. { $gt:10 }
if (typeof this.right === 'undefined' || this.right===null)
p[this.operator]=null;
else if (typeof this.right.exprOf === 'function')
p[this.operator] = (this.right instanceof MemberExpression) ? { $name:this.right.exprOf() } : this.right.exprOf();
else
p[this.operator]=this.right;
//get left expression
expr = this.left.exprOf();
//find argument list
name = Object.keys(expr)[0];
if (this.operator==='$eq')
expr[name][this.left.operator].push(p.$eq);
else
expr[name][this.left.operator].push(p);
//return query expression
return expr;
}
else if (this.left instanceof MemberExpression)
{
p = {};
//build comparison expression e.g. { $gt:10 }
if (typeof this.right === 'undefined' || this.right===null)
p[this.operator]=null;
else if (typeof this.right.exprOf === 'function') {
p[this.operator] = (this.right instanceof MemberExpression) ? { $name:this.right.exprOf() } : this.right.exprOf();
}
else
p[this.operator]=this.right;
name = this.left.name;
expr = {};
if (this.operator==='$eq' && !(this.right instanceof MemberExpression))
expr[name]=p.$eq;
else
expr[name] = p;
//return query expression
return expr;
}
};
/**
* Creates a method call expression
* @class MethodCallExpression
* @constructor
*/
function MethodCallExpression(name, args) {
/**
* Gets or sets the name of this method
* @type {String}
*/
this.name = name;
/**
* Gets or sets an array that represents the method arguments
* @type {Array}
*/
this.args = [];
if (util.isArray(args))
this.args = args;
}
/**
* Converts the current method to the equivalent query expression e.g. { orderDate: { $year: [] } } which is equivalent with year(orderDate)
* @returns {*}
*/
MethodCallExpression.prototype.exprOf = function() {
var method = {};
var result = {};
var name = '$'.concat(this.name);
//set arguments array
method[name] = [] ;
if (this.args.length===0)
throw new Error('Unsupported method expression. Method arguments cannot be empty.');
//get first argument
if (this.args[0] instanceof MemberExpression) {
var member = this.args[0].name;
for (var i = 1; i < this.args.length; i++)
{
var arg = this.args[i];
if (typeof arg === 'undefined' || arg===null)
method[name].push(null);
else if (typeof arg.exprOf === 'function')
method[name].push((arg instanceof MemberExpression) ? { $name:arg.exprOf() } : arg.exprOf());
else
method[name].push(arg);
}
result[member] = method;
return result;
}
else {
throw new Error('Unsupported method expression. The first argument of a method expression must be always a MemberExpression.');
}
};
var expressions = {
Operators : {
Not:'$not',
// Multiplicative
Mul:'$mul',
Div:'$div',
Mod:'$mod',
// Additive
Add:'$add',
Sub:'$sub',
// Relational and type testing
Lt:'$lt',
Gt:'$gt',
Le:'$lte',
Ge:'$gte',
// Equality
Eq:'$eq',
Ne:'$ne',
// In Values
In:'$in',
NotIn:'$nin',
// Conditional AND
And:'$and',
// Conditional OR
Or:'$or'
},
ArithmeticExpression:ArithmeticExpression,
MemberExpression:MemberExpression,
MethodCallExpression:MethodCallExpression,
ComparisonExpression:ComparisonExpression,
LiteralExpression:LiteralExpression,
LogicalExpression:LogicalExpression,
/**
* @param {*=} left The left operand
* @param {String=} operator The operator
* @param {*=} right The right operand
* @returns ArithmeticExpression
*/
createArithmeticExpression : function(left, operator, right) {
return new ArithmeticExpression(left, operator, right);
},
/**
* @param {*=} left The left operand
* @param {String=} operator The operator
* @param {*=} right The right operand
* @returns ComparisonExpression
*/
createComparisonExpression : function(left, operator, right) {
return new ComparisonExpression(left, operator, right);
},
/**
* @param {String=} name A string that represents the member's name
* @returns MemberExpression
*/
createMemberExpression : function(name) {
return new MemberExpression(name);
},
/**
* @param {*=} value The literal value
* @returns LiteralExpression
*/
createLiteralExpression : function(value) {
return new LiteralExpression(value);
},
/**
* Creates a method call expression of the given name with an array of arguments
* @param {String} name
* @param {Array} args
* @returns {MethodCallExpression}
*/
createMethodCallExpression : function(name, args) {
return new MethodCallExpression(name, args);
},
/**
* Creates a logical expression
* @param {String} operator The logical operator
* @param {Array} args An array that represents the expression's arguments
* @returns {LogicalExpression}
*/
createLogicalExpression : function(operator, args) {
return new LogicalExpression(operator, args);
},
/**
* Gets a boolean value that indicates whether or not the given object is an ArithmeticExpression instance.
* @param obj
*/
isArithmeticExpression: function(obj) {
return obj instanceof ArithmeticExpression;
},
/**
* Gets a boolean value that indicates whether or not the given operator is an arithmetic operator.
* @param {String} op
*/
isArithmeticOperator: function(op) {
if (typeof op === 'string')
return (op.match(ArithmeticExpression.OperatorRegEx)!==null);
return false;
},
/**
* Gets a boolean value that indicates whether or not the given operator is an arithmetic operator.
* @param {String} op
*/
isComparisonOperator: function(op) {
if (typeof op === 'string')
return (op.match(ComparisonExpression.OperatorRegEx)!==null);
return false;
},
/**
* Gets a boolean value that indicates whether or not the given operator is a logical operator.
* @param {String} op The current operator
*/
isLogicalOperator: function(op) {
if (typeof op === 'string')
return (op.match(LogicalExpression.OperatorRegEx)!==null);
return false;
},
/**
* Gets a boolean value that indicates whether or not the given object is an LogicalExpression instance.
* @param obj
* @returns Boolean
*/
isLogicalExpression: function(obj) {
return obj instanceof LogicalExpression;
},
/**
* Gets a boolean value that indicates whether or not the given object is an ComparisonExpression instance.
* @param obj
*/
isComparisonExpression: function(obj) {
return obj instanceof ComparisonExpression;
},
/**
* Gets a boolean value that indicates whether or not the given object is an MemberExpression instance.
* @param obj
*/
isMemberExpression: function(obj) {
return obj instanceof MemberExpression;
},
/**
* Gets a boolean value that indicates whether or not the given object is an LiteralExpression instance.
* @param obj
*/
isLiteralExpression: function(obj) {
return obj instanceof LiteralExpression;
},
/**
* Gets a boolean value that indicates whether or not the given object is an MemberExpression instance.
* @param obj
*/
isMethodCallExpression: function(obj) {
return obj instanceof MethodCallExpression;
}
};
if (typeof exports !== 'undefined')
{
module.exports = expressions;
}