/
location-header.ts
68 lines (60 loc) · 1.8 KB
/
location-header.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
import { csvLocationHeader } from './csv-header'
import neatCsv from 'neat-csv'
import { parseCsvColumnList } from './utils'
export const createLocationHeaderXml = async (data) => {
const parsedData = (await neatCsv(data, {
// headers: csvLocationHeader,
// skipLines: 3
mapHeaders: ({ index }) => csvLocationHeader[index] || null,
skipLines: 2
})) as any
const vocabArrayKeyList = [
'geofencePolygon_polygonPoint_seq',
'geofencePolygon_polygonPoint_value'
]
const vocabElementListItems = parsedData
.map(
(
{
id,
...optionalAttributeMap
},
index
) => {
if (!id) return ''
const optionalAttributeItems = Object.entries(optionalAttributeMap)
.filter(([k, v]) => !!v && !vocabArrayKeyList.includes(k))
.map(
([k, v]) =>
`<attribute id="urn:epcglobal:cbv:mda#${k}">${v}</attribute>`
)
.join('\n')
const polygonItemsXml = parseCsvColumnList({
csvData: parsedData,
index,
indexKey: 'id',
itemKeyList: vocabArrayKeyList
})
.map(
(d) =>
`<polygonPoint seq="${d.geofencePolygon_polygonPoint_seq}">${d.geofencePolygon_polygonPoint_value}</polygonPoint>`
)
.join('\n')
.trim()
const geofanceXml = !!polygonItemsXml
? `<attribute id="urn:epcglobal:cbv:tr#geofencePolygon">${polygonItemsXml}</attribute>`
: ''
return `<VocabularyElement id="${id}">
${optionalAttributeItems}
${geofanceXml}
</VocabularyElement>`
}
)
.join('\n')
.trim()
return `<Vocabulary type="urn:epcglobal:epcis:vtype:Location">
<VocabularyElementList>
${vocabElementListItems}
</VocabularyElementList>
</Vocabulary>`
}