Skip to content

Instantly share code, notes, and snippets.

@stewartduffy
Forked from cferdinandi/umd-script-boilerplate.js
Last active August 29, 2015 14:16
Show Gist options
  • Save stewartduffy/95e54aa2929702b81021 to your computer and use it in GitHub Desktop.
Save stewartduffy/95e54aa2929702b81021 to your computer and use it in GitHub Desktop.
/**
*
* Name v0.0.1
* Description, by Chris Ferdinandi.
* http://gomakethings.com
*
* Free to use under the MIT License.
* http://gomakethings.com/mit/
*
*/
(function (root, factory) {
if ( typeof define === 'function' && define.amd ) {
define('MyPlugin', factory(root));
} else if ( typeof exports === 'object' ) {
module.exports = factory(root);
} else {
root.MyPlugin = factory(root);
}
})(window || this, function (root) {
'use strict';
//
// Variables
//
var exports = {}; // Object for public APIs
var supports = !!document.querySelector && !!root.addEventListener; // Feature test
var settings; // Plugin settings
// Default settings
var defaults = {
someVar: 123,
callbackBefore: function () {},
callbackAfter: function () {}
};
//
// Methods
//
/**
* A simple forEach() implementation for Arrays, Objects and NodeLists
* @private
* @param {Array|Object|NodeList} collection Collection of items to iterate
* @param {Function} callback Callback function for each iteration
* @param {Array|Object|NodeList} scope Object/NodeList/Array that forEach is iterating over (aka `this`)
*/
var forEach = function (collection, callback, scope) {
if (Object.prototype.toString.call(collection) === '[object Object]') {
for (var prop in collection) {
if (Object.prototype.hasOwnProperty.call(collection, prop)) {
callback.call(scope, collection[prop], prop, collection);
}
}
} else {
for (var i = 0, len = collection.length; i < len; i++) {
callback.call(scope, collection[i], i, collection);
}
}
};
/**
* Merge defaults with user options
* @private
* @param {Object} defaults Default settings
* @param {Object} options User options
* @returns {Object} Merged values of defaults and options
*/
var extend = function ( defaults, options ) {
var extended = {};
forEach(defaults, function (value, prop) {
extended[prop] = defaults[prop];
});
forEach(options, function (value, prop) {
extended[prop] = options[prop];
});
return extended;
};
/**
* Convert data-options attribute into an object of key/value pairs
* @private
* @param {String} options Link-specific options as a data attribute string
* @returns {Object}
*/
var getDataOptions = function ( options ) {
return !options || !(typeof JSON === 'object' && typeof JSON.parse === 'function') ? {} : JSON.parse( options );
};
// @todo Do something...
/**
* Destroy the current initialization.
* @public
*/
exports.destroy = function () {
// @todo Undo init...
};
/**
* Initialize Plugin
* @public
* @param {Object} options User settings
*/
exports.init = function ( options ) {
// feature test
if ( !supports ) return;
// @todo Do something...
};
//
// Public APIs
//
return exports;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment