Give meaningful names to Deferred/Promise variables

When using a Deferred or a Promise, don’t just call the variables “deferred” and “promise”. Take this simple example:

var promise = $.ajax(...);

promise.done(function (data) {
    // Our fetching is done

This is an improvement:

var fetching = $.ajax(...);

fetching.done(function (data) {
    // Our fetching is done

Try to use nouns or verbs that describe what the Deferred/Promise is actually “promising”. In the above example, it is the promise of the completion of a data fetch. That way your code reads more like natural language: “When the fetching is done …”.

Syntactic sugar: RexExp.exec with callbacks

The RegExp.exec method returns an array of results when there’s a match or null when there’s none. I often find myself coding the same check over and over again:

var result = /(.+)=(.+)/.exec("name=value");

if (result) {
    console.log(result[1] + " = " + result[2]);

It would make life easier if exec accepted a callback method as the second argument (and then a scope as the third):

/(.+)=(.+)/.exec("name=value", function (str, name, value) {
    console.log(name + " = " + value);

The added benefit of this is, instead of accessing the results via error-prone indices, we get to access them with named arguments inside the callback.

Here’s an extension to the core exec function that adds callback support while simultaneously retaining the original calling mechanism:

RegExp.prototype.exec = (function (exec) {
    return function (str, callback, scope) {
        var results =, str);
        if (arguments.length > 1 && results) {
            callback.apply(scope, results);
        } else {
            return results;

Ruby on Rails on Windows with Pain

I decided to give RoR another try (my first attempt ended with my attention deficit overcoming my desire to learn). Things didn’t work “out of the box” as I hoped.

First, I downloaded and installed Ruby 1.8.7. I was surprised to see that the installer didn’t add the Ruby bin directory to the PATH environment variable, so I had to do it myself.

Next, I downloaded and installed RubyGems 1.3.7.

My first attempt and installing Rails (gem install rails) failed with a bunch of errors (which I wasn’t diligent enough to capture for this post). Google yielded a discussion thread with people seeing the same errors. The workaround that was suggested was to uninstall Rails (gem uninstall rails), clean uninstalled gems (gem cleanup) and install Rails again. This worked. I don’t understand why it didn’t work with a pristine Ruby installation in the first place.

Then I tried creating a new application with rails new anotherShot. I then tried rails server to run the newly created application. It didn’t work. I was told to run bundle install and then try again.

This time, I got an error saying that the SQLite DLL was missing. Makes sense, but a heads up would have been nice. I went over the SQLite downloads and got the DLL. I copied the DLL to the Ruby bin directory. This time, rails server worked.

I now have yet another “Hello World” RoR application waiting for my attention. I hope this time around I will manage to write my first RoR application!

Parsing ISO 8601 UTC dates

Here’s a concise implementation for parsing UTC date strings in ISO 8601 format into Date objects in the local time zone.

function parseIsoDate(s) {
    var tokens = /(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)\.(\d+)/
    tokens[1] -= 1;
    return new Date(Date.UTC.apply(null, tokens));

The new Date(Date.UTC.apply(null, tokens)) trick allows the direct usage of an array of date components for the construction of a new Date instance.

Catch those anchors with hash && (hash = hash)

The following cryptic bit of JavaScript is actual code that I’ve used as a workaround to a very specific issue. The issue I was having was with anchor links not pointing to the right place on the page.

Read More

Enabling Disqus comments on a custom Tumblr theme using jQuery

The official “Universal Code” that is suggested by Disqus for embedding the comment widget injects a new <script> element into the page and looks a bit messy. The embedding can be achieved with cleaner code using jQuery's JSONP support.

Read More

When semicolons are NOT optional in JavaScript

An inadvertently skipped semicolon recently left me scratching my head over a runtime error.

Read More

Parsing query string parameters into a collection

Here’s a utility function for parsing query parameters:

function getQueryParams(qs) {
    qs = qs.split("+").join(" ");
    var params = {};
    var tokens, re = /[?&]?([^=]+)=([^&]*)/g;

    while (tokens = re.exec(qs)) {
            = decodeURIComponent(tokens[2]);

    return params;

//var query = getQueryParams(;

A nice things is that it’s decoupled from

Turkish letter escape bookmarklet for dotSUB

I’ve created a bookmarklet to temporarily solve a very specific problem at hand: I’m translating a TED video into Turkish, but I don’t currently have a Turkish keyboard hooked up to my computer (it’s in a box somewhere in the basement).

Read More

Creating Todoist items with voice using reQall

Todoist is an awesome, minimalistic task management service. The voice-to-web service reQall, which is a free alternative to Jott, also does a fairly good job at task management. However, I wanted to stick to Todoist and bring voice input capabilities to it. I used reQall’s e-mail forwarding along with Todoist’s HTTP API and my awesome hosting provider's powerful features to create a quick mashup.

Read More