-
Notifications
You must be signed in to change notification settings - Fork 0
/
pivot_wider.js
77 lines (63 loc) · 1.6 KB
/
pivot_wider.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
67
68
69
70
71
72
73
74
75
76
77
function pivot_wider(data, [params]) {
const names = {}
const cols = data[0]
const rows = data.slice(1)
// capture the unique values for the names
params.names.forEach(item => {
if (!names[item]) names[item] = []
const pos = cols.indexOf(item)
rows.forEach(row => {
const key = row[pos].toString()
// console.log('key:', key, pos, row)
if (!names[item].includes(key)) names[item].push(key)
})
names[item].sort()
})
const reducedCols = cols.reduce((a, item) => {
if (
!params.names.includes(item) &&
!params.values.includes(item)
) {
a.push(item)
}
return a
}, [])
const newData = {}
const key = params.names[0]
const len = names[key].length
const val = params.values[0]
// console.log('meta:', {
// key,
// len,
// val
// })
rows.forEach(rec => {
const jsonKey = JSON.stringify(
reducedCols.map(col => rec[cols.indexOf(col)])
)
// console.log('jsonKey:', jsonKey)
if (!newData[jsonKey]) {
newData[jsonKey] = Array(len).fill('-')
}
const pivot = rec[cols.indexOf(key)].toString()
const pos = names[key].indexOf(pivot)
// console.log('meta2:', {pivot, pos})
newData[jsonKey][pos] = rec[cols.indexOf(val)]
})
// console.log('newData:', newData)
const result = Object.entries(newData).map(rec => {
const [k, v] = rec
const inflated = [
...(JSON.parse(k)),
...v
]
return inflated
})
// console.log('inflated:', result)
return [
[...reducedCols, ...names[key]],
...result
]
// return data
}
module.exports = pivot_wider