-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArrayUtil.js
320 lines (318 loc) · 6.34 KB
/
ArrayUtil.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { rnd } from "./rnd.js";
import { deepEqual } from "https://js.sabae.cc/deepEqual.js";
const min = (array, func) => {
let min = Number.MAX_SAFE_INTEGER;
let hit = null;
for (const a of array) {
const n = func(a);
if (n < min) {
min = n;
hit = a;
}
}
return hit;
};
const max = (array, func) => {
let max = Number.MIN_SAFE_INTEGER;
let hit = null;
for (const a of array) {
const n = func(a);
if (n > max) {
max = n;
hit = a;
}
}
return hit;
};
const isUnique = (array) => {
for (let i = 0; i < array.length - 1; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] == array[j]) {
return false;
}
}
}
return true;
};
const toUnique = (ar, ftarget = null) => {
if (!ftarget) {
const set = new Set();
for (const a of ar) {
set.add(a);
}
const res = [];
set.forEach(s => res.push(s));
return res;
} else {
const chk = {};
const res = [];
for (const a of ar) {
const t = ftarget(a);
if (chk[t]) continue;
chk[t] = a;
res.push(a);
}
return res;
}
};
const toDuplicated = (ar, ftarget = null) => {
if (!ftarget) {
const set = new Set();
const resset = new Set();
for (const a of ar) {
if (set.has(a)) {
resset.add(a);
} else {
set.add(a);
}
}
const res = [];
resset.forEach(s => res.push(s));
return res;
} else {
const chk = {};
for (const a of ar) {
const t = ftarget(a);
if (!chk[t]) {
chk[t] = [a];
} else {
chk[t].push(a);
}
}
const res = [];
for (const ar of Object.values(chk)) {
if (ar.length > 1) {
ar.forEach(a => res.push(a));
}
}
return res;
}
};
const toUniqueByString = (ar) => {
const map = {};
for (const a of ar) {
map[JSON.stringify(a)] = a;
}
return Object.values(map);
};
const melt = (array, keep, name, valname) => {
name = name || "varname";
valname = valname || "value";
const res = [];
array.forEach((d) => {
const base = {};
const vars = [];
const vals = [];
for (const k in d) {
if (keep.indexOf(k) >= 0) {
base[k] = d[k];
} else {
vars.push(k);
vals.push(d[k]);
}
}
for (let i = 0; i < vars.length; i++) {
const row = {};
Object.assign(row, base);
row[name] = vars[i];
row[valname] = vals[i];
res.push(row);
}
});
return res;
};
const removeByKeys = (array, keys) => {
array.forEach(d => {
for (const r of keys) {
delete d[r];
}
});
};
const groupBy = (array, key) => {
const map = {};
for (const a of array) {
const v = a[key];
if (map[v]) {
map[v].push(a);
} else {
map[v] = [a];
}
}
return map;
}
const getUnion = (ars) => {
const unions = ars[0].filter((a) => {
for (let i = 1; i < ars.length; i++) {
if (!ars[i].includes(a)) {
return false;
}
}
return true;
});
return unions;
};
const countBy = (ar) => {
const cnt = {};
for (const a of ar) {
if (!cnt[a]) {
cnt[a] = 1;
} else {
cnt[a]++;
}
}
return cnt;
};
const make = (from, to) => {
if (typeof from == "string" && from.length == 1) {
const nfrom = from.charCodeAt(0);
const nto = to.charCodeAt(0);
const res = [];
if (nfrom < nto) {
for (let i = nfrom; i <= to.charCodeAt(0); i++) {
res.push(String.fromCharCode(i));
}
} else {
for (let i = nfrom; i >= to.charCodeAt(0); i--) {
res.push(String.fromCharCode(i));
}
}
return res;
}
if (typeof from == "number") {
const res = [];
if (to > from) {
for (let i = from; i <= to; i++) {
res.push(i);
}
} else {
for (let i = from; i >= to; i--) {
res.push(i);
}
}
return res;
}
throw new Error("not supported params");
};
const mapToObject = async (nameArray, mapFunc) => {
const res = await Promise.all(nameArray.map(mapFunc));
const o = new Object();
for (let i = 0; i < res.length; i++) {
o[nameArray[i]] = res[i];
}
return o;
};
const shuffle = (array) => {
for (let i = 0; i < array.length; i++) {
const n = rnd(array.length);
const tmp = array[i];
array[i] = array[n];
array[n] = tmp;
}
return array;
};
const diff = (before, after) => {
const add = [];
const remove = [];
for (const b of before) {
let hit = false;
for (const a of after) {
if (deepEqual(b, a)) {
hit = true;
break;
}
}
if (!hit) {
remove.push(b);
}
}
for (const a of after) {
let hit = false;
for (const b of before) {
if (deepEqual(a, b)) {
hit = true;
break;
}
}
if (!hit) {
add.push(a);
}
}
return { added: add, removed: remove };
};
const getSorted = (array, name, ascorder = true, csvmode = false) => {
if (name == undefined) {
return array;
}
const getNumber = (s) => {
if (s.length == 0) {
return NaN;
}
let i;
for (i = 0; i < s.length; i++) {
if ("0123456789.,".indexOf(s.charAt(i)) == -1) {
break;
}
}
if (i == 0) {
return NaN;
}
return parseFloat(s.substring(0, i).replace(/,/g, ""));
};
const res = array.map(a => a);
let head = null;
if (csvmode) {
head = res[0];
res.shift();
}
res.sort((a, b) => {
const an = a[name];
const bn = b[name];
if ((an == null || an == "") && bn != null) {
return 1;
} else if ((bn == null || bn == "") && an != null) {
return -1;
}
let flg = 0;
const am = array.indexOf(a);
const bm = array.indexOf(b);
if (an == bn) {
flg = am - bm;
} else {
const ad = getNumber(an);
const bd = getNumber(bn);
if (isNaN(ad) && isNaN(bd) || ad == bd) {
flg = an.toString().localeCompare(bn.toString());
} else if (isNaN(ad)) {
flg = 1;
} else if (isNaN(bd)) {
flg = -1;
} else {
flg = ad > bd ? 1 : -1;
}
}
return flg * (ascorder ? 1 : -1);
});
if (csvmode) {
res.splice(0, 0, head);
}
return res;
}
const ArrayUtil = {
min,
max,
isUnique,
toUnique,
toDuplicated,
toUniqueByString,
melt,
removeByKeys,
groupBy,
getUnion,
countBy,
make,
mapToObject,
shuffle,
diff,
getSorted,
};
export { ArrayUtil }