-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
201 lines (166 loc) · 5.24 KB
/
index.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
'use strict';
function flat2DList(list, getList) {
if (typeof getList == 'undefined') {
getList = (obj) => obj;
}
let arr2D = list.map((obj, i)=> getList(obj, i));
return arr2D.reduce((prev, current)=> prev.concat(current), []);
}
//return 0 ~ keysSize -1
//NoteForME: Using bitwise OR 0 to floor a number is faster, but 32-bit signed integers only
//http://stackoverflow.com/questions/7487977/using-bitwise-or-0-to-floor-a-number
function randPosInt(listSize) {
return listSize > 0 ? Math.floor(Math.random() * listSize) : -1;
}
function randIndex(listOrObj, ignoreFunctions) {
let keys = Object.keys(listOrObj);
if (typeof ignoreFunctions === 'undefined') {
ignoreFunctions = true;
}
if (ignoreFunctions) {
keys = keys.filter(key=> typeof listOrObj[key] != 'function');
}
return keys[randPosInt(keys.length)];
}
function randPick(listOrObj, ignoreFunctions) {
return listOrObj[randIndex(listOrObj, ignoreFunctions)];
}
function isOk(value) {
return !((typeof value === 'undefined') || value === null);
}
function getValue(defaultValue) {
let valuePath = Array.prototype.slice.call(arguments, 1);
let ret = defaultValue;
if (valuePath.length === 0) {
return ret;
}
function getLastValue(lastValue, funcOrAttr) {
if (typeof funcOrAttr === 'function') {
return funcOrAttr(lastValue);
}else {
return lastValue[funcOrAttr];
}
}
let lastValue = valuePath[0];
if (isOk(lastValue) && valuePath.length === 1) {
ret = lastValue;
}
for (let i = 1; i < valuePath.length; i++) {
if (!isOk(lastValue) || !isOk(getLastValue(lastValue, valuePath[i]))) {
break;
} else if (i == valuePath.length - 1) {
ret = getLastValue(lastValue, valuePath[i]);
} else {
//ok and not the last argument
lastValue = getLastValue(lastValue, valuePath[i]);
}
}
return ret;
}
function getNewId(length, characters) {
var text = '';
const defaultCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var possible = characters ? characters : defaultCharacters;
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
function objectMap(obj, mapFunc, ignoreFunctions) {
if (typeof ignoreFunctions == 'undefined') {
ignoreFunctions = true;
}
let keys = Object.keys(obj);
if (ignoreFunctions) {
keys = keys.filter(key=> typeof obj[key] != 'function');
}
let ret = {};
keys.forEach(key=> {
ret[key] = mapFunc(obj[key]);
});
return ret;
}
function objectReduce(obj, reduceFunc, defaultValue, ignoreFunctions) {
if (typeof ignoreFunctions === 'undefined') {
ignoreFunctions = true;
}
let keys = Object.keys(obj);
if (ignoreFunctions) {
keys = keys.filter(key=> typeof obj[key] !== 'function');
}
return keys.reduce((prev, key, index)=> {
if (index === 0 && typeof defaultValue === 'undefined') {
return obj[keys[0]];
} else {
return reduceFunc(prev, obj[key]);
}
}, defaultValue);
}
function reduceByKey(listOrObj, getKeyFunc, ignoreFunctions, reduceFunc, defaultValue) {
let obj = {};
if (typeof ignoreFunctions === 'undefined') {
ignoreFunctions = true;
}
if (typeof getKeyFunc == 'undefined') {
getKeyFunc = function (key) { return key;};
}
for (let i in listOrObj) {
if (ignoreFunctions && typeof listOrObj[i] == 'function') {
continue;
}
let key = getKeyFunc(listOrObj[i]);
if (typeof defaultValue !== 'undefined' && defaultValue !== null) {
obj[key] = pickOne(obj[key], defaultValue, [0, '']);
}
obj[key] = reduceFunc(obj[key], listOrObj[i]);
}
return obj;
}
function groupByKey(listOrObj, getKeyFunc, ignoreFunctions) {
function reduceFunc(lastResult, oriObj) {
lastResult = pickOne(lastResult, []);
lastResult.push(oriObj);
return lastResult;
}
return reduceByKey(listOrObj, getKeyFunc, ignoreFunctions, reduceFunc);
}
function countByKey(listOrObj, getKeyFunc, ignoreFunctions) {
function reduceFunc(lastResult, oriObj) {
return increment(lastResult, 1);
}
return reduceByKey(listOrObj, getKeyFunc, ignoreFunctions, reduceFunc);
}
function getUniqueKeys(listOrObj, getKeyFunc, ignoreFunctions) {
let obj = countByKey(listOrObj, getKeyFunc, ignoreFunctions);
return Object.keys(obj);
}
function increment(value, diff) {
diff = pickOne(diff, 1, [0, '']);
return value ? value + diff : diff;
}
function pickOne(value, otherwise, whiteList, blackList) {
if (whiteList && whiteList.indexOf(value) != -1) {
return value;
}else if (blackList && blackList.indexOf(value) != -1) {
return otherwise;
}else {
return value ? value : otherwise;
}
}
module.exports = {
randPosInt,
randIndex,
randPick,
isOk,
getValue,
groupByKey,
reduceByKey,
objectMap,
objectReduce,
pickOne,
getNewId,
countByKey,
getUniqueKeys,
increment,
flat2DList,
};