Mind Mining Medium

archives
December 18, 2009

Object Type in Javascript

Someone noted the interesting line that I use to find the object type.

function getType(obj){
    if (obj === undefined) { return 'undefined'; }
    if (obj === null) { return 'null'; }
    return Object.prototype.toString.call(obj).split(' ').pop().split(']').shift().toLowerCase();
}

Object.prototype.getType = function(){
    return Object.prototype.toString.call(this).split(' ').pop().split(']').shift().toLowerCase();
};

I posted the QUnit test suite. I’ve tested it in IE6-8, Safari 4, and Firefox 3.5.6. It would be greatly appreciated if you post your results.

While the typical method of using typeof or instanceof may produce results, they have their faults.

Douglas Crockford writes about type detection and his methods work for most cases, but not all.

On a side note, the reason why the Object.prototype.getType doesn’t check for undefined or null is because neither of those have properties, that is, they are not an Object. That said, they cannot call any functions.

December 17, 2009

Pure JavaScript Flatten Array

Update: Paul Irish thought it would be grand to point out that my test case here isn’t the best. It’s good to note that the following functions work for all values in your array. I would suggest checking out the QUnit testing page to see other examples.

While working on my current project I needed a way to turn a heavily nested array and flatten it. There are some nice JavaScript libraries that have functions to do so, such as Underscore’s _.flatten and Prototype’s .flatten to name a few.

However, with reduce being implemented only in JavaScript 1.8 and not wanting to include a whole JavaScript library to do one task, I decided to write my own. You may want to take a look at the QUnit testing page.

Native Function

function flatten(array){
    var flat = [];
    for (var i = 0, l = array.length; i < l; i++){
        var type = Object.prototype.toString.call(array[i]).split(' ').pop().split(']').shift().toLowerCase();
        if (type) { flat = flat.concat(/^(array|collection|arguments|object)$/.test(type) ? flatten(array[i]) : array[i]); }
    }
    return flat;
}

Usage:

var given = [[1, [2, [3, [4, [5, [6, [7, [8, [9, [0]]]]]]]]]]];
var value = flatten(given);

// 'value' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Array Object Extend:

Array.prototype.flatten = function flatten(){
   var flat = [];
   for (var i = 0, l = this.length; i < l; i++){
       var type = Object.prototype.toString.call(this[i]).split(' ').pop().split(']').shift().toLowerCase();
       if (type) { flat = flat.concat(/^(array|collection|arguments|object)$/.test(type) ? flatten.call(this[i]) : this[i]); }
   }
   return flat;
};

Usage:

var given = [[1, [2, [3, [4, [5, [6, [7, [8, [9, [0]]]]]]]]]]];
var value = given.flatten();

// 'value' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
December 12, 2009

lala extremely flawed

A week ago I wrote about how I used web apps to feed into other web apps, more specifically I talked about lala and it’s “lala music mover”.

Recently a thought formed; while watching how lala’s music mover worked, I noticed that it wasn’t checking anything other than the ID3 tag. I ran a real quick experiment.

To test this out, I found a song that I didn’t already have. “Fireflies” by “Owl City” seemed a great fit. I made a clean iTunes library, created a sample mp3 and added it to iTunes. Then, I just edited the ID3 tag to match some basic information about the Fireflies track. Finally I made the lala mover rescan my library. Surprisingly when I checked lala, they had added it to my library.

What does this teach us? The lala mover does nothing but check for ID3 tags. But really, I just saved 10¢. It’s not possible to download the track from lala being that they think I already own the track.