-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (58 loc) · 1.55 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
module.exports = buildIndex
function buildIndex({ data, facets, selectedPath, maxLevel }) {
const index = {}
for (const item of data) {
let tree = buildTree({ obj: item, facets, selectedPath, maxLevel })
merge(index, tree)
}
return index
}
function buildTree({ obj, facets, currentPath = [], selectedPath = [], maxLevel }) {
if (typeof maxLevel === "undefined") {
if (selectedPath.length > 0) {
maxLevel = selectedPath.length
} else {
maxLevel = 1
}
}
const tree = { count: 1 }
for (const f of facets) {
const value = f.map ? f.map(obj) : obj[f.field]
if (currentPath.indexOf(f) == -1) { // Facet not added yet
currentPath.push(f)
let match = currentPath.length <= maxLevel
for (let i = 0; i < currentPath.length && i < selectedPath.length; i++) {
if (selectedPath[i] !== currentPath[i].name) {
match = false
}
}
if (match) {
tree[f.name] = {
[value]: { count: 1 }
}
if (match) {
tree[f.name][value] = buildTree({ obj, facets, currentPath, selectedPath, maxLevel })
}
}
currentPath.pop()
}
}
return tree
}
function merge(destination, source) {
for (const k in source) {
if (typeof source[k] === "object") {
if (!destination[k]) {
destination[k] = {}
}
merge(destination[k], source[k])
}
if (typeof source[k] === "number") {
if (!destination[k]) {
destination[k] = source[k]
} else {
destination[k] += source[k]
}
}
}
}