UNPKG

video-ad-sdk

Version:

VAST/VPAID SDK that allows video ads to be played on top of any player

52 lines (51 loc) 2.06 kB
const getChildren = (element) => { var _a; return (_a = element.elements) !== null && _a !== void 0 ? _a : []; }; const findChildByName = (element, childName) => getChildren(element).find(({ name = '' }) => name.toUpperCase() === childName.toUpperCase()); const filterChildrenByName = (element, childrenName) => getChildren(element).filter(({ name = '' }) => name.toUpperCase() === childrenName.toUpperCase()); /** * Get the first child element from the passed parsed xml element. * * @param element Parsed xml element object. * @param childName Child element name * @returns The first child element with the passed name or undefined if not found. */ export const get = findChildByName; /** * Get all the children elements of the passed parsed xml element filtered by the passed child name if passed. * * @param element Parsed xml element object. * @param childName Child element name. * @returns Array of child elements or an empty array. */ export const getAll = (element, childName) => { if (typeof childName === 'string') { return filterChildrenByName(element, childName); } return getChildren(element); }; /** * Get the first child element from the passed parsed xml element. * * @returns The first child element or undefined if there are non. */ export const getFirstChild = (element) => getChildren(element)[0]; /** * Get the text value of the passed parsed xml element. * * @returns Text of the element */ export const getText = (element) => { const firstChild = element && getFirstChild(element); return firstChild && firstChild.text; }; /** * Get all the attributes of the passed parsed xml element. * * @returns Object with the element attributes. */ export const getAttributes = (element) => { var _a; return (_a = element.attributes) !== null && _a !== void 0 ? _a : {}; }; /** * Get the attribute with the passed name of the passed parsed xml element. * * @returns Attribute value or undefined. */ export const getAttribute = (element, attributeName) => getAttributes(element)[attributeName];