Commit 3c383627 authored by Tristan Cavelier's avatar Tristan Cavelier

complex_queries.js updated

parent 032cc5bc
...@@ -741,6 +741,33 @@ function stringEscapeRegexpCharacters(string) { ...@@ -741,6 +741,33 @@ function stringEscapeRegexpCharacters(string) {
_export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters); _export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters);
/**
* Convert metadata values to array of strings. ex:
*
* "a" -> ["a"],
* {"content": "a"} -> ["a"]
*
* @param {Any} value The metadata value
* @return {Array} The value in string array format
*/
function metadataValueToStringArray(value) {
var i, new_value = [];
if (value === undefined) {
return undefined;
}
if (!Array.isArray(value)) {
value = [value];
}
for (i = 0; i < value.length; i += 1) {
if (typeof value[i] === 'object') {
new_value[i] = value[i].content;
} else {
new_value[i] = value[i];
}
}
return new_value;
}
/** /**
* A sort function to sort items by key * A sort function to sort items by key
* *
...@@ -751,12 +778,50 @@ _export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters); ...@@ -751,12 +778,50 @@ _export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters);
function sortFunction(key, way) { function sortFunction(key, way) {
if (way === 'descending') { if (way === 'descending') {
return function (a, b) { return function (a, b) {
return a[key] < b[key] ? 1 : a[key] > b[key] ? -1 : 0; // this comparison is 5 times faster than json comparison
var i, l;
a = metadataValueToStringArray(a[key]) || [];
b = metadataValueToStringArray(b[key]) || [];
l = a.length > b.length ? a.length : b.length;
for (i = 0; i < l; i += 1) {
if (a[i] === undefined) {
return 1;
}
if (b[i] === undefined) {
return -1;
}
if (a[i] > b[i]) {
return -1;
}
if (a[i] < b[i]) {
return 1;
}
}
return 0;
}; };
} }
if (way === 'ascending') { if (way === 'ascending') {
return function (a, b) { return function (a, b) {
return a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0; // this comparison is 5 times faster than json comparison
var i, l;
a = metadataValueToStringArray(a[key]) || [];
b = metadataValueToStringArray(b[key]) || [];
l = a.length > b.length ? a.length : b.length;
for (i = 0; i < l; i += 1) {
if (a[i] === undefined) {
return -1;
}
if (b[i] === undefined) {
return 1;
}
if (a[i] > b[i]) {
return 1;
}
if (a[i] < b[i]) {
return -1;
}
}
return 0;
}; };
} }
throw new TypeError("complex_queries.sortFunction(): " + throw new TypeError("complex_queries.sortFunction(): " +
...@@ -1250,17 +1315,18 @@ SimpleQuery.prototype["="] = function (object_value, comparison_value, ...@@ -1250,17 +1315,18 @@ SimpleQuery.prototype["="] = function (object_value, comparison_value,
if (comparison_value === undefined) { if (comparison_value === undefined) {
if (value === undefined) { if (value === undefined) {
return true; return true;
} else {
return false;
} }
return false;
} }
if (value === undefined) { if (value === undefined) {
return false; return false;
} }
if (convertStringToRegExp( if (
comparison_value.toString(), convertStringToRegExp(
wildcard_character comparison_value.toString(),
).test(value.toString())) { wildcard_character
).test(value.toString())
) {
return true; return true;
} }
} }
...@@ -1290,17 +1356,18 @@ SimpleQuery.prototype["!="] = function (object_value, comparison_value, ...@@ -1290,17 +1356,18 @@ SimpleQuery.prototype["!="] = function (object_value, comparison_value,
if (comparison_value === undefined) { if (comparison_value === undefined) {
if (value === undefined) { if (value === undefined) {
return false; return false;
} else {
return true;
} }
return true;
} }
if (value === undefined) { if (value === undefined) {
return true; return true;
} }
if (convertStringToRegExp( if (
comparison_value.toString(), convertStringToRegExp(
wildcard_character comparison_value.toString(),
).test(value.toString())) { wildcard_character
).test(value.toString())
) {
return false; return 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