Skip to content

Instantly share code, notes, and snippets.

View draeton's full-sized avatar

Matthew Kimball-Cobbs draeton

View GitHub Profile
@draeton
draeton / jquery.getUrlVars.js
Last active November 8, 2017 13:18
returns object with url variables
/**
* jQuery.getUrlVars
*
* Copyright 2013, Matthew Cobbs
* MIT Licensed
*/
/*global jQuery */
(function (window, $) {
"use strict";
@draeton
draeton / fibonacci.js
Created August 3, 2011 12:26
Fibonacci generator
(function (window, undefined) {
var Math = window.Math,
a = [0, 1];
if (!Math.fibonacci) {
Math.fibonacci = function (n) {
var i;
if (a[n] === undefined) {
@draeton
draeton / Object.extend.js
Created August 16, 2011 04:14
extend objects or functions from right to left with properties from other objects
(function () {
var toString = Object.prototype.toString,
slice = Array.prototype.slice,
/**
* determines type of passed argument
*/
toType = function (thing) {
return toString.call(thing).match(/\s(\w+)/)[1].toLowerCase();
@draeton
draeton / isValidDate.js
Created August 16, 2011 04:24
Date validation method
/**
* takes an input element and validates its value as a date by converting
* to a date object and then back to a string. Defaults to month-day-year order
*
* @param {HTMLElement} dateField The HTML input element to validate
* @param {Boolean} [isDMY] Is the value in day-month-year order? Optional
* @return {Boolean} is valid
*/
function isValidDate(dateField, isDMY) {
var strDate = dateField.value,
@draeton
draeton / AddandRemoveClasses.js
Created August 16, 2011 04:26
Add and remove classes to elements
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/gi, '');
}
}
function removeClass(el, className) {
var curClass = el.className,
re = new RegExp('\\b' + className + '\\b', 'g');
@draeton
draeton / Function.prototype.tail.js
Created August 20, 2011 00:13
Function.prototype.tail
Function.prototype.tail = function (cb) {
var fn = this;
return function () {
var ret = fn.apply(this, arguments);
return cb ? cb.apply(this, [].concat(ret)): ret;
};
};
@draeton
draeton / framework.js
Created September 3, 2011 07:25
Playing at a simple app framework
(function (window) {
var document = window.document;
var getType = function (thing) {
return Object.prototype.toString.call(thing).match(/\s(\w+)/)[1].toLowerCase();
};
var getIsType = function (type) {
return (function (thing) {
@draeton
draeton / testDice.js
Created September 11, 2011 01:02
Run a number of tests on a set of dice to see what the average number of rolls is required to have them all land on the first side (represented here with a "0"). Returns the average.
(function (window) {
// Return a random integer from 0 to `d`
function r(d) {
return Math.round(Math.random() * --d);
}
// Roll the dice. Returns a string of integers to represent which
// side each die fell on ("0" is the first side).
//
@draeton
draeton / checkImage.example.js
Last active January 1, 2023 03:30
Check the existence of an image file at `url` by creating a temporary Image element.
// Example Use
(function () {
function success() {
console.log("success: ", this.src);
}
function failure() {
console.log("failure: ", this.src);
}
@draeton
draeton / jquery.schema.js
Created September 12, 2011 14:58
jQuery.schema - Ported from dojox.json.schema release 1.6.1 (based on draft-zyp-json-schema-02)
// jQuery.schema
//
// Ported from dojox.json.schema release 1.6.1
// Based on draft-zyp-json-schema-02
// http://tools.ietf.org/html/draft-zyp-json-schema-02
//
(function ($) {
var schema;