Commit a99b1dfb authored by Romain Courteaud's avatar Romain Courteaud

Query: reduce number of created regexp

parent caab106c
...@@ -3,7 +3,12 @@ ...@@ -3,7 +3,12 @@
(function (RSVP, window, parseStringToObject) { (function (RSVP, window, parseStringToObject) {
"use strict"; "use strict";
var query_class_dict = {}; var query_class_dict = {},
regexp_escape = /[\-\[\]{}()*+?.,\\\^$|#\s]/g,
regexp_percent = /%/g,
regexp_underscore = /_/g,
regexp_operator = /^(?:AND|OR|NOT)$/i,
regexp_comparaison = /^(?:!?=|<=?|>=?)$/i;
/** /**
* Convert metadata values to array of strings. ex: * Convert metadata values to array of strings. ex:
...@@ -383,11 +388,7 @@ ...@@ -383,11 +388,7 @@
* @return {String} The escaped string * @return {String} The escaped string
*/ */
function stringEscapeRegexpCharacters(string) { function stringEscapeRegexpCharacters(string) {
if (typeof string === "string") { return string.replace(regexp_escape, "\\$&");
return string.replace(/([\\\.\$\[\]\(\)\{\}\^\?\*\+\-])/g, "\\$1");
}
throw new TypeError("Query.stringEscapeRegexpCharacters(): " +
"Argument no 1 is not of type 'string'");
} }
/** /**
...@@ -425,13 +426,9 @@ ...@@ -425,13 +426,9 @@
if (use_wildcard_characters === false) { if (use_wildcard_characters === false) {
return new RegExp("^" + stringEscapeRegexpCharacters(string) + "$"); return new RegExp("^" + stringEscapeRegexpCharacters(string) + "$");
} }
return new RegExp("^" + stringEscapeRegexpCharacters(string).replace( return new RegExp("^" + stringEscapeRegexpCharacters(string)
/%/g, .replace(regexp_percent, '.*')
".*" .replace(regexp_underscore, '.') + "$");
).replace(
/_/g,
"."
) + "$");
} }
/** /**
...@@ -483,7 +480,7 @@ ...@@ -483,7 +480,7 @@
*/ */
ComplexQuery.prototype.match = function (item) { ComplexQuery.prototype.match = function (item) {
var operator = this.operator; var operator = this.operator;
if (!(/^(?:AND|OR|NOT)$/i.test(operator))) { if (!(regexp_operator.test(operator))) {
operator = "AND"; operator = "AND";
} }
return this[operator.toUpperCase()](item); return this[operator.toUpperCase()](item);
...@@ -770,9 +767,9 @@ ...@@ -770,9 +767,9 @@
value = null, value = null,
key = this.key; key = this.key;
if (!(/^(?:!?=|<=?|>=?)$/i.test(operator))) { if (!(regexp_comparaison.test(operator))) {
// `operator` is not correct, we have to change it to "like" or "=" // `operator` is not correct, we have to change it to "like" or "="
if (/%/.test(this.value)) { if (regexp_percent.test(this.value)) {
// `value` contains a non escaped `%` // `value` contains a non escaped `%`
operator = "like"; operator = "like";
} else { } else {
...@@ -881,10 +878,7 @@ ...@@ -881,10 +878,7 @@
if (typeof value.cmp === "function") { if (typeof value.cmp === "function") {
return RSVP.resolve(value.cmp(comparison_value) === 0); return RSVP.resolve(value.cmp(comparison_value) === 0);
} }
if ( if (comparison_value.toString() === value.toString()) {
searchTextToRegExp(comparison_value.toString(), false).
test(value.toString())
) {
return RSVP.resolve(true); return RSVP.resolve(true);
} }
} }
...@@ -942,10 +936,7 @@ ...@@ -942,10 +936,7 @@
if (typeof value.cmp === "function") { if (typeof value.cmp === "function") {
return RSVP.resolve(value.cmp(comparison_value) !== 0); return RSVP.resolve(value.cmp(comparison_value) !== 0);
} }
if ( if (comparison_value.toString() === value.toString()) {
searchTextToRegExp(comparison_value.toString(), false).
test(value.toString())
) {
return RSVP.resolve(false); return RSVP.resolve(false);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment