Skip to content

Commit ab325a0

Browse files
committed
feat(HashMap): add support for .has() and initializing with an object
1 parent 5fc9933 commit ab325a0

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/apis.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,19 @@ function hashKey(obj, nextUidFn) {
3636
/**
3737
* HashMap which can use objects as keys
3838
*/
39-
function HashMap(array, isolatedUid) {
39+
function HashMap(seedData, isolatedUid) {
4040
if (isolatedUid) {
4141
var uid = 0;
4242
this.nextUid = function() {
4343
return ++uid;
4444
};
4545
}
46-
forEach(array, this.put, this);
46+
47+
if (seedData) {
48+
var putFn = isArray(seedData) ?
49+
this.put : reverseParams(this.put.bind(this));
50+
forEach(seedData, putFn, this);
51+
}
4752
}
4853
HashMap.prototype = {
4954
/**
@@ -63,6 +68,14 @@ HashMap.prototype = {
6368
return this[hashKey(key, this.nextUid)];
6469
},
6570

71+
/**
72+
* @param key
73+
* @returns {boolean} whether a value is stored under the specified key
74+
*/
75+
has: function(key) {
76+
return this.hasOwnProperty(hashKey(key, this.nextUid));
77+
},
78+
6679
/**
6780
* Remove the key/value pair
6881
* @param key

src/ng/directive/select.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,11 @@ var selectDirective = function() {
563563
// Write value now needs to set the selected property of each matching option
564564
selectCtrl.writeValue = function writeMultipleValue(value) {
565565
var items = new HashMap(value);
566+
var selectValueMap = selectCtrl.selectValueMap;
567+
566568
forEach(element.find('option'), function(option) {
567-
option.selected = isDefined(items.get(option.value)) || isDefined(items.get(selectCtrl.selectValueMap[option.value]));
569+
var value = option.value;
570+
option.selected = items.has(value) || items.has(selectValueMap[value]);
568571
});
569572
};
570573

test/ApiSpecs.js

+12
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ describe('api', function() {
88
var key = {};
99
var value1 = {};
1010
var value2 = {};
11+
1112
map.put(key, value1);
1213
map.put(key, value2);
14+
15+
expect(map.has(key)).toBe(true);
16+
expect(map.has({})).toBe(false);
1317
expect(map.get(key)).toBe(value2);
1418
expect(map.get({})).toBeUndefined();
1519
expect(map.remove(key)).toBe(value2);
20+
expect(map.has(key)).toBe(false);
1621
expect(map.get(key)).toBeUndefined();
1722
});
1823

@@ -23,6 +28,13 @@ describe('api', function() {
2328
expect(map.get('c')).toBeUndefined();
2429
});
2530

31+
it('should init from an object', function() {
32+
var map = new HashMap({a: 'foo', b: 'bar'});
33+
expect(map.get('a')).toBe('foo');
34+
expect(map.get('b')).toBe('bar');
35+
expect(map.get('c')).toBeUndefined();
36+
});
37+
2638
it('should maintain hashKey for object keys', function() {
2739
var map = new HashMap();
2840
var key = {};

0 commit comments

Comments
 (0)