Anatol
Anatol

Reputation: 191

JS: Hashing function for combinable hashes from Objects

I need to determine if objects are super/subsets of one another.

Given a function hash that generates fixed-length string hashes from arbitrary objects:

const fooHash = hash({foo: "foo"}) 
const barHash = hash({bar: "bar"}) 

const fooBarHash = hash({foo: "foo", bar: "bar"})

Is there a combine function, such that:

combine(fooHash, barHash) === fooBarHash

?

If yes, how would the combine function work?

Upvotes: 2

Views: 57

Answers (1)

Louay Alakkad
Louay Alakkad

Reputation: 7408

If the hashing function is reversible, then you can have the following combine function:

function combine(obj1hash, obj2hash) {
  return hash(merge(unhash(obj1hash), unhash(obj2hash)));
}

hash and unhash can be JSON.stringify and JSON.parse. merge is any function that merges two js objects like this one.

Upvotes: 1

Related Questions