Skip to content

Instantly share code, notes, and snippets.

View draeton's full-sized avatar

Matthew Kimball-Cobbs draeton

View GitHub Profile
@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 / 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).
//
@olivierlacan
olivierlacan / launch_sublime_from_terminal.markdown
Created September 5, 2011 15:50
Launch Sublime Text 2 from the Mac OS X Terminal

Launch Sublime Text 2 from the Mac OS X Terminal

Sublime Text 2 ships with a CLI called subl (why not "sublime", go figure). This utility is hidden in the following folder (assuming you installed Sublime in /Applications like normal folk. If this following line opens Sublime Text for you, then bingo, you're ready.

open /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl

You can find more (official) details about subl here: http://www.sublimetext.com/docs/2/osx_command_line.html

Installation

@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 / 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 / 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";
@desandro
desandro / jquery.imagesloaded.js
Created January 26, 2011 18:01 — forked from paulirish/README.md
$.fn.imagesLoaded jQuery plugin
// $('img.photo',this).imagesLoaded(myFunction)
// execute a callback when all images have loaded.
// needed because .load() doesn't work on cached images
// Modified with a two-pass approach to changing image
// src. First, the proxy imagedata is set, which leads
// to the first callback being triggered, which resets
// imagedata to the original src, which fires the final,
// user defined callback.
// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}