-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-service.js
66 lines (58 loc) · 1.88 KB
/
data-service.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
63
64
65
66
const fs = require('fs');
const MOCK_DATA = [
JSON.parse(fs.readFileSync('./mock-data/11YVCHAR001.json')),
JSON.parse(fs.readFileSync('./mock-data/11YVCHAR002.json')),
JSON.parse(fs.readFileSync('./mock-data/15MPPN002-VK.json')),
];
module.exports = {
buildBreakdownData,
buildBreakdownDataForYearandVariety,
MOCK_DATA,
getWine,
};
function buildBreakdownData(type, lotCode) {
const filteredData = {};
const result = [];
const dataSet = getWine(lotCode).components;
const uniqueDataset = dataSet
.map((data) => data[type])
.filter((val, idx, self) => self.indexOf(val) === idx);
uniqueDataset.map((uData) => {
filteredData[uData] = dataSet.filter((data) => data[type] === uData);
result.push({
[type]: uData,
percentage: filteredData[uData].reduce((s, b) => s + b.percentage, 0),
});
});
return result.sort((a, b) => (a.percentage > b.percentage ? -1 : 1));
}
function buildBreakdownDataForYearandVariety(lotCode) {
const marks = [];
const filteredData = {};
const uniqueDataset = [];
const dataSet = getWine(lotCode).components;
const result = [];
dataSet.map((data, i) => {
const el = `${data.year}-${data.variety}`;
if (!marks[el]) {
marks[el] = true;
uniqueDataset.push(el);
}
});
uniqueDataset.map((uData) => {
const uDataYear = +uData.split('-')[0];
const uDataVariety = uData.split('-')[1];
filteredData[uData] = dataSet.filter(
(data) => data.year === uDataYear && data.variety === uDataVariety
);
result.push({
year: uDataYear,
variety: uDataVariety,
percentage: filteredData[uData].reduce((s, b) => s + b.percentage, 0),
});
});
return result.sort((a, b) => (a.percentage > b.percentage ? -1 : 1));
}
function getWine(lotCode) {
return MOCK_DATA.find((data) => data.lotCode === lotCode);
}