forked from wrackzone/RallyTreeGrid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryStringParser.js
More file actions
188 lines (160 loc) · 5.44 KB
/
QueryStringParser.js
File metadata and controls
188 lines (160 loc) · 5.44 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
(function() {
var Ext = window.Ext4 || window.Ext;
/**
* @private
* Parses an ALM compatible query string
*/
Ext.define('Rally.data.util.QueryStringParser', {
config: {
/**
* @cfg {String} (required)
* The string to parse into a {Rally.data.QueryFilter}
*/
string: null
},
constructor: function (config) {
this.initConfig(config);
},
/**
* @private
* {RegEx} The operation regex
*/
operations: /^\s*(=|!=|<=|>=|<|>|AND|OR|[!]?CONTAINS|\(|\))/i,
/**
* @private
* {RegEx} The quote regex
*/
quotes: /^\s*"([^"|\\"]+)"/,
/**
* @private
* {RegEx} The word regex
*/
words: /^\s*([^ \)]+)/,
/**
* @private
* {Array[String]} Array of valid operations to parse from a string expression
*/
operators: ['=','!=','<=','>=','<','>','AND','OR','!CONTAINS','CONTAINS'],
/**
* Find the next token
* @private
* @return {String}
*/
peek: function () {
var string = Ext.String.trim(this.string),
matches;
matches = this.operations.exec(string);
if (matches && matches.length) {
return matches[1];
}
matches = this.quotes.exec(string);
if (matches && matches.length) {
return matches[1];
}
matches = this.words.exec(string);
if (matches && matches.length) {
return matches[1];
}
return '';
},
/**
* Remove the stringToFind from the string to move onto the next token
* @private
* @param {String} stringToFind
* @return {String}
*/
consume: function (stringToFind) {
string = Ext.String.trim(this.string);
this.string = string.substring(string.indexOf(stringToFind) + stringToFind.length);
},
/**
* Parse the next term in the string
* @private
* @return {String|Rally.data.QueryFilter}
*/
parseNextTerm: function () {
var nextTerm = this.peek();
if (nextTerm === '(') {
this.consume('(');
var expression = this.applyOperators(this.operators);
if (!(expression instanceof Rally.data.QueryFilter)) {
throw new Error('Invalid expression starting at "' + expression + '"');
}
this.consume(')');
return expression;
} else {
this.consume(nextTerm);
return nextTerm;
}
},
applyOperators: function (operators, operator) {
if (!operators.length) {
return this.parseNextTerm();
}
if (!operator) {
operator = operators[0];
}
var property = this.applyOperators(operators.slice(1));
while (this.peek() === operator) {
this.consume(operator);
var value = this.applyOperators(operators.slice(1));
property = Ext.create('Rally.data.QueryFilter', {
property: property,
operator: operator,
value: this._convertToType(value)
});
}
return property;
},
/**
* Make sure we are aware of types
* @param value
* @private
*/
_convertToType: function (value) {
if (!Ext.isString(value)) {
return value;
}
if (value.toLowerCase() === 'true') {
return true;
} else if (value.toLowerCase() === 'false') {
return false;
} else if ((value.length > 0) && !isNaN(+value)) {
return +value;
} else {
return value;
}
},
/**
* Convert the operations in the provided string to uppercase
* @private
* @return {String}
*/
_operatorsToUpperCase: function (string) {
string = string.replace(/\)\s+and\s+\(/ig, ') AND (');
string = string.replace(/\)\s+or\s+\(/ig, ') OR (');
return string.replace(/\b(contains)/ig, 'CONTAINS');
},
/**
* Parse the expression provided if possible
* @return {Rally.data.QueryFilter}
*/
parseExpression: function () {
var originalString = this.string;
if (this.string === null) {
throw new Error ('Cannot parse null query: ' + originalString);
}
this.string = Ext.String.trim(this.string);
this.string = this._operatorsToUpperCase(this.string);
var filter = this.parseNextTerm();
if (!(filter instanceof Rally.data.QueryFilter)) {
throw new Error ('Failed to parse query: ' + originalString);
}
var trailingQuery = this.parseNextTerm();
if(trailingQuery) {
throw new Error ('Failed to parse query: found trailing info at "' + trailingQuery + '". Make sure you wrap the whole query in parentheses.');
}
return filter;
}
});
})();