added equals method to angular.equals and $equals

This commit is contained in:
Misko Hevery 2010-07-19 12:29:24 -07:00
parent cc097867f4
commit 7e96af0fdd
5 changed files with 72 additions and 10 deletions

View file

@ -1,13 +1,20 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript" src="../src/angular-bootstrap.js#autobind"></script>
<script type="text/javascript"
src="http://angularjs.org/ng/js/angular-debug.js"></script>
<script type="text/javascript">
(function(window, previousOnLoad){
window.onload = function(){
try {
(previousOnLoad||angular.noop)();
} catch(e) {}
angular.compile(window.document).$init();
};
})(window, window.onload);
</script>
</head>
<body>
{{$location.hashSearch.order}} <br/>
<input type="radio" name="$location.hashSearch.order" value="A"/> A <br/>
<input type="radio" name="$location.hashSearch.order" checked value="B"/> B <br/>
<input type="radio" name="$location.hashSearch.order" value="C"/> C <br/>
{{$location.hashSearch.order}} <br/>
Hello {{'World'}}!
</body>
</html>

View file

@ -263,6 +263,32 @@ function copy(source, destination){
}
}
function equals(o1, o2) {
if (o1 == o2) return true;
var t1 = typeof o1, t2 = typeof o2, length, key, keySet;
if (t1 == t2 && t1 == 'object') {
if (o1 instanceof Array) {
if ((length = o1.length) == o2.length) {
for(key=0; key<length; key++) {
if (!equals(o1[key], o2[key])) return false;
}
return true;
}
} else {
keySet = {};
for(key in o1) {
if (key.charAt(0) !== '$' && !equals(o1[key], o2[key])) return false;
keySet[key] = true;
}
for(key in o2) {
if (key.charAt(0) !== '$' && keySet[key] !== true) return false;
}
return true;
}
}
return false;
}
function setHtml(node, html) {
if (isLeafNode(node)) {
if (msie) {
@ -333,7 +359,7 @@ function outerHTML(node) {
function toBoolean(value) {
if (value && value.length !== 0) {
var v = lowercase("" + value);
value = !(v == 'f' || v == '0' || v == 'false' || v == 'no' || v == '[]');
value = !(v == 'f' || v == '0' || v == 'false' || v == 'no' || v == 'n' || v == '[]');
} else {
value = false;
}

View file

@ -14,6 +14,7 @@ extend(angular, {
'scope': createScope,
'copy': copy,
'extend': extend,
'equals': equals,
'foreach': foreach,
'noop':noop,
'bind':bind,

View file

@ -12,7 +12,8 @@ var angularGlobal = {
};
var angularCollection = {
'size': size
'size': size,
'equals': equals
};
var angularObject = {
'extend': extend

View file

@ -54,3 +54,30 @@ describe("copy", function(){
});
});
describe('equals', function(){
it('should return true if same object', function(){
var o = {};
expect(equals(o, o)).toEqual(true);
expect(equals(1, '1')).toEqual(true);
expect(equals(1, '2')).toEqual(false);
});
it('should recurse into object', function(){
expect(equals({}, {})).toEqual(true);
expect(equals({name:'misko'}, {name:'misko'})).toEqual(true);
expect(equals({name:'misko', age:1}, {name:'misko'})).toEqual(false);
expect(equals({name:'misko'}, {name:'misko', age:1})).toEqual(false);
expect(equals({name:'misko'}, {name:'adam'})).toEqual(false);
expect(equals(['misko'], ['misko'])).toEqual(true);
expect(equals(['misko'], ['adam'])).toEqual(false);
expect(equals(['misko'], ['misko', 'adam'])).toEqual(false);
});
it('should ignore $ member variables', function(){
expect(equals({name:'misko', $id:1}, {name:'misko', $id:2})).toEqual(true);
expect(equals({name:'misko'}, {name:'misko', $id:2})).toEqual(true);
expect(equals({name:'misko', $id:1}, {name:'misko'})).toEqual(true);
});
});