Git Product home page Git Product logo

skellla / node-object-hash Goto Github PK

View Code? Open in Web Editor NEW
84.0 4.0 20.0 759 KB

Node.js object hash library with properties/arrays sorting to provide constant hashes. It also provides a method that returns sorted object strings that can be used for object comparison without hashes.

Home Page: https://savelife.in.ua/en/donate-en/

License: MIT License

JavaScript 8.40% TypeScript 91.60%
node hash node-crypto sorting nodejs javascript js hashing-library sorter

node-object-hash's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

node-object-hash's Issues

sort ... sometimes?

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.

Support for node 4.3

This lib seems great! It's exactly what I need as well
Is there a chance to have it compatible with node 4.3?
What part of it is dependent on node 6.x?

disable sorting of arrays

I'd like to be able to disable the sorting of arrays, so that hash([1,2] != hash([2,1]), while keeping all other "object" types sorted. Is this currently possible?

Library fails with ES6 classes

Hi,

Let's suppose that I have following ES6 class:

class ClassA {
  constructor(public propertyA: string) { 
    this.propertyA = propertyA;
  }
}

obj.constructor.name returns 'ClassA' and __guessObjectType() returns 'unknown'

As a result, all different objects of type ClassA return same hash!

I am not sure how to handle this case, naively I would also return 'object' in default handling of switch in guessObjectType()

Support for BigInt type

Hashing fails when there is BigInt type.

> const Hasher = require('node-object-hash')
> const hasher = Hasher()
> hasher.hash(1)
'6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b'
> hasher.hash(1n)
Uncaught TypeError: stringifiers[typeGuess_1.guessType(...)] is not a function
    at objectToString (***/node_modules/node-object-hash/dist/objectSorter.js:135:56)
    at Object.hashObject [as hash] (***/node_modules/node-object-hash/dist/hasher.js:33:22)

WeakMap & WeakSet support

My tests indicate that Array.from() does not work on WeakMap and WeakSet, so I don't think these should be removed from objectSorter.js

Hash of objects with custom prototype (created via constructor)

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)

Throws on an empty object without prototype

require('node-object-hash')().hash(Object.create(null))
TypeError: Cannot read property 'name' of undefined
    at _guessObjectType (.../node-object-hash/objectSorter.js:20:27)
    at _guessType (.../node-object-hash/objectSorter.js:68:37)
    at objectToString (.../node-object-hash/objectSorter.js:213:24)
    at sortObject (.../node-object-hash/index.js:78:12)
    at Object.hashObject [as hash] (.../node-object-hash/index.js:103:18

Here is the offending line (20):

function _guessObjectType(obj) {
if (obj === null) {
return 'null';
}
switch (obj.constructor.name) {
case 'Array':

This assumes that every (non-null) object has as constructor property, which is not generally the case.

Usage

I am sorry if this comes off as a dumb question but what are some of examples of what you could use this library to do? I have entertained the idea of hashing my objects into a history tree, similar to git and things like that. I was curious what others actually use this for.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.