Skip to content

Instantly share code, notes, and snippets.

View draeton's full-sized avatar

Matthew Kimball-Cobbs draeton

View GitHub Profile
@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 / 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 / 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 / node.js
Created December 9, 2011 03:48
Nodes with children
function Node ( val, parent ) {
this.value = val;
this.setParent( parent );
this.children = [];
}
Node.prototype = {
addChild: function ( val ) {
var node = val instanceof Node ? val : new Node( val );
node.setParent( this );
@draeton
draeton / file-extension-filter.js
Created December 9, 2011 21:58
Filter file names by extension
var list = "me.jpg,me.png,me.txt,me.zip,me.apple".split(",");
var re = /.+\.(?!jpg|jpeg|gif|png)/i;
list.forEach(function (v, i) {
console.log(v, re.test(v));
});
@draeton
draeton / base.js
Created December 22, 2011 05:10
Playing with classes and inheritance
/*
* This has essentially turned into Resig's Simple JavaScript Inheritance
* with some minor modifications for class names and use strict,
* and getters ans setters
* http://ejohn.org/blog/simple-javascript-inheritance/
* http://ejohn.org/blog/javascript-getters-and-setters/
*/
(function (window) {
"use strict";
@draeton
draeton / LICENSE
Created December 28, 2011 06:22
Fibonacci in Python
Copyright (c) 2011, Matthew Cobbs
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
@draeton
draeton / anchor.js
Created January 9, 2012 15:15
Anchor - A URL parsing utility
/**
* Anchor - A URL parsing utility
*
* Copyright 2012, Matthew Cobbs
* MIT licensed
*
* Methods:
*
* getSearchVars - returns a key-value object with the parameters in the URL search
* setSearchVars(o) - sets parameters using a key-value object in the URL search
@draeton
draeton / ngbs.u.anchor.js
Created January 24, 2012 16:53
A URL parsing utility
/**
* ngbs.u.anchor - A URL parsing utility
*/
/*global ngbs */
var ngbs = ngbs || {};
ngbs.u = ngbs.u || {};
ngbs.u.anchor = (function (window, document, location) {
"use strict";
@draeton
draeton / deeplink.js
Created February 15, 2012 16:01
Deeplinking?