This project is archived and is in readonly mode.

#1205 ✓resolved
ronin-142779 (at lighthouseapp)

Object.subset() [probably] bug and fix

Reported by ronin-142779 (at lighthouseapp) | March 25th, 2011 @ 06:30 AM | in 1.3.2

Object.subset() implementation:

subset: function(object, keys){
    var results = {};
    for (var i = 0, l = keys.length; i < l; i++){
        var k = keys[i];
        results[k] = object[k];
    }
    return results;
},

It should be:

subset: function(object, keys){
    var results = {};
    for (var i = 0, l = keys.length; i < l; i++){
        var k = keys[i];
        if (object[k] != undefined)
            results[k] = object[k];
    }
    return results;
},

Note the if line before results[k] = object[k];

Otherwise, results might contain undefined keys (but it exists).
Sorry for poor English, I'm not a native English speaker,
I'm a programmer so let's talk by code example (taken from Object.merge document) for this explanation:

var obj1 = {a: 0, b: 1};
var obj2 = {c: 2, d: 3};
var obj3 = {a: 4, d: 5};
var merged = Object.merge(obj1, obj2, obj3);
alert(JSON.encode(merged));

This work as expected, but alter above code to use Object.subset():

var obj1 = {a: 0, b: 1};
var obj2 = {c: 2, d: 3};
var obj3 = {a: 4, d: 5};
var merged = Object.merge(obj1, obj2, Object.subset(obj3, ['a', 'b', 'c', 'd']));
alert(JSON.encode(merged));

The result should be same as above, but it is not.
In Firefox, you get {"a": 4, "d": 5}.
But in IE7, you get {"a": 4, "b": null, "c": null, "d": 5}.
/ Nastier, as expected from IE, huh -_- /

Hope this help.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

People watching this ticket

Referenced by

Pages