query.js 3.58 KB
Newer Older
1 2 3 4
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global newClass: true, sortFunction: true, parseStringToObject: true,
         _export: true */

5 6 7 8 9 10 11
/**
 * The query to use to filter a list of objects.
 * This is an abstract class.
 *
 * @class Query
 * @constructor
 */
12
var Query = newClass(function (spec) {
Tristan Cavelier's avatar
Tristan Cavelier committed
13 14 15 16 17 18 19 20 21

  /**
   * The wildcard character used to extend comparison action
   *
   * @property wildcard_character
   * @type String
   */
  this.wildcard_character = spec.wildcard_character || "%";

22
  /**
Tristan Cavelier's avatar
Tristan Cavelier committed
23
   * Filter the item list with matching item only
24 25 26 27 28
   *
   * @method exec
   * @param  {Array} item_list The list of object
   * @param  {Object} [option={}] Some operation option
   * @param  {String} [option.wildcard_character="%"] The wildcard character
29 30 31 32 33
   * @param  {Array} [option.select_list=undefined] A object keys to retrieve
   * @param  {Array} [option.sort_on=[]] Couples of object keys
   *                                     and "ascending" or "descending"
   * @param  {Array} [option.limit=undefined] Couple of integer, first is an
   *                                          index and second is the length.
34
   */
Tristan Cavelier's avatar
Tristan Cavelier committed
35
  this.exec = function (item_list, option) {
36 37 38
    var i = 0;
    while (i < item_list.length) {
      if (!this.match(item_list[i], option.wildcard_character)) {
Tristan Cavelier's avatar
Tristan Cavelier committed
39 40 41 42 43 44 45 46 47
        item_list.splice(i, 1);
      } else {
        i += 1;
      }
    }
    if (option.sort_on) {
      Query.sortOn(option.sort_on, item_list);
    }
    if (option.limit) {
48 49 50 51
      item_list.splice(0, option.limit[0]);
      if (option.limit[1]) {
        item_list.splice(option.limit[1]);
      }
Tristan Cavelier's avatar
Tristan Cavelier committed
52
    }
53
    Query.filterListSelect(option.select_list || [], item_list);
Tristan Cavelier's avatar
Tristan Cavelier committed
54
  };
55 56 57

  /**
   * Test if an item matches this query
58
   *
59 60 61 62
   * @method match
   * @param  {Object} item The object to test
   * @return {Boolean} true if match, false otherwise
   */
63 64 65
  this.match = function (item, wildcard_character) {
    return true;
  };
66 67 68 69


  /**
   * Convert this query to a parsable string.
70
   *
71 72 73
   * @method toString
   * @return {String} The string version of this query
   */
74 75 76
  this.toString = function () {
    return "";
  };
77 78 79 80 81 82 83 84

  /**
   * Convert this query to an jsonable object in order to be remake thanks to
   * QueryFactory class.
   *
   * @method serialized
   * @return {Object} The jsonable object
   */
85 86 87
  this.serialized = function () {
    return undefined;
  };
88

89
}, {"static_methods": {
90 91 92 93 94 95 96 97

  /**
   * Filter a list of items, modifying them to select only wanted keys.
   *
   * @method filterListSelect
   * @param  {Array} select_option Key list to keep
   * @param  {Array} list The item list to filter
   */
98 99 100 101 102 103 104 105 106
  "filterListSelect": function (select_option, list) {
    list.forEach(function (item, index) {
      var new_item = {};
      select_option.forEach(function (key) {
        new_item[key] = item[key];
      });
      list[index] = new_item;
    });
  },
107 108 109 110 111 112 113 114

  /**
   * Sort a list of items, according to keys and directions.
   *
   * @method sortOn
   * @param  {Array} sort_on_option List of couples [key, direction]
   * @param  {Array} list The item list to sort
   */
115 116 117
  "sortOn": function (sort_on_option, list) {
    var sort_index;
    for (sort_index = sort_on_option.length - 1; sort_index >= 0;
118
         sort_index -= 1) {
119 120 121 122 123 124
      list.sort(sortFunction(
        sort_on_option[sort_index][0],
        sort_on_option[sort_index][1]
      ));
    }
  },
125 126 127 128 129 130 131 132 133

  /**
   * Parse a text request to a json query object tree
   *
   * @method parseStringToObject
   * @param  {String} string The string to parse
   * @return {Object} The json query tree
   */
  "parseStringToObject": parseStringToObject
134 135 136
}});

_export("Query", Query);