Skip to content

Instantly share code, notes, and snippets.

View TravisMullen's full-sized avatar

Travis Mullen TravisMullen

View GitHub Profile
@TravisMullen
TravisMullen / buildKey.js
Last active February 1, 2016 03:08
build key from name to be used for indexing or clean URLs
function buildKey(name) {
var key = '';
if (name !== undefined) {
key = name;
key = key.replace(/ /g, '-'); // purge spaces
key = key.replace(/&/g, 'and'); // clean ampersand
key = key.replace(/[^a-zA-Z0-9-]/g, ''); // remove all non-alphas and non-number and non-dashes
key = key.toLowerCase();
}
return key;
@TravisMullen
TravisMullen / obj-hash-conditional.js
Last active February 8, 2016 21:42
Matching Object Hash
var alpha = {
'a': {
'random data': {}
},
'b': {
'more random data': {}
}
}, // object hash
beta = {
'a': true,
@TravisMullen
TravisMullen / jQuery.isDefined.js
Last active February 16, 2016 17:45
if jQuery isn't defined, load it
// Only do anything if jQuery isn't defined
if (typeof jQuery == 'undefined') {
if (typeof $ == 'function') {
// warning, global var
thisPageUsingOtherJSLibrary = true;
}
function getScript(url, success) {
@TravisMullen
TravisMullen / document.ready.js
Created February 17, 2016 04:53
jQuery-less Wait for DOM Complete
var waitForDocument = setInterval(function() {
if (document.readyState !== 'complete') return;
// or var WIN = window.open();
// if (WIN.document.readyState !== 'complete') return;
// if (WIN.opener.document.readyState !== 'complete') return;
clearInterval(waitForDocument);
// do your work
}, 100);
@TravisMullen
TravisMullen / tMinus.js
Last active February 17, 2016 05:05
Execute Function on specified time.
/* ============================================= *
queue to fire today ╭∩╮(︶︿︶)╭∩╮
* ============================================= */
function triggerOnTime(cb, hour, minute, pm) {
var config = {
// set to noon as default
@TravisMullen
TravisMullen / fixedContentDirective.js
Last active February 21, 2016 01:45
Fixing Elements to Page
angular.module( 'YOUR_APP.directives' ).directive( 'fixedContent',
[
function() {
'use strict';
return {
restrict: 'A',
scope: {
fixedBelow: '=',
fixedPadding: '='
@TravisMullen
TravisMullen / extend-obj.js
Created March 6, 2016 19:25
Extend an object
var extend = function(out) {
out = out || {};
for (var i = 1; i < arguments.length; i++) {
if (!arguments[i])
continue;
for (var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key))
out[key] = arguments[i][key];
@TravisMullen
TravisMullen / keyCodes.js
Last active April 30, 2016 04:24
Create Keyboard Code (Key to Code) as Object
// http://jsfiddle.net/vWx8V/
// use `keyCodes` for complete object of keyboard codes
'use strict';
var keyCodes = ( function() {
var codes = {
'backspace' : 8,
'tab' : 9,
'enter' : 13,
@TravisMullen
TravisMullen / match-exterior.js
Created July 23, 2016 15:56
Match by Exterior Regex
// Try
/{(.*?)}/
// That means, match any character between { and }, but don't be greedy - match the shortest string which ends with } (the ? stops * being greedy). The parentheses let you extract the matched portion.
// Another way would be
@TravisMullen
TravisMullen / hasRequiredAttributeSpec.js
Last active September 17, 2016 16:42
hasRequiredAttribute asmine Matchers
beforeEach(function() {
jasmine.addMatchers({
//
// actual: data object to check for property
//
// expected: property or property chain as string
//
hasRequiredAttribute: function() {
return {
compare: function(actual, expected) {