-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollection.js
202 lines (180 loc) · 5.43 KB
/
Collection.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* 集合类,封装数组的各种复杂取值操作,同时支持链式操作,简化业务逻辑代码
* by kingpjchen 20160114
*
* 例子:
* var games = new Collection(gameList)
* games.orderBy("installed").get() =》 按照是否安装排序并返回全部
* games.orderBy("installed").first() =》 按照是否安装排序并返回第一个
* games.orderBy("installed").last() =》 按照是否安装排序并返回最后一个
* games.where("gameName", "全民突击").get() =》 按照是否安装排序并返回最后一个
* games.where("installed_time", ">", new Date().getTime()) =》 获取安装时间大开当前时间的集合
* games.where("ctag", 1).orderBy("installer").toArrayByKey("appId") =》 获取分类id为1,然后按照是否安装排序,再取得appid对应的值存为数组
*/
define(function factory(require, exports, module) {
var $ = require('lib/zepto');
/**
* 集合类
* @param data {Array} 数组
* @constructor
*/
function Collection(data){
this.__data = data;
this.init();
}
var proto = Collection.prototype;
proto.init = function(){
this.mapCache = {};
};
/**
* 获取当前集合的成员个数
* @returns {Number}
*/
proto.count = function(){
return this.__data.length;
};
/**
* 跟据某个key,把所有对应值存成数组返回
* @param key
* @returns {Array}
* @example
* var games = new Collection(gameList)
* games.toArrayByKey("appid") =》 [363, 1000001286, 100555390, 1104466820, 101019894]
*/
proto.toArrayByKey = function(key){
var tempArr = [];
var data = this.get();
$.each(data, function(i, item){
tempArr.push(item[key]);
});
return tempArr;
};
/**
* 获取集合成员
* @param index {Number} 不传,获取全部集合,传值则获取相应下标成员
* @returns {*}
*/
proto.get = function(index){
var tempData = $.extend(true, {}, this.__data);
if(typeof index == "undefined"){
return tempData;
}
return tempData[index];
};
proto.first = function(){
return this.get(0);
};
proto.last = function(){
return this.get(this.count()-1);
};
/**
* 排序
* @param field {string} 跟据哪个字段排序
* @param type {string} 默认和desc为降序,asc为升序
*/
proto.orderBy = function(field, type){
var data = this.__data;
var iteration = null;
if(type=="asc"){
iteration = function(a, b){
return -((a[field] < b[field]) || -1);
}
}else{
iteration = function(a, b){
return (a[field] < b[field]) || -1;
}
}
data.sort(iteration);
return this;
};
/**
* 集合成员查询方法
* @param key
* @param condition
* @param value
* @returns {*}
* @example
* var games = new Collection(gameList)
* games.where("installed_time", ">", new Date().getTime())
*/
proto.where = function(key, condition, value){
var argLen = arguments.length;
var data = this.__data;
var iteration;
var tempArr = [];
if(argLen<2){
return this.getData();
}else if(argLen==2){
value = condition;
condition = "=";
}
switch (condition) {
case "=":
iteration = isEqual;
break;
case ">":
iteration = isGreaterThan;
break;
case ">=":
iteration = isGreaterThanOrIsEqual;
break;
case "<":
iteration = isLessThan;
break;
case "<=":
iteration = isLessThanOrIsEqual;
break;
case "in":
iteration = contains;
break;
default :
iteration = function(){}
}
$.each(data, function(i, item){
if(iteration(item[key], value)){
tempArr.push(item);
}
});
return new Collection(tempArr);
};
/**
* 将集合转成对象
* 有的场景需要多次对集合的每一项进行配置,这时转成对象key-value加速配置速度
* @param key
* @returns {*}
*/
proto.toMapByKey = function(key){
var that = this;
if (that.mapCache.key) return that.mapCache.key;
var obj = that.mapCache.key = {};
$.each(that.get(), function(i, item){
var target = obj[item[key]];
if(target){
!$.isArray(target) && (target = [target]);
target = target.push(item)
}else{
obj[item[key]] = item;
}
});
return obj;
};
function isEqual(a, b){
return a==b;
}
function isGreaterThan(a, b){
return a>b
}
function isLessThan(a, b){
return a<b
}
function isGreaterThanOrIsEqual(a, b){
return isEqual(a, b) || isGreaterThan(a, b)
}
function isLessThanOrIsEqual(a, b){
return isEqual(a, b) || isLessThan(a, b)
}
function contains(item, array){
return ~$.inArray(item, array);
}
return Collection;
});