Last active
May 27, 2016 11:39
-
-
Save vincentorback/c9b0c9631532310c8aa2 to your computer and use it in GitHub Desktop.
Lots of small javascript helpers that I tend to reuse in projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* isUndefined | |
* Check if object is undefined | |
* @param {Anything} value | |
* @return {Boolean} Returns `true` if `value` is `undefined`, else `false`. | |
*/ | |
export function isUndefined(value) { | |
return value === undefined; | |
} | |
/** | |
* getViewport | |
* Return viewport with and height | |
*/ | |
export function getViewport() { | |
return { | |
width: Math.max(document.documentElement.clientWidth, window.innerWidth || 0), | |
height: Math.max(document.documentElement.clientHeight, window.innerHeight || 0) | |
}; | |
} | |
/** | |
* debounce | |
* Delay execution of functions | |
*/ | |
export function debounce(fn, wait) { | |
let timeout; | |
return function () { | |
clearTimeout(timeout); | |
timeout = setTimeout(() => fn.apply(this, arguments), (wait || 1)); | |
}; | |
} | |
/** | |
* svgRefresh | |
* Fixes a webkit bug with svg elements using <use> | |
*/ | |
export function svgRefresh () { | |
Array.from(document.querySelectorAll('use')) | |
.forEach(e => e.setAttribute('xlink:href', e.getAttribute('xlink:href'))); | |
} | |
/** | |
* Check for https | |
* @return { Boolean } | |
*/ | |
export function isSecure() { | |
return window.location.protocol === 'https:'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* requestAnimationFrame | |
* Polyfill for better animations and timed events. | |
*/ | |
export function requestFrame () { | |
return window.requestAnimationFrame || | |
window.webkitRequestAnimationFrame || | |
window.mozRequestAnimationFrame; | |
} | |
/** | |
* Check if element matches selector | |
* @param {Element} el | |
* @param {String} selector | |
* @return {Boolean} | |
*/ | |
export function matches(el, selector) { | |
const p = Element.prototype; | |
const f = | |
p.matches || | |
p.matchesSelector || | |
p.mozMatchesSelector || | |
p.msMatchesSelector || | |
p.webkitMatchesSelector || | |
p.oMatchesSelector || | |
function(s) { | |
return [].indexOf.call((document || el.ownerDocument).querySelectorAll(s), this) !== -1; | |
}; | |
return f.call(el, selector); | |
} | |
/** | |
* Get element data attributes in an object | |
* @param {Element} el | |
* @return {Object} dataObject | |
*/ | |
export function getElData(el) { | |
if (el.dataset) { | |
return JSON.parse(JSON.stringify(el.dataset)); | |
} | |
const dataObject = {}; | |
const dataRegEx = /^data\-(.+)$/; | |
Array.from(el.attributes).forEach(attribute => { | |
if (dataRegEx.test(attribute.nodeName)) { | |
let key = attribute.nodeName.match(dataRegEx)[1]; | |
dataObject[key] = attribute.nodeValue; | |
} | |
}); | |
return dataObject; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment