Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26d90b1

Browse files
committed
Added HashMap
1 parent 02dbf41 commit 26d90b1

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/apis.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,40 @@ var angularFunction = {
790790
}
791791
};
792792

793+
function hashKey(obj) {
794+
var objType = typeof obj;
795+
var key = obj;
796+
if (objType == 'object') {
797+
keyType = typeof (key = obj.$hashKey);
798+
if (typeof key == 'function') {
799+
// must invoke on object to keep the right this
800+
key = obj.$hashKey();
801+
} else if (key === undefined) {
802+
key = obj.$hashKey = nextUid();
803+
}
804+
};
805+
return objType + ':' + key;
806+
}
807+
808+
function HashMap(){}
809+
HashMap.prototype = {
810+
put: function(key, value) {
811+
var _key = hashKey(key);
812+
var oldValue = this[_key];
813+
this[_key] = value;
814+
return oldValue;
815+
},
816+
get: function(key) {
817+
return this[hashKey(key)];
818+
},
819+
remove: function(key) {
820+
var _key = hashKey(key);
821+
var value = this[_key];
822+
delete this[_key];
823+
return value;
824+
}
825+
};
826+
793827
function defineApi(dst, chain){
794828
angular[dst] = angular[dst] || {};
795829
forEach(chain, function(parent){
@@ -802,6 +836,8 @@ defineApi('Array', [angularGlobal, angularCollection, angularArray]);
802836
defineApi('Object', [angularGlobal, angularCollection, angularObject]);
803837
defineApi('String', [angularGlobal, angularString]);
804838
defineApi('Date', [angularGlobal, angularDate]);
839+
// TODO: enable and document this API
840+
//defineApi('HashMap', [HashMap]);
805841
//IE bug
806842
angular['Date']['toString'] = angularDate['toString'];
807843
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)