expect(utils.parseSorting('-size')).toEqual({ sortDesc: true, sortValue: 'size' });
});
});
+
+describe('cumulativeMapFacetValues', () => {
+ it('should correctly cumulate facets', () => {
+ expect(
+ utils.cumulativeMapFacetValues([
+ { val: '1', count: 50 },
+ { val: '2', count: 1 },
+ { val: '3', count: 6 },
+ { val: '4', count: 2 },
+ { val: '5', count: 0 }
+ ])
+ ).toEqual({ '1': 50, '2': 9, '3': 8, '4': 2, '5': 0 });
+ });
+ it('should correctly cumulate facets with NO_DATA items', () => {
+ const expectedResult = {
+ '80.0-*': 59,
+ '70.0-80.0': 26,
+ '50.0-70.0': 15,
+ '30.0-50.0': 11,
+ '*-30.0': 7,
+ NO_DATA: 5
+ };
+ expect(
+ utils.cumulativeMapFacetValues([
+ { val: '80.0-*', count: 59 },
+ { val: '70.0-80.0', count: 11 },
+ { val: '50.0-70.0', count: 4 },
+ { val: '30.0-50.0', count: 4 },
+ { val: '*-30.0', count: 7 },
+ { val: 'NO_DATA', count: 5 }
+ ])
+ ).toEqual(expectedResult);
+ expect(
+ utils.cumulativeMapFacetValues([
+ { val: 'NO_DATA', count: 5 },
+ { val: '80.0-*', count: 59 },
+ { val: '70.0-80.0', count: 11 },
+ { val: '50.0-70.0', count: 4 },
+ { val: '30.0-50.0', count: 4 },
+ { val: '*-30.0', count: 7 }
+ ])
+ ).toEqual(expectedResult);
+ expect(
+ utils.cumulativeMapFacetValues([
+ { val: '80.0-*', count: 59 },
+ { val: '70.0-80.0', count: 11 },
+ { val: '50.0-70.0', count: 4 },
+ { val: 'NO_DATA', count: 5 },
+ { val: '30.0-50.0', count: 4 },
+ { val: '*-30.0', count: 7 }
+ ])
+ ).toEqual(expectedResult);
+ });
+});
return map;
}
-function cumulativeMapFacetValues(values: Array<{ val: string; count: number }>) {
+export function cumulativeMapFacetValues(values: Array<{ val: string; count: number }>) {
+ const noDataVal = values.find(value => value.val === 'NO_DATA');
+ const filteredValues = noDataVal ? values.filter(value => value.val !== 'NO_DATA') : values;
+
+ let sum = sumBy(filteredValues, value => value.count);
const map: { [value: string]: number } = {};
- let sum = sumBy(values, value => value.count);
- values.forEach((value, index) => {
+ filteredValues.forEach((value, index) => {
map[value.val] = index > 0 && index < values.length - 1 ? sum : value.count;
sum -= value.count;
});
+
+ if (noDataVal) {
+ map[noDataVal.val] = noDataVal.count;
+ }
return map;
}