Skip to content

Instantly share code, notes, and snippets.

View erberg-snippets's full-sized avatar

erberg-snippets

View GitHub Profile
@erberg-snippets
erberg-snippets / colorComboAA
Last active January 26, 2016 23:27
Tests if color combo is AA compliant.
var colorContrastUtil = (function () {
/**
* Convert Hex value to RGB.
* @param {String} hex Color representation
* @return {Object} Object with r, g, and b, params.
*/
var hexToRgb = function (hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
@erberg-snippets
erberg-snippets / phoneNumberFilter.js
Created May 2, 2015 20:15
Angular Phone Number Filter
.filter('phoneNumber', function(){
return function(input){
if(input && (input.length === 7)){
return input.substr(0,3) + '-' + input.substr(3,7);
} else if(input.length === 10){
console.log(input);
return '(' + input.substr(0,3) + ') ' + input.substr(3,3) + '-' + input.substr(6,4);
}
return input;
};
if(developer === 'eric' && domain === 'phoenix.edu'){ eric.toggle(); }
/**
* Returns new globally unique identifier.
* @return {String} identifier in form 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
*/
var getGUID = function(){
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
};
@erberg-snippets
erberg-snippets / modulePattern.js
Created July 10, 2014 21:16
Module pattern in javascript.
var myNamespace = (function () {
var myPrivateVar, myPrivateMethod;
// A private counter variable
myPrivateVar = 0;
// A private function which logs any arguments
myPrivateMethod = function( foo ) {
console.log( foo );
/**
* Trim Polyfill for IE8 and lower.
*/
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
@erberg-snippets
erberg-snippets / consoleScrape
Last active August 29, 2015 13:57
consoleScrape.js
/**
* Scrape indicated selectorString element from single or multiple web pages.
* @param {Number} numPages Number of pages to scrape. Depends on RESTful pagination.
* @param {String} urlRoot Base URL.
* @param {String} selectorString jQuery selector string.
* @param {Array} returnArray Array of jQuery objects matching the selectorString.
* @return {Array} returnArray
*/
function consoleScrape(numPages, urlRoot, selectorString, returnArray) {
/**
* Takes a string and returns true if its a valid email address.
* @param {String} email Email address to be tested for validity.
* @return {Boolean} True if valid email, false if not.
*/
function isValidEmail(email) {
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
/**
* Changes the first character of each word to upper case, and any other characters to lowercase.
* @return {String} String formatted as a title.
*/
String.prototype.toTitleCase = function() {
var title = this.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});