aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMisko Hevery2011-04-12 16:15:05 -0700
committerMisko Hevery2011-06-08 15:01:32 -0700
commitbb67ee8d28f2cddb4b503dc8909649994a4d67e1 (patch)
tree3abc82d98d29aee7abfd197e3d6d5c897f375804
parent2a12f7dcaa078e1d6c3b5092e62dd5f404b8c3e4 (diff)
downloadangular.js-bb67ee8d28f2cddb4b503dc8909649994a4d67e1.tar.bz2
Added HashMap
-rw-r--r--src/apis.js35
-rw-r--r--test/ApiSpecs.js15
2 files changed, 50 insertions, 0 deletions
diff --git a/src/apis.js b/src/apis.js
index 237a1c1c..11fa9838 100644
--- a/src/apis.js
+++ b/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]);
diff --git a/test/ApiSpecs.js b/test/ApiSpecs.js
index 0383a457..2fd51873 100644
--- a/test/ApiSpecs.js
+++ b/test/ApiSpecs.js
@@ -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);
+ });
+ });
+
});