aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMisko Hevery2010-07-19 12:29:24 -0700
committerMisko Hevery2010-07-19 12:29:24 -0700
commit7e96af0fdd9af8c479992363f68578305df0337e (patch)
treefccb29641d030f390db1250a98be5412af993e94
parentcc097867f49673005d47a7f8f0cbe25f7d5c2163 (diff)
downloadangular.js-7e96af0fdd9af8c479992363f68578305df0337e.tar.bz2
added equals method to angular.equals and $equals
-rw-r--r--example/temp.html23
-rw-r--r--src/Angular.js28
-rw-r--r--src/AngularPublic.js1
-rw-r--r--src/apis.js3
-rw-r--r--test/AngularSpec.js27
5 files changed, 72 insertions, 10 deletions
diff --git a/example/temp.html b/example/temp.html
index d07a6948..838a463d 100644
--- a/example/temp.html
+++ b/example/temp.html
@@ -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>
diff --git a/src/Angular.js b/src/Angular.js
index 07e9096b..e39e31c9 100644
--- a/src/Angular.js
+++ b/src/Angular.js
@@ -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;
}
diff --git a/src/AngularPublic.js b/src/AngularPublic.js
index 7230c3e5..817a1f93 100644
--- a/src/AngularPublic.js
+++ b/src/AngularPublic.js
@@ -14,6 +14,7 @@ extend(angular, {
'scope': createScope,
'copy': copy,
'extend': extend,
+ 'equals': equals,
'foreach': foreach,
'noop':noop,
'bind':bind,
diff --git a/src/apis.js b/src/apis.js
index 306d0ce8..2d0c571e 100644
--- a/src/apis.js
+++ b/src/apis.js
@@ -12,7 +12,8 @@ var angularGlobal = {
};
var angularCollection = {
- 'size': size
+ 'size': size,
+ 'equals': equals
};
var angularObject = {
'extend': extend
diff --git a/test/AngularSpec.js b/test/AngularSpec.js
index 6d462b14..b4e90175 100644
--- a/test/AngularSpec.js
+++ b/test/AngularSpec.js
@@ -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);
+ });
+
+});