Skip to content

Instantly share code, notes, and snippets.

@datchley
datchley / interface-pattern-idea.js
Created January 31, 2011 18:51
An implementation idea for JavaScript interfaces
/**
* @class Interface
* @constructor
* Allows the application of the interface design pattern, by defining
* an interface as a name and set of methods. The methods are given a name
* and a number of arguments. Not type information.
*
* The methods are passed as an array, with one object per method. Each
* method is defined as follows in the methods array:
*
@datchley
datchley / JSM.js
Created February 13, 2011 02:11
A Javascript Module facility, similar to Perl's Modules/Exporter (and JSAN)
// Copyright @2011, David Atchley (david.m.atchley@gmail.com)
/**
* @fileOverview
* <p>
* JSM stands for JavaScript Module, and is based on Perl's module and exporter
* facilities. Combines a number of those ideas from Perl and from the existing
* JSAN facility, though with some slight changes.
* </p>
*
@datchley
datchley / jsongrid.html
Created May 4, 2011 15:23
ExtJS 4.0 Json Grid
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<!-- Stylesheets to handle CSS -->
<link rel="stylesheet" type="text/css" href="js/extjs/resources/css/ext-all.css" />
<!-- Javascript for the applications -->
<!-- <script type="text/javascript" src="js/extjs/ext-all-debug.js"></script> -->
<script type="text/javascript" src="js/extjs/bootstrap.js"></script>
<script type="text/javascript">
@datchley
datchley / gist:6215614
Created August 12, 2013 21:48
Remove duplicates from an array of objects in javascript using a function generator.
var data = [{
author: "Stephen King",
title: "It"
}, {
author: "Anne Rice",
title: "Exit to Eden"
}, {
author: "Stephen King",
title: "It"
}, {
@datchley
datchley / gist:6234265
Last active July 1, 2019 02:29
Git pre-commit hook to make sure I don't leave console/debug statements in my code.
#!/bin/sh
#
# @file pre-commit
# @author David Atchley, <david.atchley@answers.com>
# @date Wed Dec 18 14:02:39 CST 2013
#
# Pre Commit checks for various syntax and cleaning prio
# to committing. Exits with non-zero to stop commit
#
#----------------------------------------------------------------------
@datchley
datchley / vimrc
Created August 28, 2013 18:23 — forked from heygarrett/vimrc
" Set colorscheme to solarized
colorscheme solarized
" Change the Solarized background to dark or light depending upon the time of
" day (5 refers to 5AM and 17 to 5PM). Change the background only if it is not
" already set to the value we want.
function! SetSolarizedBackground()
if strftime("%H") >= 5 && strftime("%H") < 17
if &background != 'light'
set background=light
@datchley
datchley / mixins.js
Last active December 23, 2015 11:01
Inspired by functional mixins in frameworks like Twitter's Flight, I thought I'd work up a version that handled both functional and object based mixins, and with variable arguments instead of having to pass in an array of mixins to add.
var mixin = function(target /*, mixins */) {
var args = [].slice.call(arguments, 1);
target.mixins = target.hasOwnProperty('mixins') ? target.mixins : [];
args.forEach(function(mixin) {
if (target.mixins.indexOf(mixin) === -1) {
if (typeof mixin === 'function') {
// Functional mixin using 'call'
mixin.call(target);
}
@datchley
datchley / set-iframe-size-onload.js
Created October 2, 2013 13:06
An attempt to set each iframe's width/height on the page to the width/height of it's contained document.
$('document').ready(function() {
$('iframe').load(function() {
'use strict';
var self = this;
setTimeout(function() {
var iframe = self,
w = $(iframe).width(), h = $(iframe).height(),
docw = iframe.contentWindow.document.width,
doch = iframe.contentWindow.document.height;
@datchley
datchley / iframe-overlay.js
Created October 2, 2013 13:33
For each iframe on a page, build an overlay on top of it that captures click events and sends them to the document in the iframe. This is to allow mobile scrolling in webkit, since it seems touching the iframe when swiping won't bubble up the touch event to the main page and hence won't scroll the page. This only works if you control the content…
$(document).ready(function() {
"use strict";
var ident = 0; // generate unique element id
// Find all iframe DFP ads
$('iframe').each(function() {
var width = $(this).width(),
height = $(this).height(),
@datchley
datchley / data-utils.js
Last active December 25, 2015 12:59
The ability to do a deep copy of objects and arrays in javascript, handling Dates, RegEx and others correctly; along with a deep equals for comparing javascript objects. Both functions handle native types as well as objects and arrays; and the deep equals function should treat arrays with the same values; but not in the same order as equal as we…
if (!Array.isArray) {
Array.isArray = function(a) { return Object.prototype.toString.call(a) == '[object Array]'; }
}
if (!Object.isObject) {
Object.isObject = function(a) { return Object.prototype.toString.call(a) == '[object Object]'; }
}
// Clone an object or native type in javascript
// Exceptions (doesn't handle these types):