Support custom hashes in ve.getHash()

Change-Id: I9193472773b999b45ce850a609ad5fe141421dbf
This commit is contained in:
Catrope 2012-10-03 17:51:10 -07:00
parent 22c7f16b99
commit b630b8a2a6

View file

@ -241,6 +241,10 @@
* argument of JSON.stringify and sort the object by key as it's being serialized. This may or may
* not be the fastest way to do this; we should investigate this further.
*
* Objects an arrays are hashed recursively. When hashing an object that has a .getHash()
* function, we call that function and use its return value rather than hashing the object
* ourselves. This allows classes to define custom hashing.
*
* @static
* @method
* @param {Object} val Object to generate hash for
@ -263,9 +267,13 @@
*/
ve.getHash.keySortReplacer = function ( key, val ) {
var normalized, keys, i, len;
// Only normalize objects when the key-order is ambiguous
// (e.g. any object not an array).
if ( val && typeof val.getHash === 'function' ) {
// This object has its own custom hash function, use it
return val.getHash();
}
if ( !ve.isArray( val ) && Object( val ) === val ) {
// Only normalize objects when the key-order is ambiguous
// (e.g. any object not an array).
normalized = {};
keys = ve.getObjectKeys( val ).sort();
i = 0;