mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 15:40:22 +00:00
BREAKING CHANGE: It is considered an error to have two items produce the same track by key. (This was tolerated before.)
67 lines
1.5 KiB
JavaScript
67 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
|
|
/**
|
|
* Computes a hash of an 'obj'.
|
|
* Hash of a:
|
|
* string is string
|
|
* number is number as string
|
|
* object is either result of calling $$hashKey function on the object or uniquely generated id,
|
|
* that is also assigned to the $$hashKey property of the object.
|
|
*
|
|
* @param obj
|
|
* @returns {string} hash string such that the same input will have the same hash string.
|
|
* The resulting string key is in 'type:hashKey' format.
|
|
*/
|
|
function hashKey(obj) {
|
|
var objType = typeof obj,
|
|
key;
|
|
|
|
if (objType == 'object' && obj !== null) {
|
|
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();
|
|
}
|
|
} else {
|
|
key = obj;
|
|
}
|
|
|
|
return objType + ':' + key;
|
|
}
|
|
|
|
/**
|
|
* HashMap which can use objects as keys
|
|
*/
|
|
function HashMap(array){
|
|
forEach(array, this.put, this);
|
|
}
|
|
HashMap.prototype = {
|
|
/**
|
|
* Store key value pair
|
|
* @param key key to store can be any type
|
|
* @param value value to store can be any type
|
|
*/
|
|
put: function(key, value) {
|
|
this[hashKey(key)] = value;
|
|
},
|
|
|
|
/**
|
|
* @param key
|
|
* @returns the value for the key
|
|
*/
|
|
get: function(key) {
|
|
return this[hashKey(key)];
|
|
},
|
|
|
|
/**
|
|
* Remove the key/value pair
|
|
* @param key
|
|
*/
|
|
remove: function(key) {
|
|
var value = this[key = hashKey(key)];
|
|
delete this[key];
|
|
return value;
|
|
}
|
|
};
|