-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvastParser.js
73 lines (57 loc) · 2.1 KB
/
vastParser.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
import xmlParser from './xmlParser'
function mustBeArray (item) {
if (
item === undefined ||
// Because Object.keys(new Date()).length === 0 we have to do some additional check
(typeof item === 'object' && Object.keys(item).length === 0 && item.constructor === Object) ||
item === null ||
(typeof item === 'number' && item !== 0 && !item)
) {
return []
}
return Array.isArray(item) ? item : [item]
}
function ensureArrays (vastInnerObject) {
if (vastInnerObject.Creatives) {
vastInnerObject.Creatives.Creative = mustBeArray(vastInnerObject.Creatives.Creative)
}
if (vastInnerObject.Extensions) {
vastInnerObject.Extensions.Extension = mustBeArray(vastInnerObject.Extensions.Extension)
}
vastInnerObject.Impression = mustBeArray(vastInnerObject.Impression)
}
function ensureArraysOnCreatives (vastInnerObject) {
var creative = vastInnerObject.Creatives.Creative
var nonLinearCreatives = creative.filter(function (item) { return item.NonLinearAds && item.NonLinearAds.NonLinear })
var linearCreatives = creative.filter(function (item) { return item.Linear })
nonLinearCreatives.forEach(function (creative) {
creative.NonLinearAds.NonLinear.NonLinearClickTracking = mustBeArray(creative.NonLinearAds.NonLinear.NonLinearClickTracking)
})
linearCreatives.forEach(function (creative) {
if (creative.Linear.MediaFiles) {
creative.Linear.MediaFiles.MediaFile = mustBeArray(creative.Linear.MediaFiles.MediaFile)
}
})
}
function parse (xmlDocument) {
var parsedXml = xmlParser.parse(xmlDocument)
if (!parsedXml.VAST || !parsedXml.VAST.Ad) {
return parsedXml
}
if (Array.isArray(parsedXml.VAST.Ad)) {
parsedXml.VAST.Ad = parsedXml.VAST.Ad[0]
}
if (parsedXml.VAST.Ad.Wrapper) {
ensureArrays(parsedXml.VAST.Ad.Wrapper)
if (parsedXml.VAST.Ad.Wrapper.Creatives) {
ensureArraysOnCreatives(parsedXml.VAST.Ad.Wrapper)
}
} else if (parsedXml.VAST.Ad.InLine) {
ensureArrays(parsedXml.VAST.Ad.InLine)
ensureArraysOnCreatives(parsedXml.VAST.Ad.InLine)
}
return parsedXml
}
export default {
parse: parse
}