This repository was archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.js
More file actions
136 lines (127 loc) · 4.17 KB
/
data.js
File metadata and controls
136 lines (127 loc) · 4.17 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import cli from 'better-console'
import _ from 'lodash'
import _fp from 'lodash/fp'
import humps from 'lodash-humps'
import sanitizeHtml from 'sanitize-html'
import {
addAuthors, doTitleize, fixAuthor, isPoster,
rmNoData, sortPresentations, titleId, validPresenations,
} from './src/utils'
export function getAuthId(firstname, lastname, company) {
const coStr = company ? ` ${company.toString().substr(0, 3)}` : ''
return titleId(`${firstname} ${lastname}${coStr}`)
}
export function fixPanelDescription(description) {
const panelPresentationIndex = {}
function addPanelIndex(matches, fieldId, value) {
if (matches) {
const index = parseInt(matches[1], 10)
if (!panelPresentationIndex[index]) {
panelPresentationIndex[index] = { index }
}
panelPresentationIndex[index][fieldId] = value
}
}
_.each(description, (value, key) => {
const isTitle = key.match(/^presentation([1-9][0-9]?)Title$/)
addPanelIndex(isTitle, 'title', value)
const isPresenter = key.match(/^presenterOfPresentation([1-9][0-9]?)/)
addPanelIndex(isPresenter, 'presenter', value)
})
return _fp.values(panelPresentationIndex)
}
function fixDescription(sessionDescription) {
if (!sessionDescription) {
return sessionDescription
}
return sanitizeHtml(sessionDescription, {
allowedTags: ['b', 'i', 'em', 'strong', 'p', 'ul', 'li'],
})
}
const fixPresentation = ({ sessionCode, sessionType }) => ({
orderof, description, authors = [], ...rest
}) => {
const presentation = { ...rest, description: {} }
presentation.authors = authors.map(auth => fixAuthor({
...auth,
presenter: sessionType === 'Preformed Panel' ? undefined : auth.presenter,
}))
if (isPoster(sessionType)) {
const idParts = presentation.id.toString().split('.')
if (idParts.length === 2) {
presentation.id = `${idParts[0]}.${_.padEnd(idParts[1], 3, '0')}`
}
}
// Fix description fields.
if (_.isString(description)) {
presentation.description.text = fixDescription(description)
} else {
_.each(description, (desc) => {
presentation.description[titleId(desc.fieldLabel)] = desc.fieldValue
})
}
if (presentation.description.title) {
presentation.description.title = doTitleize(presentation.description.title)
}
if (presentation.title) {
presentation.title = doTitleize(presentation.title)
}
if (presentation.authors.length > 1) {
presentation.authors = _fp.orderBy(
['isPresenter', 'lastName', 'firstName', 'isChair'], ['desc', 'asc', 'asc'], presentation.authors,
)
}
presentation.key = _fp.kebabCase([sessionCode, rest.id])
return presentation
}
const fixPresentations = session => _fp.flow(
validPresenations,
_fp.map(fixPresentation(session)),
sortPresentations(session),
)
function fixDataItem({
presentations, sessionDescription, sessionChairs,
sessionDate, sessionId, sessionType, ...rest
}, position) {
const newItem = {
sessionChairs: sessionChairs.map(fixAuthor),
sessionDate: sessionDate || 'none',
sessionDescription: fixDescription(sessionDescription),
sessionType: sessionType || 'Opening',
trackId: titleId(rest.trackName || sessionType),
position,
...rest,
}
if (!newItem.sessionCode) {
newItem.sessionCode = sessionId.toString()
newItem.sessionCodeErr = true
} else {
const idParts = newItem.sessionCode.toString().split('.')
if (idParts.length === 3) {
newItem.sessionCode = `${idParts[0]}.${idParts[1]}.${_.padStart(idParts[2], 2, '0')}`
}
}
newItem.presentations = fixPresentations(newItem)(presentations)
return newItem
}
const getItems = _fp.flow(humps, rmNoData, items => _.map(items, fixDataItem))
const getAuthors = _fp.flow(
_fp.reduce(addAuthors, {}),
_fp.values,
_fp.sortBy(['lastSort', 'firstname']),
)
const getUniq = fieldId => _fp.flow(_fp.map(fieldId), _.uniq)
export default function fixData(data) {
let apiData = null
cli.log('fetch new data')
cli.log('transform new data')
const items = getItems(data)
apiData = {
authors: getAuthors(items),
items,
trackIds: getUniq('trackId')(items),
sessionIds: getUniq('sessionType')(items),
}
cli.log('return new data')
return { apiData, data }
}