All files / src utils.js

77.78% Statements 7/9
50% Branches 2/4
100% Functions 0/0
77.78% Lines 7/9
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                                                                                                                6x     6x       3x   3x 3x 3x           3x      
 
export function convertToArray (value) {
	if (typeof value === 'string') {
		value = value.split(' ')
	}
	return value
}
 
/**
 * Add classes to an element.
 * This method checks to ensure that the classes don't already exist before adding them.
 * It uses el.className rather than classList in order to be IE friendly.
 * @param {object} el - The element to add the classes to.
 * @param {classes} string - List of space separated classes to be added to the element.
 */
export function addClasses (el, classes) {
	const newClasses = convertToArray(classes)
	let classList
	if (classList instanceof SVGAnimatedString) {
		classList = Array.from(classList)
	} else {
		classList = convertToArray(el.className)
	}
	newClasses.forEach((newClass) => {
		if (classList.indexOf(newClass) === -1) {
			classList.push(newClass)
		}
	})
	if (el instanceof SVGElement) {
		el.setAttribute('class', classList.join(' '))
	} else {
		el.className = classList.join(' ')
	}
}
 
/**
 * Remove classes from an element.
 * It uses el.className rather than classList in order to be IE friendly.
 * @export
 * @param {any} el The element to remove the classes from.
 * @param {any} classes List of space separated classes to be removed from the element.
 */
export function removeClasses (el, classes) {
	const newClasses = convertToArray(classes)
	let classList
	if (classList instanceof SVGAnimatedString) {
		classList = Array.from(classList)
	} else {
		classList = convertToArray(el.className)
	}
	newClasses.forEach((newClass) => {
		const index = classList.indexOf(newClass)
		if (index !== -1) {
			classList.splice(index, 1)
		}
	})
	Iif (el instanceof SVGElement) {
		el.setAttribute('class', classList.join(' '))
	} else {
		el.className = classList.join(' ')
	}
}
 
export let supportsPassive = false
 
Eif (typeof window !== 'undefined') {
	supportsPassive = false
	try {
		var opts = Object.defineProperty({}, 'passive', {
			get () {
				supportsPassive = true
			},
		})
		window.addEventListener('test', null, opts)
	} catch (e) {}
}