-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_main.lodash.gen.js
214 lines (190 loc) · 8.43 KB
/
test_main.lodash.gen.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
203
204
205
206
207
208
209
210
211
212
213
214
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
/**
* Created by timtian on 2016/7/12.
*/
var testData = require('./test_data.js').arrayData;
var _ = require('lodash');
//"id": 1,
//"name": "Finn Obrien",
//"email": "[email protected]",
//"count": 103,
//"type": "A",
//"city": "Iqaluit",
//"country": "Canada",
//"birthday": "497520060"
var testCase = {};
testCase.formatter = {};
testCase.formatter.formatMoney = function (number) {
return number.toFixed(2).replace(/./g, function (c, i, a) {
return i && c !== "." && (a.length - i) % 3 === 0 ? ',' + c : c;
});
};
testCase.customReduce = function (empty, result, value, key) {
var ids = [];
value.forEach(function (item) {
ids.push(item.id);
});
return ids;
};
testCase.demo = function (type, id, size) {
return function (params) {
/**
* select id, name, ${type} from ${testData} where id>${id} order by id desc limit 10, ${size}
*/var source = params[1];var res = _.chain(source).filter(function (item) {
return item['id'] > params[2];
}).map(function (item) {
return { 'id': item['id'], 'name': item['name'], 'field_2': params[0] };
}).orderBy(['id'], ['desc']).slice(10, 10 + params[3]).value();return res;
}([type, testData, id, size]);
};
testCase.checkPrefix = function (str) {
return 'hi:this is a test ' + str + '!';
};
testCase.selectById = function (id) {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return item['id'] == params[1];
}).map(function (item) {
return _extends({}, item);
}).value();return res;
}([testData, id]);
};
testCase.selectByNotEqId = function (excludeId, minId) {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return item['id'] != params[1] && item['id'] > params[2];
}).map(function (item) {
return _extends({}, item);
}).value();return res;
}([testData, excludeId, minId]);
};
testCase.selectByIdAndName = function (id, name) {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return item['id'] == params[1] && item['name'] == params[2];
}).map(function (item) {
return _extends({}, item);
}).value();return res;
}([testData, id, name]);
};
testCase.selectByIdOrCount = function (id, mincount) {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return item['id'] == params[1] || item['count'] > params[2];
}).map(function (item) {
return _extends({}, item);
}).value();return res;
}([testData, id, mincount]);
};
testCase.selectByBwId = function (minid, maxid) {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return item['id'] >= params[1] && item['id'] <= params[2];
}).map(function (item) {
return _extends({}, item);
}).value();return res;
}([testData, minid, maxid]);
};
testCase.selectByBwIdWithOrder = function (minid, maxid, start, count) {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return item['id'] >= params[1] && item['id'] <= params[2];
}).map(function (item) {
return _extends({}, item);
}).orderBy(['type', 'count', 'id'], ['asc', 'desc', 'asc']).slice(params[3], params[3] + params[4]).value();return res;
}([testData, minid, maxid, start, count]);
};
testCase.selectByMaxIdWithOrderType = function (maxid, ordername, ordertype) {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return item['id'] <= params[1];
}).map(function (item) {
return _extends({}, item);
}).orderBy([params[2], 'id'], [params[3], 'asc']).value();return res;
}([testData, maxid, ordername, ordertype]);
};
testCase.selectByMaxWithLimit = function (maxid) {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return item['id'] <= params[1];
}).map(function (item) {
return _extends({}, item);
}).orderBy(['type', 'count', 'id'], ['asc', 'desc', 'asc']).slice(0, 0 + 2).value();return res;
}([testData, maxid]);
};
testCase.selectByMaxWithLimit2 = function (maxid) {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return item['id'] <= params[1];
}).map(function (item) {
return _extends({}, item);
}).orderBy(['type', 'count', 'id'], ['asc', 'desc', 'asc']).slice(2).value();return res;
}([testData, maxid]);
};
testCase.selectByInCountryListAndNotInTypeList = function (ctyList, typeList) {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return _.includes(params[1], item['country']) && !_.includes(params[2], item['type']);
}).map(function (item) {
return _extends({}, item);
}).value();return res;
}([testData, ctyList, typeList]);
};
testCase.selectByInStaticTypeList = function () {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return _.includes(['A', 'C'], item['type']);
}).map(function (item) {
return _extends({}, item);
}).value();return res;
}([testData]);
};
testCase.selectFieldWithExpressionByType = function (type) {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return item['type'] == params[1];
}).map(function (item) {
return { 'ID': item['id'], 'ID2': item['id'] + 1, 'ID3': item['id'] / 3, 'address': item['city'] + '@' + item['country'], 'field_4': testCase.formatter.formatMoney(item['count'], item) };
}).value();return res;
}([testData, type]);
};
testCase.selectAggByTypeAndCountry = function (minId) {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return item['id'] > params[1];
}).groupBy(function (item) {
return "" + "_" + item['type'] + "_" + item['country'];
}).reduce(function (result, value, key) {
var item = value[0];var aggitem = { 'type': item['type'], 'country': item['country'], 'min': function () {
var min = Number.MAX_VALUE;value.forEach(function (item) {
min = Math.min(min, item['count']);
});return min;
}(), 'max': function () {
var max = Number.MIN_VALUE;value.forEach(function (item) {
max = Math.max(max, item['count']);
});return max;
}(), 'total': value.length, 'sum': function () {
var sum = 0;value.forEach(function (item) {
sum += item['count'];
});return sum;
}() };result.push(aggitem);return result;
}, []).orderBy(['country', 'type'], ['asc', 'asc']).value();return res;
}([testData, minId]);
};
testCase.selectAvgAndCustomReduce = function () {
return function (params) {
var source = params[0];var res = _.chain(source).filter(function (item) {
return item['id'] <= 4;
}).groupBy(function (item) {
return "" + "_" + item['type'];
}).reduce(function (result, value, key) {
var item = value[0];var aggitem = { 'type': item['type'], 'ids': testCase.customReduce(item['id'], result, value, key), 'avg': function () {
var sum = 0;value.forEach(function (item) {
sum += item['count'];
});return sum / value.length;
}(), 'count': value.length };result.push(aggitem);return result;
}, []).orderBy(['type'], ['asc']).value();return res;
}([testData]);
};
module.exports = testCase;