Skip to content

Commit 2bd1dc5

Browse files
committed
mvp
1 parent 1495fe5 commit 2bd1dc5

File tree

4 files changed

+2451
-0
lines changed

4 files changed

+2451
-0
lines changed

index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const getDepth = require("get-depth");
2+
3+
function count(data, options) {
4+
const debug = (options && options.debug) || false;
5+
// get the depth or number of dimensions
6+
const depth = (options && options.depth) || getDepth(data);
7+
if (debug) console.log("depth:", depth);
8+
const counts = (options && options.counts) || {};
9+
const currentDepth = (options && options.currentDepth) || 1;
10+
if (debug) console.log("currentDepth:", currentDepth);
11+
12+
if (currentDepth === depth) {
13+
for (let i = 0; i < data.length; i++) {
14+
const value = data[i];
15+
if (counts[value] === undefined) {
16+
counts[value] = 1;
17+
} else {
18+
counts[value]++;
19+
}
20+
}
21+
} else {
22+
for (let i = 0; i < data.length; i++) {
23+
count(data[i], { counts, currentDepth: currentDepth + 1, depth });
24+
}
25+
}
26+
return counts;
27+
}
28+
29+
module.exports = count;

0 commit comments

Comments
 (0)