videojs-vast-vpaid
Version:
VAST plugin to use with video.js
339 lines (275 loc) • 8.16 kB
JavaScript
const NODE_TYPE_ELEMENT = 1;
const SNAKE_CASE_REGEXP = /[A-Z]/g;
const EMAIL_REGEXP = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/i;
const ISO8086_REGEXP = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
function noop () { }
function isNull (o) {
return o === null;
}
function isDefined (o) {
return o !== undefined;
}
function isUndefined (o) {
return o === undefined;
}
function isObject (obj) {
return typeof obj === 'object';
}
function isFunction (str) {
return typeof str === 'function';
}
function isNumber (num) {
return typeof num === 'number';
}
function isWindow (obj) {
return utilities.isObject(obj) && obj.window === obj;
}
function isArray (array) {
return Object.prototype.toString.call(array) === '[object Array]';
}
function isArrayLike (obj) {
if (obj === null || utilities.isWindow(obj) || utilities.isFunction(obj) || utilities.isUndefined(obj)) {
return false;
}
const length = obj.length;
if (obj.nodeType === NODE_TYPE_ELEMENT && length) {
return true;
}
return utilities.isString(obj) || utilities.isArray(obj) || length === 0 ||
typeof length === 'number' && length > 0 && length - 1 in obj;
}
function isString (str) {
return typeof str === 'string';
}
function isEmptyString (str) {
return utilities.isString(str) && str.length === 0;
}
function isNotEmptyString (str) {
return utilities.isString(str) && str.length !== 0;
}
function arrayLikeObjToArray (args) {
return Array.prototype.slice.call(args);
}
function forEach (obj, iterator, context) {
let key, length;
if (obj) {
if (isFunction(obj)) {
for (key in obj) {
// Need to check if hasOwnProperty exists,
// as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function
if (key !== 'prototype' && key !== 'length' && key !== 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) {
iterator.call(context, obj[key], key, obj);
}
}
} else if (isArray(obj)) {
const isPrimitive = typeof obj !== 'object';
for (key = 0, length = obj.length; key < length; key++) {
if (isPrimitive || key in obj) {
iterator.call(context, obj[key], key, obj);
}
}
} else if (obj.forEach && obj.forEach !== forEach) {
obj.forEach(iterator, context, obj);
} else {
for (key in obj) {
if (obj.hasOwnProperty(key)) {
iterator.call(context, obj[key], key, obj);
}
}
}
}
return obj;
}
function snake_case (name, separator) {
separator = separator || '_';
return name.replace(SNAKE_CASE_REGEXP, (letter, pos) => {
return (pos ? separator : '') + letter.toLowerCase();
});
}
function isValidEmail (email) {
if (!utilities.isString(email)) {
return false;
}
return EMAIL_REGEXP.test(email.trim());
}
function extend (obj) {
let arg, i, k;
for (i = 1; i < arguments.length; i++) {
arg = arguments[i];
for (k in arg) {
if (arg.hasOwnProperty(k)) {
if (isObject(obj[k]) && !isNull(obj[k]) && isObject(arg[k])) {
obj[k] = extend({}, obj[k], arg[k]);
} else {
obj[k] = arg[k];
}
}
}
}
return obj;
}
function capitalize (s) {
return s.charAt(0).toUpperCase() + s.slice(1);
}
function decapitalize (s) {
return s.charAt(0).toLowerCase() + s.slice(1);
}
/**
* This method works the same way array.prototype.map works but if the transformer returns undefine, then
* it won't be added to the transformed Array.
*/
function transformArray (array, transformer) {
const transformedArray = [];
array.forEach((item, index) => {
const transformedItem = transformer(item, index);
if (utilities.isDefined(transformedItem)) {
transformedArray.push(transformedItem);
}
});
return transformedArray;
}
function toFixedDigits (num, digits) {
let formattedNum = String(num);
digits = utilities.isNumber(digits) ? digits : 0;
num = utilities.isNumber(num) ? num : parseInt(num, 10);
if (utilities.isNumber(num) && !isNaN(num)) {
formattedNum = String(num);
while (formattedNum.length < digits) {
formattedNum = '0' + formattedNum;
}
return formattedNum;
}
return String(NaN);
}
function throttle (callback, delay) {
let previousCall = new Date().getTime() - (delay + 1);
return function () {
const time = new Date().getTime();
if (time - previousCall >= delay) {
previousCall = time;
callback.apply(this, arguments);
}
};
}
function debounce (callback, wait) {
let timeoutId;
return function () {
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(function () {
callback.apply(this, arguments);
timeoutId = undefined;
}, wait);
};
}
// a function designed to blow up the stack in a naive way
// but it is ok for videoJs children components
function treeSearch (root, getChildren, found) {
const children = getChildren(root);
for (let i = 0; i < children.length; i++) {
if (found(children[i])) {
return children[i];
}
else {
const el = treeSearch(children[i], getChildren, found);
if (el) {
return el;
}
}
}
}
function echoFn (val) {
return function () {
return val;
};
}
// Note: Supported formats come from http://www.w3.org/TR/NOTE-datetime
// and the iso8601 regex comes from http://www.pelagodesign.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
function isISO8601 (value) {
if (utilities.isNumber(value)) {
value = String(value); // we make sure that we are working with strings
}
if (!utilities.isString(value)) {
return false;
}
return ISO8086_REGEXP.test(value.trim());
}
/**
* Checks if the Browser is IE9 and below
* @returns {boolean}
*/
function isOldIE () {
const version = utilities.getInternetExplorerVersion(navigator);
if (version === -1) {
return false;
}
return version < 10;
}
/**
* Returns the version of Internet Explorer or a -1 (indicating the use of another browser).
* Source: https://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx
* @returns {number} the version of Internet Explorer or a -1 (indicating the use of another browser).
*/
function getInternetExplorerVersion (navigator) {
let rv = -1;
if (navigator.appName === 'Microsoft Internet Explorer') {
const ua = navigator.userAgent;
const re = new RegExp('MSIE ([0-9]{1,}[.0-9]{0,})');
const res = re.exec(ua);
if (res !== null) {
rv = parseFloat(res[1]);
}
}
return rv;
}
/** * Mobile Utility functions ***/
function isIDevice () {
return /iP(hone|ad)/.test(utilities.UA);
}
function isMobile () {
return /iP(hone|ad|od)|Android|Windows Phone/.test(utilities.UA);
}
function isIPhone () {
return /iP(hone|od)/.test(utilities.UA);
}
function isAndroid () {
return /Android/.test(utilities.UA);
}
const utilities = {
UA: navigator.userAgent,
noop: noop,
isNull: isNull,
isDefined: isDefined,
isUndefined: isUndefined,
isObject: isObject,
isFunction: isFunction,
isNumber: isNumber,
isWindow: isWindow,
isArray: isArray,
isArrayLike: isArrayLike,
isString: isString,
isEmptyString: isEmptyString,
isNotEmptyString: isNotEmptyString,
arrayLikeObjToArray: arrayLikeObjToArray,
forEach: forEach,
snake_case: snake_case,
isValidEmail: isValidEmail,
extend: extend,
capitalize: capitalize,
decapitalize: decapitalize,
transformArray: transformArray,
toFixedDigits: toFixedDigits,
throttle: throttle,
debounce: debounce,
treeSearch: treeSearch,
echoFn: echoFn,
isISO8601: isISO8601,
isOldIE: isOldIE,
getInternetExplorerVersion: getInternetExplorerVersion,
isIDevice: isIDevice,
isMobile: isMobile,
isIPhone: isIPhone,
isAndroid: isAndroid
};
module.exports = utilities;