Skip to content

Instantly share code, notes, and snippets.

@MadLittleMods
Last active September 22, 2015 18:08
Show Gist options
  • Save MadLittleMods/f71b0ef905832b8c16c9 to your computer and use it in GitHub Desktop.
Save MadLittleMods/f71b0ef905832b8c16c9 to your computer and use it in GitHub Desktop.
// Inspired by bling.js: https://gist.github.com/paulirish/12fb951a8b893a454b32
// But we needed full module encapsulation
// This will concat anything including array-like things(like NodeLists or HTMLCollections)
let concat = function(...args) {
return args.reduce((result, item) => {
// If array-like
if(
item && item.length !== undefined && !Array.isArray(item) &&
// The window object acts as an array of the iframes in the document (undesired effects for our use cases)
(!window || (window && !(item instanceof window.constructor)))
) {
item = Array.prototype.slice.call(item);
}
return result.concat(item);
}, []);
};
// Pass in a selector string, dom node, or array of dom nodes
export function coerceIntoElementsArray(...args) {
let elements = [];
if(typeof args[0] === 'string') {
elements = concat(document.querySelectorAll.call(document, ...args));
}
else {
elements = concat(...args);
}
return elements;
}
// `arrayLike` can be a single object, array, or array-like (NodeList, HTMLCollection)
export function forEach(arrayLike, cb) {
concat(arrayLike).forEach((...args) => {
if(cb) {
cb(...args);
}
});
}
// Listen to events.
// Pass in a string name of events separated by spaces
export function on(elements, names, cb) {
names.split(/\s/).forEach((name) => {
forEach(elements, (element) => {
element.addEventListener(name, cb);
});
});
// Keep the chaining going
return this;
}
// Remove the event listener
// Pass in a string name of events separated by spaces
export function off(elements, names, cb) {
names.split(/\s/).forEach((name) => {
forEach(elements, (element) => {
element.removeEventListener(name, cb);
});
});
// Keep the chaining going
return this;
}
let $ = function(...args) {
return coerceIntoElementsArray(...args);
};
export default $;
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.domUtility=e():t.domUtility=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e){"use strict";function n(){for(var t=[],e=arguments.length,n=Array(e),r=0;e>r;r++)n[r]=arguments[r];if("string"==typeof n[0]){var o;t=(o=document.querySelectorAll).call.apply(o,[document].concat(n))}else t=c.apply(void 0,n);return t}function r(t,e){c(t).forEach(function(){e&&e.apply(void 0,arguments)})}function o(t,e,n){return e.split(/\s/).forEach(function(e){r(t,function(t){t.addEventListener(e,n)})}),this}function i(t,e,n){return e.split(/\s/).forEach(function(e){r(t,function(t){t.removeEventListener(e,n)})}),this}Object.defineProperty(e,"__esModule",{value:!0}),e.coerceIntoElementsArray=n,e.forEach=r,e.on=o,e.off=i;var c=function(){for(var t=arguments.length,e=Array(t),n=0;t>n;n++)e[n]=arguments[n];return e.reduce(function(t,e){return!e||void 0===e.length||Array.isArray(e)||window&&(!window||e instanceof window.constructor)||(e=Array.prototype.slice.call(e)),t.concat(e)},[])},u=function(){return n.apply(void 0,arguments)};e["default"]=u}])});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment