Skip to content

Instantly share code, notes, and snippets.

View OscarGodson's full-sized avatar
:shipit:
Workin'

Oscar Godson OscarGodson

:shipit:
Workin'
  • CTO
  • Portland, OR
  • 11:21 (UTC -07:00)
View GitHub Profile
@OscarGodson
OscarGodson / connect_mongohq_node.js
Created September 11, 2011 04:25
How to connect to MongoHQ with Node + node-mongodb-native
/**
* To get all the info to login, sign into your MongoHQ account, go to the db you want,
* click the "Database Info" tab, then look for the line that looks like:
* -------------------------------------------------------------
* mongodb://<user>:<password>@staff.mongohq.com:10056/node-test
* ---------| |-| |------------------| |-| |
* USER PASSWORD PORT DB NAME
*
* ALSO, for testing, you should manually add a document and collection into MongoHQ
* from their "Add a Collection" > "Add a Document" links, then below we'll log it.
@OscarGodson
OscarGodson / multiple-expressions-ternary.js
Created September 12, 2011 18:29
Multiple expressions in the ternary operator
//NOTE: don't use semicolon inside a ternary, use a comma like so:
x == 1 //If x == 1
? alert(x) //Alert 1
:( //Else...
alert('Error'), //alert an error
x = 1, //Change x to 1
alert('All Fixed!') //Alert the user again, say it's fixed
);
var list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
list = list.sort(function(){
return Math.random() - 0.5;
});
alert(list);
@OscarGodson
OscarGodson / parseUrl.js
Created September 16, 2011 05:42
Parses a URL and returns a JS obj of key->value pairs with the URL params
var parseUrl = function(url){
var q = {}
, r = new RegExp("([^?=&]+)(=([^&]*))?", "g");
url.replace(r,function($0, $1, $2, $3){ q[$1] = $3; });
return q;
}
/**
* var example = parseUrl('http://google.com?foo=bar&hello=world');
*
@OscarGodson
OscarGodson / bacon.js
Created September 16, 2011 18:52
How to eat bacon with JavaScript
var bacon = new Bacon('hickory');
bacon.cook(function(cookedBacon){
cookedBacon.nom();
});
@OscarGodson
OscarGodson / fakejax.js
Created September 21, 2011 19:03
A quick and dirty way to prototype AJAX
/**
* fakejax is a simple way to prototype AJAX. It will give you a random response time of 0-3 seconds
* and it will also give you a 1 in 10 chance of returning an error
* @param {String} dummyCall This is the fake path like "/api/users"
* @param {Object} returnThis This is what you want it to return if it's successful like {data:'hi'}
* @param {Function} callback This is the callback to your AJAX after it completes
* @example http://jsbin.com/oguyoz/5
*/
var fakejax = function(dummyCall,returnThis,callback){
dummyCall = dummyCall || '';
/*
Major subtleties ahead: Most hash schemes depend on having a "good" hash
function, in the sense of simulating randomness. Python doesn't: its most
important hash functions (for strings and ints) are very regular in common
cases:
>>> map(hash, (0, 1, 2, 3))
[0, 1, 2, 3]
>>> map(hash, ("namea", "nameb", "namec", "named"))
[-1658398457, -1658398460, -1658398459, -1658398462]

#Why I Don't Use CoffeeScript

CoffeeScript in of itself, and not as a transpiled language, is slick. The problem for me is what it's supposedly "fixing".

##Syntax Sugar

For a Rails and Python developer JS is ugly (as I hear). You have... dun dun dun, curly brackets and white space doesn't mean anything (oh the humanity!). But, I like my whitespace. I like not being restricted on how I write my code aesthetically. Not to mention CoffeeScript is by far less human readable than JavaScript. Programming languages, IMO, need to be a good balance of both human and machine readable. Simple example:

cube = (x) -&gt; square(x) * x
@OscarGodson
OscarGodson / gist:1359958
Created November 12, 2011 02:52
Generates a base HTML project then opens it in sublime
function genproj {
mkdir $1
cd $1
mkdir js
mkdir css
mkdir images
touch index.html
echo -e "<!DOCTYPE html>\n<html>\n <head>\n <title>$2</title>\n </head>\n <body>\n \n </body>\n</html>" >> index.html
subl .
}
/**
* Converts an input type to another specified because you aren't allowed for security
* reasons to change the type via JS attribute APIs.
* @param {string} type The type you want to convert it to
* @returns {object} A jQuery object of the new selected inputs it was replaced with
*/
$.fn.convertInputType = function(type){
return this.map(function(i, elem){
var clone = $( this ).clone().attr('type',type);
$(this).replaceWith(clone);