Skip to content

Instantly share code, notes, and snippets.

@jeffreymeng
Last active August 14, 2016 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffreymeng/cd218aaf838904afcfcf905e1eaa4512 to your computer and use it in GitHub Desktop.
Save jeffreymeng/cd218aaf838904afcfcf905e1eaa4512 to your computer and use it in GitHub Desktop.
A Template for a javascript library, with some utilities included
/* Copyright (c) 2016 Jeffrey Meng | MIT License
* Partially taken from http://checkman.io/blog/creating-a-javascript-library/ and/or youmightnotneedjquery.com
*/
(function(window) {
'use strict';
function define_Library() {
//UTILS
var throwError = function(txt, type) {
//throws an error
var type = " " + type + " " || " ";
var e = new Error(txt);
e.name = 'Libary' + type + 'Error';
throw e;
}
//get's an object and adds some utils to the wrapper. Sort of like jquery, but a lot fewer functions.(delete it if you want)
//some functions in this utility function is taken from youmightnotneedjquery.com
var /* $ = */ getElement = function(selector) {
var wrapper = document.querySelector(selector);
// get Dom wrapper.
//function to add custom functions to the wrapper.
this.addFunction = function(name, code) {
// if code is undefined, assume name is an object with name:function pairs
if (!code) {
for (var key in name) {
//check is item is not a prototype
if (name.hasOwnProperty(key)) {
var value = name[key];
//set function name in the wrapper to the value(function code)
wrapper[key] = value;
}
}
}
//there is only one function being defined. Set the name to the function(code)
else {
wrapper[name] = code;
}
};
//add utility functions(you can add more functions or delete some to suit what you need for your libary)
this.addFunction({
//returns or modifies the value of an input or textarea field
"val": function(val) {
//if val is undefined, assume we want to read it and return the value
if (val === null) {
return this.value;
}
//else set the value to val
else {
this.value = val;
}
},
//returns the html string of an element.
"html": function(val) {
//if html is undefined, assume we want to read it and return the html
if (!val) {
return this.innerHTML;
}
//else set the value to html
else {
this.innerHTML = val;
}
},
// class functions: adds or removes a class.
"addClass": function(className) {
if (this.classList) this.classList.add(className);
else this.className += ' ' + className;
},
"hasClass": function(className) {
if (this.classList) return this.classList.contains(
className);
else return new RegExp('^| ' + className + ' |$', 'gi').test(
this.className);
},
"removeClass": function(className) {
if (this.classList) {
this.classList.remove(className);
} else {
this.className = this.className.replace(new RegExp(
"(^|\\s+)" + className + "(\\s+|$)"), ' ');
}
},
//event handler
"on": function(event, handler) {
if (this.addEventListener) {
this.addEventListener(event, handler);
} else {
this.attachEvent('on' + event, function() {
handler.call(this);
});
}
},
//hides an element
"hide": function() {
this.style.display = 'none';
},
//shows an element
"show": function() {
this.style.display = 'block';
},
"keyup": function(handler) {
return this.on("keyup", handler);
}
});
return wrapper;
};
// end of UTILS
var Library = {};
Library.about = {
version:1.0,
author:"Your Name",
//etc.
}
Libary.version = Libary.about.version;
Libary.author = Libary.about.author;
Library.myFunction = function() {
//do stuff
}
return Library;
}
//define globally if it doesn't already exist
if (typeof(Library) === 'undefined') {
window.Library = define_Library();
} else {
var e = new Error("The Library was already defined. Please rename all variables with the name 'Library'.");
e.name = 'Library Inclusion Error';
throw e;
}
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment