SkeLLLa/node-object-hash

sort ... sometimes?

shaunc opened this issue · 2 comments

I would like to hash my arrays hashed depending on their order, but objects and sets are unordered, so want them sorted before hash. Yet currently "sort" is a global option....

Possible solutions:

  1. On initialization, "sortFilter" option that takes a predicate which should return true if given object should be sorted before hash.
  2. per-hash, pass "sort" option, which could be explicitly true or false to override global, or not-present/null to accept default.

EDIT -- In fact, if (2) is to be viable, it should take "sortFilter" as well, so that subobjects can be taken care of properly.

@shaunc do you want sort filter by variable names? or by types?

{
  sort_a: [3,2,1],
  dontsort_b: [1,2,3],
  sort_c: {a:1, b:2} 
}

So there are two options to pass some kind sort: "^sort_.*" to reorder sort_a and sort_c or pass sort: ['array'] to sort only array types?

BTW: Order of fields in objects in js could be unpredictable, so without sorting them it's not guaranteed that objects {a:1, b:2} and {b:2, a:1} will have different hashes.

It seems not actual. Closing for now. Reopen if necessary.