dom/activities/src/ActivitiesServiceFilter.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/activities/src/ActivitiesServiceFilter.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict"
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = ['ActivitiesServiceFilter'];
    1.11 +
    1.12 +this.ActivitiesServiceFilter = {
    1.13 +  match: function(aValues, aFilters) {
    1.14 +
    1.15 +    function matchValue(aValue, aFilter, aFilterObj) {
    1.16 +      if (aFilter !== null) {
    1.17 +        // Custom functions for the different types.
    1.18 +        switch (typeof(aFilter)) {
    1.19 +        case 'boolean':
    1.20 +          return aValue === aFilter;
    1.21 +
    1.22 +        case 'number':
    1.23 +          return Number(aValue) === aFilter;
    1.24 +
    1.25 +        case 'string':
    1.26 +          return String(aValue) === aFilter;
    1.27 +
    1.28 +        default: // not supported
    1.29 +          return false;
    1.30 +        }
    1.31 +      }
    1.32 +
    1.33 +      // Pattern.
    1.34 +      if (('pattern' in aFilterObj)) {
    1.35 +        var pattern = String(aFilterObj.pattern);
    1.36 +
    1.37 +        var patternFlags = '';
    1.38 +        if (('patternFlags' in aFilterObj)) {
    1.39 +          patternFlags = String(aFilterObj.patternFlags);
    1.40 +        }
    1.41 +
    1.42 +        var re = new RegExp('^(?:' + pattern + ')$', patternFlags);
    1.43 +        return re.test(aValue);
    1.44 +      }
    1.45 +
    1.46 +      // Validation of the min/Max.
    1.47 +      if (('min' in aFilterObj) || ('max' in aFilterObj)) {
    1.48 +        // Min value.
    1.49 +        if (('min' in aFilterObj) &&
    1.50 +            aFilterObj.min > aValue) {
    1.51 +          return false;
    1.52 +        }
    1.53 +
    1.54 +        // Max value.
    1.55 +        if (('max' in aFilterObj) &&
    1.56 +            aFilterObj.max < aValue) {
    1.57 +          return false;
    1.58 +        }
    1.59 +      }
    1.60 +
    1.61 +      return true;
    1.62 +    }
    1.63 +
    1.64 +    // this function returns true if the value matches with the filter object
    1.65 +    function matchObject(aValue, aFilterObj) {
    1.66 +
    1.67 +      // Let's consider anything an array.
    1.68 +      let filters = ('value' in aFilterObj)
    1.69 +                      ? (Array.isArray(aFilterObj.value)
    1.70 +                          ? aFilterObj.value
    1.71 +                          : [aFilterObj.value])
    1.72 +                      : [ null ];
    1.73 +      let values  = Array.isArray(aValue) ? aValue : [aValue];
    1.74 +
    1.75 +      for (var filterId = 0; filterId < filters.length; ++filterId) {
    1.76 +        for (var valueId = 0; valueId < values.length; ++valueId) {
    1.77 +          if (matchValue(values[valueId], filters[filterId], aFilterObj)) {
    1.78 +            return true;
    1.79 +          }
    1.80 +        }
    1.81 +      }
    1.82 +
    1.83 +      return false;
    1.84 +    }
    1.85 +
    1.86 +    // Creation of a filter map useful to know what has been
    1.87 +    // matched and what is not.
    1.88 +    let filtersMap = {}
    1.89 +    for (let filter in aFilters) {
    1.90 +      // Convert this filter in an object if needed
    1.91 +      let filterObj = aFilters[filter];
    1.92 +
    1.93 +      if (Array.isArray(filterObj) || typeof(filterObj) !== 'object') {
    1.94 +        filterObj = {
    1.95 +          required: false,
    1.96 +          value: filterObj
    1.97 +        }
    1.98 +      }
    1.99 +
   1.100 +      filtersMap[filter] = { filter: filterObj,
   1.101 +                             found:  false };
   1.102 +    }
   1.103 +
   1.104 +    // For any incoming property.
   1.105 +    for (let prop in aValues) {
   1.106 +      // If this is unknown for the app, let's continue.
   1.107 +      if (!(prop in filtersMap)) {
   1.108 +        continue;
   1.109 +      }
   1.110 +
   1.111 +      if (Array.isArray(aValues[prop]) && aValues[prop].length == 0) {
   1.112 +        continue;
   1.113 +      }
   1.114 +
   1.115 +      // Otherwise, let's check the value against the filter.
   1.116 +      if (!matchObject(aValues[prop], filtersMap[prop].filter)) {
   1.117 +        return false;
   1.118 +      }
   1.119 +
   1.120 +      filtersMap[prop].found = true;
   1.121 +    }
   1.122 +
   1.123 +    // Required filters:
   1.124 +    for (let filter in filtersMap) {
   1.125 +      if (filtersMap[filter].filter.required && !filtersMap[filter].found) {
   1.126 +        return false;
   1.127 +      }
   1.128 +    }
   1.129 +
   1.130 +    return true;
   1.131 +  }
   1.132 +}

mercurial