-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
123 lines (109 loc) · 2.65 KB
/
index.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
var _ = require('lodash');
module.exports = {
registerHandler: registerHandler,
filter: filter
};
function registerHandler (op, handler, overwrite) {
if (_.isObject(op)) {
_.each(op, _.partialRight(registerHandler, overwrite));
return;
}
if (handlers[op] && !overwrite) {
throw new Error('Handler is already registered: ' + op + '!');
}
handlers[op] = handler;
}
var fieldlessCommands = ['and', 'or', 'not', '!'];
var handlers = {
'=': eqHandler,
'>': _.partial(comparisonHandler, '>'),
'<': _.partial(comparisonHandler, '<'),
'>=': _.partial(comparisonHandler, '>='),
'<=': _.partial(comparisonHandler, '<='),
'in': _.partial(arrayArgHandler, 'whereIn'),
'@>': _.partial(arrayFnHandler, '@>'),
'between': _.partial(arrayArgHandler, 'whereBetween'),
'like': _.partial(comparisonHandler, 'like'),
'ilike': _.partial(comparisonHandler, 'ilike'),
'!': notHandler,
'not': notHandler,
'and': _.partial(logicalHandler, 'where'),
'or': _.partial(logicalHandler, 'orWhere')
};
function eqHandler (field, arg) {
if (arg === null) {
this.whereNull(field);
} else {
this.where(field, arg);
}
}
function comparisonHandler (op, field, arg) {
this.where(field, op, arg);
}
function arrayArgHandler (op, field, arg) {
if (!_.isArray(arg) || _.isEmpty(arg)) {
return;
}
this[op](field, arg);
}
function arrayFnHandler (op, field, arg) {
if (!_.isArray(arg) || _.isEmpty(arg)) {
return;
}
this.where(field, op, arg);
}
function notHandler (field, arg) {
this.whereNot(filter(arg));
}
function logicalHandler (op, field, arg) {
if (_.isArray(arg) && !_.isEmpty(arg)) {
this.where(function () {
_.each(arg, function (arg) {
logicalHandler.call(this, op, null, arg);
}, this);
});
return;
}
if (!_.isObject(arg)) {
return;
}
if (op === 'where') {
return walkLevel.call(this, arg);
}
this.where(function () {
var self = this
_.each(arg, function (arg, commandText) {
self[op](filter(_.zipObject([commandText], [arg])));
});
});
}
function parseCommand (text) {
var tokens = text.split(' ');
if (tokens.length === 1 && _.includes(fieldlessCommands, tokens[0])) {
return {
field: null,
command: tokens[0]
};
}
return {
field: tokens[0],
command: tokens[1] || '='
};
}
function walkLevel (level) {
var builder = this;
_.each(compactObject(level), function (arg, commandText) {
var command = parseCommand(commandText);
handlers[command.command].call(builder, command.field, arg);
});
}
function filter (query) {
return function () {
walkLevel.call(this, query);
};
}
function compactObject (o) {
return _.omit(o, function (v) {
return v === undefined || (_.isArray(v) || _.isString(v)) && _.isEmpty(v);
});
}