mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
Added HashMap
This commit is contained in:
parent
2a12f7dcaa
commit
bb67ee8d28
2 changed files with 50 additions and 0 deletions
35
src/apis.js
35
src/apis.js
|
|
@ -807,6 +807,39 @@ var angularFunction = {
|
|||
}
|
||||
};
|
||||
|
||||
function hashKey(obj) {
|
||||
var objType = typeof obj;
|
||||
var key = obj;
|
||||
if (objType == 'object') {
|
||||
if (typeof (key = obj.$hashKey) == 'function') {
|
||||
// must invoke on object to keep the right this
|
||||
key = obj.$hashKey();
|
||||
} else if (key === undefined) {
|
||||
key = obj.$hashKey = nextUid();
|
||||
}
|
||||
};
|
||||
return objType + ':' + key;
|
||||
}
|
||||
|
||||
function HashMap(){}
|
||||
HashMap.prototype = {
|
||||
put: function(key, value) {
|
||||
var _key = hashKey(key);
|
||||
var oldValue = this[_key];
|
||||
this[_key] = value;
|
||||
return oldValue;
|
||||
},
|
||||
get: function(key) {
|
||||
return this[hashKey(key)];
|
||||
},
|
||||
remove: function(key) {
|
||||
var _key = hashKey(key);
|
||||
var value = this[_key];
|
||||
delete this[_key];
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
function defineApi(dst, chain){
|
||||
angular[dst] = angular[dst] || {};
|
||||
forEach(chain, function(parent){
|
||||
|
|
@ -819,6 +852,8 @@ defineApi('Array', [angularGlobal, angularCollection, angularArray]);
|
|||
defineApi('Object', [angularGlobal, angularCollection, angularObject]);
|
||||
defineApi('String', [angularGlobal, angularString]);
|
||||
defineApi('Date', [angularGlobal, angularDate]);
|
||||
// TODO: enable and document this API
|
||||
//defineApi('HashMap', [HashMap]);
|
||||
//IE bug
|
||||
angular['Date']['toString'] = angularDate['toString'];
|
||||
defineApi('Function', [angularGlobal, angularCollection, angularFunction]);
|
||||
|
|
|
|||
|
|
@ -252,5 +252,20 @@ describe('api', function(){
|
|||
assertEquals({a:1, b:2}, angular.Object.extend({a:1}, {b:2}));
|
||||
});
|
||||
|
||||
describe('HashMap', function(){
|
||||
it('should do basic crud', function(){
|
||||
var map = new HashMap();
|
||||
var key = {};
|
||||
var value1 = {};
|
||||
var value2 = {};
|
||||
expect(map.put(key, value1)).toEqual(undefined);
|
||||
expect(map.put(key, value2)).toEqual(value1);
|
||||
expect(map.get(key)).toEqual(value2);
|
||||
expect(map.get({})).toEqual(undefined);
|
||||
expect(map.remove(key)).toEqual(value2);
|
||||
expect(map.get(key)).toEqual(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue