-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
416 lines (370 loc) · 12.9 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
var debug = require('debug')('nxhero');
var in_array = require('in_array');
var extend = require("extend");
module.exports = {
/** Get an array of the unique values of property 'column'
* of the objects.
* Values true, false, null will be converted to
* 'true, 'false', 'null'
*
* @param objects
* @param column
* @param options
* @returns {Array}
*/
getColumnUnique: function(objects, column, options) {
vals = {};
let result = [];
for (var i = 0; i < objects.length; ++i) {
vals[objects[i][column]] = true;
}
for (val in vals) {
if (typeof options !== "undefined" && options.type === "int") {
var int = parseInt(val);
if (!isNaN(int) || options.ignoreNaN !== true)
result.push(parseInt(val));
} else if (typeof options !== "undefined" && options.type === "bool")
result.push(val === "true");
else
result.push(val)
}
return result;
},
/** sort array (using sortfunc if given) and then remove duplicates
* slightly modified from: http://stackoverflow.com/questions/4833651/javascript-array-sort-and-unique
* @param arr
* @param sortfunc
* @returns {*}
*/
sortUnique: function(arr, sortfunc) {
if (arr.length === 0) return arr;
if (sortfunc) {
if (typeof sortfunc === "string") {
arr = arr.sort(this.getComparisonFuncFromString(sortfunc))
} else
arr = arr.sort(sortfunc)
} else
arr = arr.sort();
let result = [arr[0]];
for (var i = 1; i < arr.length; i++) {
if (arr[i-1] !== arr[i]) {
result.push(arr[i]);
}
}
return result;
},
/** Get an object with the unique values of property 'column'
* of the objects as keys and the count of these values as value
* Values true, false, null will be converted to
* 'true, 'false', 'null'
*
* @param objects
* @param column
* @param options
* @returns {Array}
*/
getColumnUniqueCounts: function(objects, column, options) {
let vals = {};
let result = [];
for (var i = 0; i < objects.length; ++i) {
if (typeof vals[objects[i][column]] === "undefined")
vals[objects[i][column]] = 1;
else
++vals[objects[i][column]];
}
return vals;
},
/**
* Count how often elements appear in an array
* Copied form @SheetJS's answer on https://stackoverflow.com/questions/19395257/how-to-count-duplicate-value-in-an-array-in-javascript
* @param array
* @param options
*/
countElements: function(arr, options) {
let counts = {};
arr.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });
return counts;
},
/** Get the array of values of property 'column'
* of the objects
*
* @param objects
* @param column
* @param options
* @returns {Array}
*/
getColumn: function(objects, column, options) {
let result = [];
for (var i = 0; i < objects.length; ++i)
result.push(objects[i][column]);
return result;
},
/** Resolve comparison function shortcuts */
getComparisonFuncFromString(str) {
var compare;
switch (str) {
case "min":
compare = function(a,b) {
return a < b
};
break;
case "max":
compare = function(a,b) {
return a > b
};
break;
default:
throw new Error("Unknown comparator string " + compare);
}
return compare;
},
/** Searches an object and by comparing adjacent objects
* with the comparator and keeping the first one if the comparison is true
* and the second one if the comparison is false
* Handy to find key of max/min value in object (see test for example usage)
* @param object
* @param compare
* @param options
*/
findByComparison: function(object, compare, options) {
var first = true;
var prop = null;
var val = null;
options = options || {};
if (typeof compare === "string")
compare = this.getComparisonFuncFromString(compare);
for (var curProp in object) {
var curVal = object[curProp];
if (first || compare(curVal, val)) {
val = curVal;
prop = curProp;
}
first = false;
}
if (options.returnValue === true)
return val;
else if (options.returnBoth === true)
return {value: val, property: prop}
else
return prop;
},
/** Builds an object (key-value pairs) from an array of objects.
*
* @param objects
* @param from
* @param to
*/
map: function(objects, from, to, options ) {
let result = {};
options = options || {};
for (var i = 0; i < objects.length; ++i) {
if (typeof options.filter === "function") {
if (!options.filter(objects[i]))
continue;
}
if (typeof to === "undefined")
result[objects[i][from]] = objects[i];
else
result[objects[i][from]] = objects[i][to];
}
return result;
},
/** Builds an array of objects containing the
* remapped properties of objects
*
*
* @param objects either an array of objects or an object containing objects
* @param fromProperties array of names of properties in the input objects
* if objects is a object of objects, elements of fromProperties can be set to true
* Then, the value of the object key will be used
* if objects is an array of objects, elements of from fromProperties can be set to true
* then, the full original object will be assigned to the corresponding fromProperty
* @param toProperties array of names of properties in the new returned
*/
reMap: function(objects, fromProperties, toProperties ) {
if (fromProperties.length !== toProperties.length)
throw new Error("Expect fromProperties and toProperties to have the same length");
var result = [];
if (Array.isArray(objects)) {
for (var i = 0; i < objects.length; ++i) {
newObject = {};
for (var j = 0; j < fromProperties.length; ++j) {
if (fromProperties[j] === true)
newObject[toProperties[j]] = objects[i];
else
newObject[toProperties[j]] = objects[i][fromProperties[j]];
}
result.push(newObject);
}
} else {
for (var i in objects) {
newObject = {};
for (var j = 0; j < fromProperties.length; ++j) {
if (fromProperties[j] === true) {
newObject[toProperties[j]] = objects[i][fromProperties[j]];
} else {
newObject[toProperties[j]] = objects[i][fromProperties[j]];
}
}
result.push(newObject);
}
}
return result;
},
/**
* Creates an array object with properties from a an array
* where there will be as many objects in the array as the array
* had entries, and each of those objects will have properites properties
* with the corresponding array value as value
* Example:
* objectify([1,2,3], [a,b])
* will return
* [{a: 1, b:1}, {a:2, b:2}, {a:3, b:3}]
*
* @param arr
* @param properties
*/
objectify: function(arr, properties, options) {
var result = [];
for (var i = 0; i < arr.length; ++i) {
var obj = {};
for (var j = 0; j < properties.length; ++j) {
obj[properties[j]] = arr[i];
}
result.push(obj);
}
return result;
},
/** Builds an array of objects from a map
*
* Example Input: {k1: v1, k2: v2}
* Output: of unMap(objects, "prop1", "prop2"):
* [{prop1: k1, prop2: v1}, {prop1: k2, prop2: v2}]
*
* @param objects
* @param keyProperty
* @param valueProperty
* @returns {Array}
*
*/
unMap: function(objects, keyProperty, valueProperty ) {
var result = [];
for (var i in objects) {
var newObject = {};
newObject[keyProperty] = i;
newObject[valueProperty] = objects[i];
result.push(newObject);
}
return result;
},
/** Makes an ''object-tree'' from an array of
* objects, using the specified properties
*
* @param array objects
* @param array properties
*/
treeify: function(objects, properties) {
var result = [];
for (var i = 0; i < objects.length; i++) {
var obj = objects[i];
var leaf = result;
/* Create tree structure for all but the last element of properties */
for (var j = 0; j < properties.length - 1; j++) {
var prop = properties[j];
var propVal = obj[prop];
if (typeof leaf[propVal] === "undefined")
leaf[propVal] = {};
leaf = leaf[propVal];
}
/* Insert object in the lowest level of the tree */
var prop = properties.slice(-1)[0];
var propVal = obj[prop];
if (typeof leaf[propVal] === "undefined")
leaf[propVal] = [obj];
else
leaf[propVal].push(obj);
}
return result;
},
/** ''Flattens'' an object tree:
* Returns an array with one entry for each leaf of
* the tree. Each entry is an object containing the leaf
* as well as tehe keys leading to that leaf
*
* @param array tree
* @param array properties, array of property names or depth
* of tree
*/
flattenTree: function(tree, properties) {
var result = [];
var depth = typeof properties === "number" ? properties : properties.length;
var keys = [];
var keysObj = {};
var propName = typeof properties === "number" ? null : properties[0];
var keys = Object.keys(tree);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
var keyObj = {};
if (propName !== null)
keyObj[propName] = key;
var subtree = tree[key];
var subtreeProperties;
if (typeof properties === "number")
subtreeProperties = properties -1;
else
subtreeProperties = properties.slice(1);
if (depth === 1) {
var subtree = {
keys: [key],
leaf: subtree,
};
if (propName !== null)
subtree.keysObj = keyObj;
result.push(subtree);
} else {
/* Handle tree with real subtree */
/* - Get subtree recursively */
var flatSubTree = module.exports.flattenTree(subtree, subtreeProperties);
/* Amend keys and keysObj in subtree */
for (var i = 0; i < flatSubTree.length; i++) {
var subTreeObject = flatSubTree[i];
subTreeObject.keys = [key].concat(subTreeObject.keys);
if (propName !== null)
subTreeObject.keysObj[propName] = key;
}
result = result.concat(flatSubTree);
}
}
return result;
},
stringToBool: function(arr) {
var result = [];
for (var i = 0; i < arr.length; ++i) {
result.push(arr[i] === "true");
}
return result;
},
containsAll: function(needles, haystack) {
foundNeedles = {};
for (var i = 0; i < haystack.length; ++i) {
if (in_array(haystack[i], needles))
foundNeedles[haystack[i]] = true;
}
return (Object.keys(foundNeedles).length === needles.length)
},
getColumnList: function(objects, column, userOptions) {
var options = {
seperator: ", "
};
extend(options, userOptions);
var values = module.exports.getColumn(objects, column);
if (typeof options.limit !== "undefined") {
if (values.length > options.limit)
return values.length;
}
if (typeof options.sort !== "undefined") {
values.sort();
}
return values.join(options.seperator);
},
}