object-hash
Advanced tools
Comparing version 0.3.0 to 0.4.0
24
index.js
@@ -85,4 +85,6 @@ 'use strict'; | ||
typeHasher(hashFn, options).dispatch(object); | ||
var context = []; | ||
typeHasher(hashFn, options, context).dispatch(object); | ||
return (options.encoding === 'buffer') ? hashFn.digest() : | ||
@@ -92,3 +94,3 @@ hashFn.digest(options.encoding); | ||
function typeHasher(hashFn, options){ | ||
function typeHasher(hashFn, options, context){ | ||
return { | ||
@@ -104,7 +106,15 @@ dispatch: function(value){ | ||
var objType = pattern.exec(objString)[1] || 'null'; | ||
var objectNumber = null; | ||
objType = objType.toLowerCase(); | ||
if ((objectNumber = context.indexOf(object)) >= 0) { | ||
typeHasher(hashFn, options, context).dispatch("[CIRCULAR]: " + objectNumber); | ||
return; | ||
} else { | ||
context.push(object); | ||
} | ||
if(objType !== 'object') { | ||
if(typeHasher(hashFn, options)['_' + objType]) { | ||
typeHasher(hashFn, options)['_' + objType](object); | ||
if(typeHasher(hashFn, options, context)['_' + objType]) { | ||
typeHasher(hashFn, options, context)['_' + objType](object); | ||
}else{ | ||
@@ -119,3 +129,3 @@ throw new Error('Unknown object type "' + objType + '"'); | ||
if(!options.excludeValues) { | ||
typeHasher(hashFn, options).dispatch(object[key]); | ||
typeHasher(hashFn, options, context).dispatch(object[key]); | ||
} | ||
@@ -127,7 +137,7 @@ }); | ||
return arr.forEach(function(el){ | ||
typeHasher(hashFn, options).dispatch(el); | ||
typeHasher(hashFn, options, context).dispatch(el); | ||
}); | ||
}, | ||
_date: function(date){ | ||
return hashFn.update(date.toString()); | ||
return hashFn.update(date.toJSON()); | ||
}, | ||
@@ -134,0 +144,0 @@ _boolean: function(bool){ |
@@ -40,2 +40,28 @@ 'use strict'; | ||
HashTable.prototype.remove = function(/* values to be removed */){ | ||
var self = this; | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
args.forEach(function(obj){ | ||
if(Object.prototype.toString.call(obj) === '[object Array]'){ | ||
obj.forEach(function(val){ | ||
self._removeObject(val); | ||
}); | ||
}else{ | ||
self._removeObject(obj); | ||
} | ||
}); | ||
return this; | ||
}; | ||
HashTable.prototype._removeObject = function(object){ | ||
var hash = hasher(object, this.options), | ||
count = this.getCount(hash); | ||
if(count<=1) { | ||
delete this._table[hash]; | ||
} else { | ||
this._table[hash].count = count-1; | ||
} | ||
}; | ||
HashTable.prototype._addObject = function(object){ | ||
@@ -55,3 +81,3 @@ var hash = hasher(object, this.options); | ||
} | ||
} | ||
}; | ||
@@ -90,2 +116,2 @@ HashTable.prototype.hasKey = function(key){ | ||
return this; | ||
} | ||
}; |
{ | ||
"name": "object-hash", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Generate hashes from javascript objects in node and the browser.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/puleos/object-hash", |
@@ -113,1 +113,16 @@ var test = require('tape'); | ||
test('.remove() should decrement count', function(assert){ | ||
var hashTable = new hash.HashTable(); | ||
var hash1 = hash(obj1); | ||
var hash3 = hash(obj3); | ||
assert.plan(3); | ||
hashTable.add(obj1, obj3); | ||
hashTable.add(obj1); | ||
hashTable.remove(obj1, obj3); | ||
assert.equal(hashTable.getCount(hash1), 1, 'hash1 count = 1'); | ||
assert.equal(hashTable.getCount(hash3), 0, 'hash3 count = 0'); | ||
assert.equal(hashTable.hasKey(hash3), false, 'hash3 key is gone'); | ||
}); |
@@ -104,1 +104,26 @@ var test = require('tape'); | ||
}); | ||
test("recursive objects don't blow up stack", function(assert) { | ||
assert.plan(1); | ||
var hash1 = {foo: 'bar'}; | ||
hash1.recursive = hash1; | ||
assert.doesNotThrow(function(){hash(hash1);}, /Maximum call stack size exceeded/, 'Should not throw an stack size exceeded exception'); | ||
}); | ||
test("recursive arrays don't blow up stack", function(assert) { | ||
assert.plan(1); | ||
var hash1 = ['foo', 'bar']; | ||
hash1.push(hash1); | ||
assert.doesNotThrow(function(){hash(hash1);}, /Maximum call stack size exceeded/, 'Should not throw an stack size exceeded exception'); | ||
}); | ||
test("recursive handling tracks identity", function(assert) { | ||
assert.plan(1); | ||
var hash1 = {k1: {k: 'v'}, k2: {k: 'k2'}}; | ||
hash1.k1.r1 = hash1.k1; | ||
hash1.k2.r2 = hash1.k2; | ||
var hash2 = {k1: {k: 'v'}, k2: {k: 'k2'}}; | ||
hash2.k1.r1 = hash2.k2; | ||
hash2.k2.r2 = hash2.k1; | ||
assert.notEqual(hash(hash1), hash(hash2), "order of recursive objects should matter"); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
309527
6372