-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (26 loc) · 855 Bytes
/
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
const getDepth = require("get-depth");
function count(data, options) {
const debug = (options && options.debug) || false;
// get the depth or number of dimensions
const depth = (options && options.depth) || getDepth(data);
if (debug) console.log("depth:", depth);
const counts = (options && options.counts) || {};
const currentDepth = (options && options.currentDepth) || 1;
if (debug) console.log("currentDepth:", currentDepth);
if (currentDepth === depth) {
for (let i = 0; i < data.length; i++) {
const value = data[i];
if (counts[value] === undefined) {
counts[value] = 1;
} else {
counts[value]++;
}
}
} else {
for (let i = 0; i < data.length; i++) {
count(data[i], { counts, currentDepth: currentDepth + 1, depth });
}
}
return counts;
}
module.exports = count;