Skip to content

Instantly share code, notes, and snippets.

View draeton's full-sized avatar

Matthew Kimball-Cobbs draeton

View GitHub Profile
'use strict';
var config = require('./config');
var noop = function () {};
var console = {};
var methods = 'debug error info log warn dir dirxml table trace assert count markTimeline profile profileEnd time timeEnd timeStamp timeline timelineEnd group groupCollapsed groupEnd clear';
@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 / 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;
@draeton
draeton / istype.js
Created September 15, 2011 03:55
isType methods
var o = {};
function getIsType(type) {
return function (o) {
return /* undefined */ typeof o === type ||
/* null */ o === null && type === "null" ||
Object.prototype.toString.call(o).match(/\s+(\w+)/)[1].toLowerCase() === type;
};
}