fix Ox.isEqual

This commit is contained in:
rolux 2012-05-25 23:00:28 +02:00
parent c481889f3a
commit 12dcdf13fc

View file

@ -73,6 +73,10 @@ Ox.isElement = function(value) {
/*@
Ox.isEqual <function> Returns true if two values are equal
<script>
Ox.test.element = document.createElement('a');
Ox.test.fn = function() {};
</script>
> Ox.isEqual((function() { return arguments; }()), (function() { return arguments; }()))
true
> Ox.isEqual([1, 2, 3], [1, 2, 3])
@ -85,20 +89,24 @@ Ox.isEqual <function> Returns true if two values are equal
true
> Ox.isEqual(new Date(0), new Date(1))
false
> Ox.isEqual(Ox.test.element, Ox.test.element)
true
> Ox.isEqual(document.createElement('a'), document.createElement('a'))
true
> Ox.isEqual(document.createElement('a'), document.createElement('b'))
false
> Ox.isEqual(function(a) { return a; }, function(a) { return a; })
> Ox.isEqual(Ox.test.fn, Ox.test.fn)
true
> Ox.isEqual(function(a) { return a; }, function(b) { return b; })
> Ox.isEqual(function() {}, function() {})
false
> Ox.isEqual(Infinity, Infinity)
true
> Ox.isEqual(-Infinity, Infinity)
> Ox.isEqual(Infinity, -Infinity)
false
> Ox.isEqual(NaN, NaN)
false
> Ox.isEqual(null, null)
true
> Ox.isEqual(null, void 0)
false
> Ox.isEqual(0, 0)
true
> Ox.isEqual({}, {})
@ -117,34 +125,32 @@ Ox.isEqual <function> Returns true if two values are equal
true
@*/
Ox.isEqual = function(a, b) {
var isEqual = false, type = Ox.typeOf(a);
if (a === b) {
isEqual = true;
var ret = false, type = Ox.typeOf(a);
// 0 === -0, but not equal
if (a === b && (a !== 0 || 1 / a === 1 / b)) {
ret = true;
} else if (type == Ox.typeOf(b)) {
if (a == b) {
isEqual = true;
ret = true;
} else if (type == 'date') {
isEqual = a.getTime() == b.getTime();
ret = +a == +b;
} else if (type == 'element') {
isEqual = a.isEqualNode(b);
} else if (type == 'function') {
// fixme: this doesn't do it
// isEqual = a.toString() == b.toString();
ret = a.isEqualNode(b);
} else if (type == 'regexp') {
isEqual = a.global == b.global && a.ignore == b.ignore
ret = a.global == b.global && a.ignore == b.ignore
&& a.multiline == b.multiline && a.source == b.source;
} else if (
['arguments', 'array', 'object'].indexOf(type) > -1
&& Ox.len(a) == Ox.len(b)
) {
isEqual = true;
Ox.forEach(a, function(v, k) {
isEqual = Ox.isEqual(v, b[k]);
!isEqual && Ox.Break();
ret = true;
Ox.forEach(a, function(value, key) {
ret = Ox.isEqual(value, b[key]);
!ret && Ox.Break();
});
}
}
return isEqual;
return ret;
};
/*@