-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathadvanced_search.js
200 lines (153 loc) · 4.94 KB
/
advanced_search.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
'use strict';
/* eslint-disable no-invalid-this, no-use-before-define */
let Util = require('./util').Util;
let _ = require('underscore');
function argsToArray(argsObject) {
return Array.prototype.slice.call(argsObject);
}
class AdvancedSearch {
static equalityFields() {
let fields = argsToArray(arguments);
return this._createFieldAccessors(fields, EqualityNode);
}
static partialMatchFields() {
let fields = argsToArray(arguments);
return this._createFieldAccessors(fields, PartialMatchNode);
}
static textFields() {
let fields = argsToArray(arguments);
return this._createFieldAccessors(fields, TextNode);
}
static keyValueFields() {
let fields = argsToArray(arguments);
return this._createFieldAccessors(fields, KeyValueNode);
}
static multipleValueField(field, options) {
options = options || {};
return this._createFieldAccessors([field], MultipleValueNode, options);
}
static multipleValueOrTextField(field, options) {
options = options || {};
return this._createFieldAccessors([field], MultipleValueOrTextNode, options);
}
static rangeFields() {
let fields = argsToArray(arguments);
return this._createFieldAccessors(fields, RangeNode);
}
static _createFieldAccessors(fields, nodeClass, options) {
return fields.map((field) => {
this.prototype[field] = this._fieldTemplate(field, nodeClass, options);
return this.prototype[field];
});
}
static _fieldTemplate(field, NodeClass, options) {
return function () { return new NodeClass(field, this, options); };
}
constructor() { this.criteria = {}; }
addCriteria(key, value) { // eslint-disable-line consistent-return
if (this.criteria[key] === Object(this.criteria[key]) && !_.isArray(this.criteria[key])) {
return Util.merge(this.criteria[key], value);
}
this.criteria[key] = value;
}
toHash() { return this.criteria; }
}
class SearchNode {
static operators() {
let operators = argsToArray(arguments);
let operatorTemplate = operator => { // eslint-disable-line func-style
return function (value) {
let criterion = {};
criterion[operator] = `${value}`;
return this.parent.addCriteria(this.nodeName, criterion);
};
};
return operators.map((operator) => {
this.prototype[operator] = operatorTemplate(operator);
});
}
constructor(nodeName, parent) {
this.nodeName = nodeName;
this.parent = parent;
}
}
class EqualityNode extends SearchNode {
static initClass() {
this.operators('is', 'isNot');
}
}
EqualityNode.initClass();
class PartialMatchNode extends EqualityNode {
static initClass() {
this.operators('endsWith', 'startsWith');
}
}
PartialMatchNode.initClass();
class TextNode extends PartialMatchNode {
static initClass() {
this.operators('contains');
}
}
TextNode.initClass();
class KeyValueNode extends SearchNode {
is(value) { return this.parent.addCriteria(this.nodeName, value); }
}
class MultipleValueNode extends SearchNode {
constructor(nodeName, parent, options) {
super(nodeName, parent);
this.options = options;
}
allowedValues() { return this.options.allows; }
in() {
let values = argsToArray(arguments);
values = Util.flatten(values);
if (__guardMethod__(this, 'allowedValues', o => o.allowedValues())) {
let allowedValues = this.allowedValues();
let badValues = Util.without(values, allowedValues);
if (!Util.arrayIsEmpty(badValues)) { throw new Error(`Invalid argument(s) for ${this.nodeName}`); }
}
return this.parent.addCriteria(this.nodeName, values);
}
is(value) { return this.in(value); }
}
class MultipleValueOrTextNode extends MultipleValueNode {
static initClass() {
this.delegators('contains', 'endsWith', 'is', 'isNot', 'startsWith');
}
static delegators() {
let delegatedMethods = argsToArray(arguments);
let delegatorTemplate = methodName => { // eslint-disable-line func-style
return function (value) { return this.textNode[methodName](value); };
};
return delegatedMethods.map((methodName) => {
this.prototype[methodName] = delegatorTemplate(methodName);
});
}
constructor(nodeName, parent, options) {
super(nodeName, parent, options);
this.textNode = new TextNode(nodeName, parent);
}
}
MultipleValueOrTextNode.initClass();
class RangeNode extends SearchNode {
static initClass() {
this.operators('is');
}
between(min, max) {
this.min(min);
return this.max(max);
}
max(value) {
return this.parent.addCriteria(this.nodeName, {max: value});
}
min(value) {
return this.parent.addCriteria(this.nodeName, {min: value});
}
}
RangeNode.initClass();
module.exports = {AdvancedSearch: AdvancedSearch};
function __guardMethod__(obj, methodName, transform) { // eslint-disable-line consistent-return
if (typeof obj !== 'undefined' && obj !== null && typeof obj[methodName] === 'function') {
return transform(obj, methodName);
}
}