Commit 7cc44e36 authored by Tristan Cavelier's avatar Tristan Cavelier

query.js filter static methods are now able to clone the given list or not

parent 1c36476d
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global newClass: true, sortFunction: true, parseStringToObject: true,
_export: true, stringEscapeRegexpCharacters: true */
_export: true, stringEscapeRegexpCharacters: true, deepClone: true */
/**
* The query to use to filter a list of objects.
......@@ -39,12 +39,9 @@ var Query = newClass(function () {
Query.sortOn(option.sort_on, item_list);
}
if (option.limit) {
item_list.splice(0, option.limit[0]);
if (option.limit[1]) {
item_list.splice(option.limit[1]);
}
Query.limit(option.limit, item_list);
}
Query.filterListSelect(option.select_list || [], item_list);
Query.select(option.select_list || [], item_list);
};
/**
......@@ -163,15 +160,21 @@ var Query = newClass(function () {
}, {"static_methods": {
/**
* Filter a list of items, modifying them to select only wanted keys.
* Filter a list of items, modifying them to select only wanted keys. If
* `clone` is true, then the method will act on a cloned list.
*
* @method filterListSelect
* @method select
* @static
* @param {Array} select_option Key list to keep
* @param {Array} list The item list to filter
* @param {Boolean} [clone=false] If true, modifies a clone of the list
* @return {Array} The filtered list
*/
"filterListSelect": function (select_option, list) {
"select": function (select_option, list, clone) {
var i, j, new_item;
if (clone) {
list = deepClone(list);
}
for (i = 0; i < list.length; i += 1) {
new_item = {};
for (j = 0; j < select_option.length; j += 1) {
......@@ -184,18 +187,25 @@ var Query = newClass(function () {
}
}
}
return list;
},
/**
* Sort a list of items, according to keys and directions.
* Sort a list of items, according to keys and directions. If `clone` is true,
* then the method will act on a cloned list.
*
* @method sortOn
* @static
* @param {Array} sort_on_option List of couples [key, direction]
* @param {Array} list The item list to sort
* @param {Boolean} [clone=false] If true, modifies a clone of the list
* @return {Array} The filtered list
*/
"sortOn": function (sort_on_option, list) {
"sortOn": function (sort_on_option, list, clone) {
var sort_index;
if (clone) {
list = deepClone(list);
}
for (sort_index = sort_on_option.length - 1; sort_index >= 0;
sort_index -= 1) {
list.sort(sortFunction(
......@@ -203,6 +213,29 @@ var Query = newClass(function () {
sort_on_option[sort_index][1]
));
}
return list;
},
/**
* Limit a list of items, according to index and length. If `clone` is true,
* then the method will act on a cloned list.
*
* @method limit
* @static
* @param {Array} limit_option A couple [from, length]
* @param {Array} list The item list to limit
* @param {Boolean} [clone=false] If true, modifies a clone of the list
* @return {Array} The filtered list
*/
"limit": function (limit_option, list, clone) {
if (clone) {
list = deepClone(list);
}
list.splice(0, limit_option[0]);
if (limit_option[1]) {
list.splice(limit_option[1]);
}
return list;
},
/**
......
......@@ -128,3 +128,31 @@ function sortFunction(key, way) {
return a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0;
};
}
/**
* Clones all native object in deep. Managed types: Object, Array, String,
* Number, Boolean, null.
*
* @param {A} object The object to clone
* @return {A} The cloned object
*/
function deepClone(object) {
var i, cloned;
if (Object.prototype.toString.call(object) === "[object Array]") {
cloned = [];
for (i = 0; i < object.length; i += 1) {
cloned[i] = deepClone(object[i]);
}
return cloned;
}
if (typeof object === "object") {
cloned = {};
for (i in object) {
if (object.hasOwnProperty(i)) {
cloned[i] = deepClone(object[i]);
}
}
return cloned;
}
return object;
}
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