diff --git a/modules/angular2/src/facade/collection.dart b/modules/angular2/src/facade/collection.dart index 76df62499d..3e3671d02d 100644 --- a/modules/angular2/src/facade/collection.dart +++ b/modules/angular2/src/facade/collection.dart @@ -86,6 +86,17 @@ class StringMapWrapper { return a.keys.toList(); } static bool isEmpty(Map m) => m.isEmpty; + static bool equals(Map m1, Map m2) { + if (m1.length != m2.length) { + return false; + } + for (var key in m1.keys) { + if (m1[key] != m2[key]) { + return false; + } + } + return true; + } } class ListWrapper { diff --git a/modules/angular2/src/facade/collection.ts b/modules/angular2/src/facade/collection.ts index ac684b8bdb..a974018541 100644 --- a/modules/angular2/src/facade/collection.ts +++ b/modules/angular2/src/facade/collection.ts @@ -86,6 +86,22 @@ export class StringMapWrapper { return m; } + + static equals(m1, m2) { + var k1 = Object.keys(m1); + var k2 = Object.keys(m2); + if (k1.length != k2.length) { + return false; + } + var key; + for (var i = 0; i < k1.length; i++) { + key = k1[i]; + if (m1[key] !== m2[key]) { + return false; + } + } + return true; + } } export class ListWrapper { diff --git a/modules/angular2/test/facade/collection_spec.js b/modules/angular2/test/facade/collection_spec.js index 886242cb8c..3dd20cf45d 100644 --- a/modules/angular2/test/facade/collection_spec.js +++ b/modules/angular2/test/facade/collection_spec.js @@ -1,7 +1,7 @@ import {describe, it, expect, beforeEach, ddescribe, iit, xit} from 'angular2/test_lib'; -import {List, ListWrapper} from 'angular2/src/facade/collection'; +import {List, ListWrapper, StringMap, StringMapWrapper} from 'angular2/src/facade/collection'; export function main() { describe('ListWrapper', () => { @@ -69,4 +69,44 @@ export function main() { }); }); }); + + describe('StringMapWrapper', () => { + describe('equals', () => { + it('should return true when comparing empty maps', () => { + expect(StringMapWrapper.equals({}, {})).toBe(true); + }); + + it('should return true when comparing the same map', () => { + var m1 = {'a': 1, 'b': 2, 'c': 3}; + expect(StringMapWrapper.equals(m1, m1)).toBe(true); + }); + + it('should return true when comparing different maps with the same keys and values', () => { + var m1 = {'a': 1, 'b': 2, 'c': 3}; + var m2 = {'a': 1, 'b': 2, 'c': 3}; + expect(StringMapWrapper.equals(m1, m2)).toBe(true); + }); + + it('should return false when comparing maps with different numbers of keys', () => { + var m1 = {'a': 1, 'b': 2, 'c': 3}; + var m2 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}; + expect(StringMapWrapper.equals(m1, m2)).toBe(false); + expect(StringMapWrapper.equals(m2, m1)).toBe(false); + }); + + it('should return false when comparing maps with different keys', () => { + var m1 = {'a': 1, 'b': 2, 'c': 3}; + var m2 = {'a': 1, 'b': 2, 'CC': 3}; + expect(StringMapWrapper.equals(m1, m2)).toBe(false); + expect(StringMapWrapper.equals(m2, m1)).toBe(false); + }); + + it('should return false when comparing maps with different values', () => { + var m1 = {'a': 1, 'b': 2, 'c': 3}; + var m2 = {'a': 1, 'b': 20, 'c': 3}; + expect(StringMapWrapper.equals(m1, m2)).toBe(false); + expect(StringMapWrapper.equals(m2, m1)).toBe(false); + }); + }); + }); }