[dojo-contributors] Another RFC - dojo.areEqual

Alex Russell alex at dojotoolkit.org
Fri Sep 19 17:05:51 EDT 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(sorry to find this thread so late)

FWIW, there's already a deep equality function in DOH:

	http://bugs.dojotoolkit.org/browser/util/trunk/doh/runner.js#L597

It's much shorter because it depends on DOH-ported variants of the  
dojo.js dojo.is* functions and a couple of short helpers  
(doh._arrayEq, doh._objPropEa, etc.), so should be simple to port.  
Note that it's recursive and can therefore bomb out on deep equality  
checks of recursive data structures.

That said, I don't think either variant belong in base since they can  
be so very slow (lots and lots of iteration on deep structures).

Regards

On Aug 26, 2008, at 9:47 PM, Adam L. Peller wrote:

> We could probably collapse this code down further -- doesn't ===  
> cover a lot
> of these cases (&&null, undefined) and I think including a == check  
> may make
> this method less precise than some might want. Mixing different  
> notions of
> equality may or may not be a bug, depending on the circumstance.
>
> However, it's hard to think of too many cases where a === check  
> would need
> to be backed up by deep equality checks on other types, outside of  
> unit
> testing.  If you're dealing with an Array or Date, you often know it  
> and
> don't need this whole method.  _FormWidget.compare is actually doing a
> tri-state check for gt/lt/equals, and typically only needs to deal  
> with a
> given type.  The QueryReadStore method does look like a match,  
> though, but I
> think the need there is primarily for deep object comparison  
> (arrays, too)
>
> -Adam
>
> On Tue, Aug 26, 2008 at 8:19 PM, Nathan Toone <toonetown at dojotoolkit.org 
> >wrote:
>
>> I don't know if there is anything like this that exists already  
>> (and this
>> would probably be a 1.3 thing too...).  doh.runner defines a  
>> doh.assertEqual
>> that will take pretty much any two things (dates, objects, native  
>> values,
>> arrays, etc) - and it will tell you if they are "equal" or not  
>> (that is -
>> that their values are equal).
>> This seems to be something that would be useful in dojo as well.   
>> In fact,
>> there are a couple of places that I could see it being really  
>> helpful right
>> off-the-bat - dijit.form._FormWidget's compare() function for  
>> one...and
>> there are places in dojox.data.QueryReadStore that have comments  
>> about not
>> having an "objects equality" function.
>>
>> It shouldn't be too difficult to do...here is a block of code that  
>> I have
>> used that will add this functionality (it probably needs to be  
>> cleaned
>> up/reviewed before adding to dojo).
>>
>> =====================
>> dojo.areEqual = function(item1, item2){
>>    if((item1 === undefined)&&(item2 === undefined)){
>>        return true;
>>    }
>>    if((item1 === item2)||(item1 == item2)){
>>        return true;
>>    }
>>    var arrayEq = function(i1, i2){
>>        var l = i1.length;
>>        if(l != i2.length){ return false; }
>>        for(var x=0; x < l; x++){
>>            if(!dojo.areEqual(i1[x], i2[x])){ return false; }
>>        }
>>        return true;
>>    };
>>    var objEq = function(i1, i2){
>>        if(i1 instanceof Date){
>>            return (i2 instanceof Date && i1.getTime()==i2.getTime());
>>        }
>>        if(i1 === null && i2 === null){
>>            return true;
>>        }else if(i1 === null || i2 === null){
>>            return false;
>>        }
>>        var x;
>>        for(x in i1){
>>            if(!(x in i2)){
>>                return false;
>>            }
>>        }
>>        for(x in i2){
>>            if(!dojo.areEqual(i1[x], i2[x])){
>>                return false;
>>            }
>>        }
>>        return true;
>>        };
>>    if(dojo.isArray(item1) && dojo.isArray(item2) && arrayEq(item1,
>> item2)){
>>        return true;
>>    }
>>    if(typeof item1 == "object" && typeof item2 == "object" &&
>> objEq(item1, item2)){
>>        return true;
>>    }
>>    return false;
>> };
>>
>> =====================
>>
>> Let me know what you guys think.  If this would be helpful, I'll  
>> open a
>> ticket for 1.3.  If not, I'll crawl back under my rock.
>>
>> -Nathan
>>
>> _______________________________________________
>> dojo-contributors mailing list
>> dojo-contributors at dojotoolkit.org
>> http://turtle.dojotoolkit.org/mailman/listinfo/dojo-contributors
>>
>>
> _______________________________________________
> dojo-contributors mailing list
> dojo-contributors at dojotoolkit.org
> http://turtle.dojotoolkit.org/mailman/listinfo/dojo-contributors

- --
Alex Russell
alex at sitepen.com  A99F 8785 F491 D5FD 04D7 ACD9 4158 FFDF 2894 6876
alex at dojotoolkit.org BE03 E88D EABB 2116 CC49 8259 CF78 E242 59C3 9723

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFI1BQvz3jiQlnDlyMRAr7xAKC9Yx7r5ZssqRMB48s08LK3NP0BrwCfa9u3
6RylFwf7H+mpM+dVahugpzA=
=Y9y7
-----END PGP SIGNATURE-----



More information about the dojo-contributors mailing list