From b630b8a2a69eb82194214a8117191bc7005038e7 Mon Sep 17 00:00:00 2001 From: Catrope Date: Wed, 3 Oct 2012 17:51:10 -0700 Subject: [PATCH] Support custom hashes in ve.getHash() Change-Id: I9193472773b999b45ce850a609ad5fe141421dbf --- modules/ve/ve.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/ve/ve.js b/modules/ve/ve.js index 4fd424b5d3..5b0a489e83 100644 --- a/modules/ve/ve.js +++ b/modules/ve/ve.js @@ -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;