All files / utils general.js

35.82% Statements 24/67
30.95% Branches 13/42
41.67% Functions 5/12
34.38% Lines 22/64
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 1601x                                                                                     18x 1x     17x             17x 14x         6x   3x 1x     2x 1x     1x 1x 1x 1x           1x 1x 1x   1x 1x                         10x                                                             1x                                                              
import document from 'global/document';
 
/**
 *
 * @param {Object} obj
 * @param {String} name
 * @param {Function} onGet
 * @param {Function} onSet
 */
export function addProperty (obj, name, onGet, onSet) {
 
	// wrapper functions
	let oldValue = obj[name];
	const
		getFn = () => onGet.apply(obj, [oldValue]),
		setFn = (newValue) => {
			oldValue = onSet.apply(obj, [newValue]);
			return oldValue;
		};
 
	// Modern browsers, IE9+ (IE8 only works on DOM objects, not normal JS objects)
	if (Object.defineProperty) {
 
		Object.defineProperty(obj, name, {
			get: getFn,
			set: setFn
		});
 
		// Older Firefox
	} else if (obj.__defineGetter__) {
 
		obj.__defineGetter__(name, getFn);
		obj.__defineSetter__(name, setFn);
	}
}
 
/**
 *
 * @param {String} input
 * @return {string}
 */
export function escapeHTML (input) {
 
	if (typeof input !== 'string') {
		throw new Error('Argument passed must be a string');
	}
 
	const map = {
		'&': '&',
		'<': '&lt;',
		'>': '&gt;',
		'"': '&quot;'
	};
 
	return input.replace(/[&<>"]/g, (c) => {
		return map[c];
	});
}
 
// taken from underscore
export function debounce (func, wait, immediate = false) {
 
	if (typeof func !== 'function') {
		throw new Error('First argument must be a function');
	}
 
	if (typeof wait !== 'number') {
		throw new Error('Second argument must be a numeric value');
	}
 
	let timeout;
	return () => {
		let context = this, args = arguments;
		let later = () => {
			timeout = null;
			if (!immediate) {
				func.apply(context, args);
			}
		};
		let callNow = immediate && !timeout;
		clearTimeout(timeout);
		timeout = setTimeout(later, wait);
 
		Eif (callNow) {
			func.apply(context, args);
		}
	};
}
 
/**
 * Determine if an object contains any elements
 *
 * @see http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
 * @param {Object} instance
 * @return {Boolean}
 */
export function isObjectEmpty (instance) {
	return (Object.getOwnPropertyNames(instance).length <= 0);
}
 
export function splitEvents (events, id) {
	let rwindow = /^((after|before)print|(before)?unload|hashchange|message|o(ff|n)line|page(hide|show)|popstate|resize|storage)\b/;
	// add player ID as an event namespace so it's easier to unbind them all later
	let ret = {d: [], w: []};
	(events || '').split(' ').forEach((k, v) => {
		let eventname = v + '.' + id;
		if (eventname.indexOf('.') === 0) {
			ret.d.push(eventname);
			ret.w.push(eventname);
		}
		else {
			ret[rwindow.test(v) ? 'w' : 'd'].push(eventname);
		}
	});
 
 
	ret.d = ret.d.join(' ');
	ret.w = ret.w.join(' ');
	return ret;
}
 
/**
 *
 * @param {String} className
 * @param {HTMLElement} node
 * @param {String} tag
 * @return {HTMLElement[]}
 */
mejs.getElementsByClassName = (className, node, tag) => {
 
	if (node === undefined || node === null) {
		node = document;
	}
	if (node.getElementsByClassName !== undefined && node.getElementsByClassName !== null) {
		return node.getElementsByClassName(className);
	}
	if (tag === undefined || tag === null) {
		tag = '*';
	}
 
	let
		classElements = [],
		j = 0,
		teststr,
		els = node.getElementsByTagName(tag),
		elsLen = els.length
		;
 
	for (i = 0; i < elsLen; i++) {
		if (els[i].className.indexOf(className) > -1) {
			teststr = `,${els[i].className.split(' ').join(',')},`;
			if (teststr.indexOf(`,${className},`) > -1) {
				classElements[j] = els[i];
				j++;
			}
		}
	}
 
	return classElements;
};