-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransform.js
More file actions
40 lines (32 loc) · 1.03 KB
/
transform.js
File metadata and controls
40 lines (32 loc) · 1.03 KB
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
// libs
import groupBy from 'lodash/groupBy'
export default data => {
// group by tags
let output = groupBy(data, 'tag')
const isLabelsKey = key => key.indexOf(':fields') > -1
// extract labels
const labels = Object.keys(output)
.reduce((labels, key) => {
if ( !isLabelsKey(key) ) {
labels[key] = output[`${key}:fields`][0].data.parsed
}
return labels
}, {})
const toObjectArray = (labels, values) => values.map(value =>
value.data.parsed.reduce((nextObject, fieldValue, index) => {
nextObject[labels[index]] = fieldValue
return nextObject
}, {})
)
// remove labels from output
// and
// convert output items from arrays to objects
output = Object.keys(output)
.reduce((nextOutput, key) => {
if ( !isLabelsKey(key) ) {
nextOutput[key] = toObjectArray(labels[key], output[key])
}
return nextOutput
}, {})
return output
}