Skip to content

Instantly share code, notes, and snippets.

View joshrhoades's full-sized avatar

Josh Rhoades joshrhoades

View GitHub Profile
@joshrhoades
joshrhoades / dateHelpers.js
Last active August 29, 2015 14:07
Helper Date Functions, including determining leap year, and how many days in a month
var isLeapYear = function(year) {
return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
}
, getDaysInMonth = function(month, year) {
return [31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}
;
@joshrhoades
joshrhoades / textContentPolyfill.js
Created February 7, 2014 23:41
IE8 polyfill/shim for supporting textContent (vs innerText) so that textContent is universally accessible. Can be embedded as a standalone script in a `[if lte IE 8]` embed.
if (Object.defineProperty && Object.getOwnPropertyDescriptor && Object.getOwnPropertyDescriptor(Element.prototype, "textContent") && !Object.getOwnPropertyDescriptor(Element.prototype, "textContent").get) {
(function() {
var innerText = Object.getOwnPropertyDescriptor(Element.prototype, "innerText");
Object.defineProperty(Element.prototype, "textContent",
{
get: function() {
return innerText.get.call(this);
},
set: function(s) {
return innerText.set.call(this, s);
@joshrhoades
joshrhoades / formatSeconds.js
Created January 27, 2014 21:30
Quick function to format passed in seconds and determine the weeks, days, hours, minutes, and seconds in that time. Returns an object of the values.
var _formatCount = function(sec) {
return {
w: Math.floor(sec / 86400 / 7),//weeks
d: Math.floor(sec / 86400 % 7),//days
h: Math.floor(sec / 3600 % 24),//hours
m: Math.floor(sec % 3600 / 60),//minutes
s: Math.floor(sec % 3600 % 60)//seconds
};
};
@joshrhoades
joshrhoades / emptySRC.html
Created August 28, 2013 00:22
Set a blank img src for setting a background image since img.src can't be targeted directly. Disables the outline/placeholder of an img element that has no SRC value (which often is a side-effect of styling the parent element of the img with the background-image).
<img src="data:image/png;base64,R0lGODlhFAAUAIAAAP///wAAACH5BAEAAAAALAAAAAAUABQAAAIRhI+py+0Po5y02ouz3rz7rxUAOw==" />
var getFileExtension = function(theFile) {
return theFile.split('.').pop();
};
getFileExtension('myfile.js');//returns 'js'
@joshrhoades
joshrhoades / stripFQDN.js
Last active December 20, 2015 18:39
Remove FQDN from a string
var stripFQDN = function(theURL) {
return theURL.replace(/^.*\/\/[^\/]+/, '');
};
//_stripFQDN('https://gist.github.com/assets/application-f348fb986aecf8d3b65e959e23f6a29f.css');
//returns '/assets/application-f348fb986aecf8d3b65e959e23f6a29f.css'
@joshrhoades
joshrhoades / noCallbackInstantExecute.js
Created June 21, 2013 19:53
Make a dynamically injected function always execute, without the delay of callbacks or the evil of `eval`. Amazingly simple solution that works like a charm. I needed this because I wanted to make dynamically precompiled templates instantly available (in PROD mode they are compiled into a JS file, and injected if a module on the page requires it…
/*
using jQuery AJAX as an example, though we do not leverage jQuery in our Production app.
This technique can be used anywhere with injected data/functionality
*/
$.ajax({
url: theTemplate.filePath,
success: function(data) {
/*
Take the raw data of the file, insert it into a new function, and execute it.
Bypasses need for callbacks, instantly executes it without delay, and does it without using `eval`.
@joshrhoades
joshrhoades / handlebars.getTemplate.js
Last active December 18, 2015 17:28
Function to expose precompiled handlebars as part of the handlebars object to make it easier to use HB in both DEV (runtime compile) and PROD (build/release-time compile)
Handlebars.getTemplate = function(name) {
if (Handlebars.templates === undefined || Handlebars.templates[name] === undefined) {
$.ajax({
url : 'templatesfolder/' + name + '.handlebars',
success : function(data) {
if (Handlebars.templates === undefined) {
Handlebars.templates = {};
}
Handlebars.templates[name] = Handlebars.compile(data);
},
@joshrhoades
joshrhoades / logUncaught.js
Created May 9, 2013 00:28
JS global function to log all uncaught exceptions/errors, with optional method (stub) to fire and log to a server.
var arrErrors = [];
window.onerror = function(msg, fileURL, lineNum) {
arrErrors.push({ msg: msg, file: fileURL, line: lineNum });
};
setInterval(function() {
sendToServer(arrErrors);
arrErrors = [];
}, 5000);
sendToServer(arrErrors) {
@joshrhoades
joshrhoades / prototypeShims.js
Created May 2, 2013 17:28
Common and useful Prototype Shims
/**
* SHIM to add `Date.now` functionality if it is not available, based off of EcmaScript 5.
* The `Date.now()` function returns a `number` value that is the time value designating the UTC date and time of the occurrence of the call to `now`.
* @see {@link http://es5.github.com/#x15.9.4.4}
* @global
* @method
* @name Date.now
* @example var dtNow = Date.now();
* @returns {date} Number value that is the time value designating the UTC data dn time of the time of the call to this
*/