Code coverage report for thinkjs/lib/Lib/Util/Filter.js

Statements: 100% (50 / 50)      Branches: 100% (36 / 36)      Functions: 100% (9 / 9)      Lines: 100% (50 / 50)      Ignored: none     

All files » thinkjs/lib/Lib/Util/ » Filter.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127        1               6               7 6   7 1   6 8 8 8 8 6                   14 14 7   7               6 2 2 1   1   4 3   4 1   3 3 6 6 6 5     3                   4 2   4 2   2                   5 4   5 1   4 7                   1   35 35 34 34 34   1  
/**
 * 过滤器
 * @return {[type]} [description]
 */
var Filter = module.exports = {
  /**
   * 分页
   * @param  {[type]} value [description]
   * @return {[type]}       [description]
   */
  page: function(value){
    'use strict';
    return this.id(value) || 1;
  },
  /**
   * xxx asc,yyy desc
   * @return {[type]} [description]
   */
  order: function(value){
    'use strict';
    if (isString(value)) {
      value = value.split(',');
    }
    if (!isArray(value)) {
      return '';
    }
    return value.filter(function(item){
      item = item.trim().split(' ');
      var field = item[0];
      var type = item[1];
      if (/^(ASC|DESC)$/i.test(type) && /^[\w]+$/.test(field)) {
        return field + ' ' + type;
      }
    }).join(',');
  },
  /**
   * 大于0
   * @return {[type]} [description]
   */
  id: function(value){
    'use strict';
    value = parseInt(value + '', 10);
    if (value > 0) {
      return value;
    }
    return 0;
  },
  /**
   * id列表
   * @return {[type]} [description]
   */
  ids: function(value, split){
    'use strict';
    if (isNumber(value)) {
      value = this.id(value);
      if (value) {
        return [value];
      }
      return [];
    }
    if (isString(value)) {
      value = value.split(split || ',');
    }
    if (!isArray(value)) {
      return [];
    }
    var ret = [];
    for(var i = 0, length = value.length; i < length; i++){
      var item = (value[i] + '').trim();
      item = parseInt(item, 10);
      if (item > 0) {
        ret.push(item);
      }
    }
    return ret;
  },
  /**
   * 是否在一个中
   * @param  {[type]} value [description]
   * @param  {[type]} arr   [description]
   * @return {[type]}       [description]
   */
  in: function(value, arr){
    'use strict';
    if (!isArray(arr)) {
      arr = [arr];
    }
    if(arr.indexOf(value) > -1){
      return value;
    }
    return '';
  },
  /**
   * 将字符串切割为数组
   * @param  {[type]} value [description]
   * @param  {[type]} split [description]
   * @return {[type]}       [description]
   */
  strs: function(value, split){
    'use strict';
    if (isString(value)) {
      value = value.split(split || ',');
    }
    if (!isArray(value)) {
      return [];
    }
    return value.filter(function(item){
      return (item + '').trim();
    });
  }
};
/**
 * 调用一个过滤器
 * @param  {[type]} data [description]
 * @param  {[type]} type [description]
 * @return {[type]}      [description]
 */
Filter.filter = function(value, type){
  'use strict';
  var fn = Filter[type];
  if (typeof fn === 'function') {
    var args = [].slice.call(arguments, 2);
    args.unshift(value);
    return Filter[type].apply(Filter, args);
  }
  return false;
};