SkeLLLa/node-object-hash

Hash of objects with custom prototype (created via constructor)

DmitriyBerezin opened this issue · 3 comments

I've tried your module to calculate hash of objects are created via constructor. Although two objects have different property values they hashes are equal. Here is an example:

const assert = require('assert')
const hasher = require('node-object-hash')().hash

class Test {
	constructor(id) {
		this.id = id
	}
}

const t1 = new Test(1)
const t2 = new Test(2)
const h1 = hasher(t1)
const h2 = hasher(t2)

assert.notEqual(h1, h2)

@DmitriyBerezin that's expected :).
Unfortunately there's no way to predict what kind of structure will be in custom objects. It can contain different fields, properties, etc.

For custom objects it calculates hashes from constructor name + object's string representation which is the result of calling .toString() method. See https://github.com/SkeLLLa/node-object-hash/blob/master/objectSorter.js#L202

So if you need to get it work, then you'll need to implement proper .toString method. There you can choose only those properties that are valuable for your hash. Also you can use objectSorter method as well.

Something like this:

const {hash, sort} = require('node-object-hash')()

class Test {
...
        toString() {
                return sort({
                      id: this.id
                });
        }
}

See full example here: https://runkit.com/skellla/5a5e39f8463c7c001229d057

@SkeLLLa Thank you for this response and example! Now it clear for me.

Thanks to you too, @DmitriyBerezin. I'll add it later to Readme later.