mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
typeof null == 'object', but it doesn't behave like an object because its properties can't be dereferenced, so we need to special-case it. Closes #702
101 lines
2.2 KiB
JavaScript
101 lines
2.2 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;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* A map where multiple values can be added to the same key such that they form a queue.
|
|
* @returns {HashQueueMap}
|
|
*/
|
|
function HashQueueMap() {}
|
|
HashQueueMap.prototype = {
|
|
/**
|
|
* Same as array push, but using an array as the value for the hash
|
|
*/
|
|
push: function(key, value) {
|
|
var array = this[key = hashKey(key)];
|
|
if (!array) {
|
|
this[key] = [value];
|
|
} else {
|
|
array.push(value);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Same as array shift, but using an array as the value for the hash
|
|
*/
|
|
shift: function(key) {
|
|
var array = this[key = hashKey(key)];
|
|
if (array) {
|
|
if (array.length == 1) {
|
|
delete this[key];
|
|
return array[0];
|
|
} else {
|
|
return array.shift();
|
|
}
|
|
}
|
|
}
|
|
};
|