Skip to content

Commit bb67ee8

Browse files
committed
Added HashMap
1 parent 2a12f7d commit bb67ee8

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/apis.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,39 @@ var angularFunction = {
807807
}
808808
};
809809

810+
function hashKey(obj) {
811+
var objType = typeof obj;
812+
var key = obj;
813+
if (objType == 'object') {
814+
if (typeof (key = obj.$hashKey) == 'function') {
815+
// must invoke on object to keep the right this
816+
key = obj.$hashKey();
817+
} else if (key === undefined) {
818+
key = obj.$hashKey = nextUid();
819+
}
820+
};
821+
return objType + ':' + key;
822+
}
823+
824+
function HashMap(){}
825+
HashMap.prototype = {
826+
put: function(key, value) {
827+
var _key = hashKey(key);
828+
var oldValue = this[_key];
829+
this[_key] = value;
830+
return oldValue;
831+
},
832+
get: function(key) {
833+
return this[hashKey(key)];
834+
},
835+
remove: function(key) {
836+
var _key = hashKey(key);
837+
var value = this[_key];
838+
delete this[_key];
839+
return value;
840+
}
841+
};
842+
810843
function defineApi(dst, chain){
811844
angular[dst] = angular[dst] || {};
812845
forEach(chain, function(parent){
@@ -819,6 +852,8 @@ defineApi('Array', [angularGlobal, angularCollection, angularArray]);
819852
defineApi('Object', [angularGlobal, angularCollection, angularObject]);
820853
defineApi('String', [angularGlobal, angularString]);
821854
defineApi('Date', [angularGlobal, angularDate]);
855+
// TODO: enable and document this API
856+
//defineApi('HashMap', [HashMap]);
822857
//IE bug
823858
angular['Date']['toString'] = angularDate['toString'];
824859
defineApi('Function', [angularGlobal, angularCollection, angularFunction]);

test/ApiSpecs.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,20 @@ describe('api', function(){
252252
assertEquals({a:1, b:2}, angular.Object.extend({a:1}, {b:2}));
253253
});
254254

255+
describe('HashMap', function(){
256+
it('should do basic crud', function(){
257+
var map = new HashMap();
258+
var key = {};
259+
var value1 = {};
260+
var value2 = {};
261+
expect(map.put(key, value1)).toEqual(undefined);
262+
expect(map.put(key, value2)).toEqual(value1);
263+
expect(map.get(key)).toEqual(value2);
264+
expect(map.get({})).toEqual(undefined);
265+
expect(map.remove(key)).toEqual(value2);
266+
expect(map.get(key)).toEqual(undefined);
267+
});
268+
});
269+
255270
});
256271

0 commit comments

Comments
 (0)