Skip to content

Commit 25b358e

Browse files
authored
Merge pull request #23 from nbonfils/default-thresholds
Set default thresholds for the filters
2 parents 43fbdb7 + c01ee4c commit 25b358e

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/lib/processing.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,38 @@ export const getFilters = (data) => {
9696
return acc;
9797
}, {lowerBounds: [], counts: [0]});
9898

99-
// Initialize de value of the filters
100-
filters[field].value = field == 'refs' ? filters[field].counts.length - 2 : 0;
99+
// Initialize the value of the filters
100+
let threshold = 0;
101+
switch (field) {
102+
case 'refs':
103+
threshold = 5000;
104+
break;
105+
case 'concepts':
106+
threshold = 200;
107+
break;
108+
case 'works':
109+
case 'authors':
110+
case 'institutions':
111+
case 'sources':
112+
threshold = 50;
113+
break;
114+
case 'countries':
115+
case 'funders':
116+
threshold = 25;
117+
break;
118+
}
119+
120+
// Get the filter value closest to the threshold
121+
let idxAbove = filters[field].counts.findIndex((el) => el >= threshold);
122+
let idxBelow = filters[field].counts.findLastIndex((el) => el < threshold);
123+
let diffAbove = Math.abs(threshold - filters[field].counts[idxAbove]);
124+
let diffBelow = Math.abs(threshold - filters[field].counts[idxBelow]);
125+
filters[field].value = diffAbove < diffBelow ? idxAbove : idxBelow;
126+
127+
if (field === 'refs') {
128+
// We want at least 2 occurences of refs by default
129+
filters[field].value = Math.min(filters[field].value, filters[field].counts.length - 2);
130+
}
101131
});
102132

103133
return filters;

0 commit comments

Comments
 (0)