forked from stopcovid19-okayama/covid19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatConfirmedCases.ts
105 lines (99 loc) · 2.01 KB
/
formatConfirmedCases.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
type DataType = {
attr: '検査実施人数'
value: number
children: [
{
attr: '陽性患者数'
value: number
children: [
{
attr: '入院調整中'
value: number
},
{
attr: '入院中'
value: number
},
{
attr: '宿泊療養'
value: number
},
{
attr: '自宅療養'
value: number
},
{
attr: '調査中'
value: number
},
{
attr: '退院'
value: number
},
{
attr: '死亡'
value: number
}
]
}
]
}
type ConfirmedCasesType = {
検査実施人数: number
陽性者数: number
入院調整中: number
入院中: number
/*
軽症中等症: number
重症: number
宿泊療養: number
自宅療養: number
調査中: number
*/
死亡: number
退院: number
}
interface ChildData {
attr: string
value: number
}
type ChildDataType = {
attr: string
value: number
children?: ChildData[]
}
function getSelectedItem(data: DataType, key: string) {
let result: number | undefined
const recursiveSearch = (data: ChildDataType) => {
if (result) {
return
}
if (data.attr === key) {
result = data.value
} else if (data.children) {
data.children.forEach((child: ChildDataType) => {
if (result) {
return
}
recursiveSearch(child)
})
}
}
recursiveSearch(data)
return result || 0
}
/**
* Format for *Chart component
*
* @param data - Raw data
*/
export default (data: DataType) => {
return {
検査実施人数: getSelectedItem(data, '検査実施人数'),
陽性者数: getSelectedItem(data, '陽性患者数'),
入院中: getSelectedItem(data, '入院中'),
入院調整中: getSelectedItem(data, '入院調整中'),
死亡: getSelectedItem(data, '死亡'),
退院: getSelectedItem(data, '退院')
} as ConfirmedCasesType
}