Commit fdcb654c authored by Vincent Bechu's avatar Vincent Bechu Committed by Vincent Bechu

[query] use key_schema to sort

parent 167105f4
......@@ -44,8 +44,8 @@
* @param {String} [way="ascending"] 'ascending' or 'descending'
* @return {Function} The sort function
*/
function sortFunction(key, way) {
var result;
function sortFunction(key, way, key_schema) {
var result, cast_to;
if (way === 'descending') {
result = 1;
} else if (way === 'ascending') {
......@@ -54,6 +54,29 @@
throw new TypeError("Query.sortFunction(): " +
"Argument 2 must be 'ascending' or 'descending'");
}
if (key_schema !== undefined &&
key_schema.key_set !== undefined &&
key_schema.key_set[key] !== undefined &&
key_schema.key_set[key].cast_to !== undefined) {
if (typeof key_schema.key_set[key].cast_to === "string") {
cast_to = key_schema.cast_lookup[key_schema.key_set[key].cast_to];
} else {
cast_to = key_schema.key_set[key].cast_to;
}
return function (a, b) {
var f_a = cast_to(a[key]), f_b = cast_to(b[key]);
if (typeof f_b.cmp === 'function') {
return result * f_b.cmp(f_a);
}
if (f_a > f_b) {
return -result;
}
if (f_a < f_b) {
return result;
}
return 0;
};
}
return function (a, b) {
// this comparison is 5 times faster than json comparison
var i, l;
......@@ -78,6 +101,7 @@
};
}
/**
* Sort a list of items, according to keys and directions.
*
......@@ -85,7 +109,7 @@
* @param {Array} list The item list to sort
* @return {Array} The filtered list
*/
function sortOn(sort_on_option, list) {
function sortOn(sort_on_option, list, key_schema) {
var sort_index;
if (!Array.isArray(sort_on_option)) {
throw new TypeError("jioquery.sortOn(): " +
......@@ -95,7 +119,8 @@
sort_index -= 1) {
list.sort(sortFunction(
sort_on_option[sort_index][0],
sort_on_option[sort_index][1]
sort_on_option[sort_index][1],
key_schema
));
}
return list;
......@@ -270,7 +295,7 @@
}
if (option.sort_on) {
sortOn(option.sort_on, item_list);
sortOn(option.sort_on, item_list, this._key_schema);
}
if (option.limit) {
......@@ -609,7 +634,7 @@
*/
QueryFactory.create = function (object, key_schema) {
if (object === "") {
return new Query();
return new Query(key_schema);
}
if (typeof object === "string") {
object = parseStringToObject(object);
......
......@@ -505,6 +505,78 @@
});
});
test('Query & sort_on option', function () {
var doc_list = [
{
idendifier: 'a',
date: "Fri, 08 Sep 2017 07:46:27 +0000"
},
{
identifier: 'c',
date: "Wed, 06 Sep 2017 00:27:13 +0000"
},
{
identifier: 'b',
date: "Thu, 07 Sep 2017 18:59:23 +0000"
}
];
stop();
expect(2);
jIO.QueryFactory.create("").exec(
doc_list,
{sort_on: [['date', 'descending']]}
).
then(function (list) {
var key_schema =
{
key_set: {
date: {
read_from: 'date',
cast_to: 'dateType'
}
},
cast_lookup: {
dateType: function (str) {
return window.jiodate.JIODate(new Date(str).toISOString());
}
}
};
deepEqual(list, [
{
identifier: 'c',
date: "Wed, 06 Sep 2017 00:27:13 +0000"
},
{
identifier: 'b',
date: "Thu, 07 Sep 2017 18:59:23 +0000"
},
{
idendifier: 'a',
date: "Fri, 08 Sep 2017 07:46:27 +0000"
}
], 'Document list is sorted');
return jIO.QueryFactory.create("", key_schema).exec(
doc_list,
{sort_on: [['date', 'ascending']]}
);
})
.then(function (list) {
deepEqual(list, [
{
identifier: 'c',
date: "Wed, 06 Sep 2017 00:27:13 +0000"
},
{
identifier: 'b',
date: "Thu, 07 Sep 2017 18:59:23 +0000"
},
{
idendifier: 'a',
date: "Fri, 08 Sep 2017 07:46:27 +0000"
}
], 'Document list is sorted with key_schema');
}).always(start);
});
// Asterisk wildcard is not supported yet.
/* test('Full text query with asterisk', function () {
var doc_list = [
......
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