Skip to content

Instantly share code, notes, and snippets.

View draeton's full-sized avatar

Matthew Kimball-Cobbs draeton

View GitHub Profile
@draeton
draeton / nan.js
Created December 7, 2011 21:17
NaN and implicit type conversion
console.log(NaN == true); // false
console.log(NaN == false); // false
@draeton
draeton / printf.js
Created December 5, 2011 04:47
printf
// courtesy of http://www.reddit.com/user/itsnotlupus @
// http://bit.ly/upcWxw
function printf(msg) {
var args = Array.prototype.slice.call(arguments,1), arg;
return msg.replace(/(%[disv])/g, function(a,val) {
arg = args.shift();
if (arg !== undefined) {
switch(val.charCodeAt(1)){
case 100: return +arg; // d
case 105: return Math.round(+arg); // i
@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;
};
}
@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 / 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 / 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 / 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 / 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 / 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 / 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,