planttheidea / hash-it Goto Github PK
View Code? Open in Web Editor NEWHash any object type based on its values
License: MIT License
Hash any object type based on its values
License: MIT License
The encoding used by hash-it a) makes it trivial to generate collisions (cf.), b) doesn't handle nested dates, and c) doesn't distinguish between different circular references:
import hash from 'hash-it'
hash.is(42, "number|42") // true
hash.is(null, "null|null") // true
hash.is(new Date(1970, 0, 1), "Date|0") // true
hash.is(new Date(1970, 0, 1), "1970-01-01T00:00:00.000Z") // false
hash.is({ date: new Date(1970, 0, 1) }, { date: "1970-01-01T00:00:00.000Z" }) // true
obj = {}
obj.self = obj
hash.is(obj, { "self": "~" }) // true
arr1 = [obj, obj, obj]
arr2 = [obj, obj]
arr2.push(arr2)
hash.is(arr1, '[{"self":"~"},"~","~"]') // true
hash.is(arr1, arr2) // true
These can be worked around with a more consistent/robust encoding, e.g. nested dates can be detected via this[key] instanceof Date
, and strings can be distinguished from other primitives by (always) using a prefix, e.g. "string|Hello, world!".
I haven't used it, but it looks like (your :-) fast-stringify provides a path parameter which can be used to distinguish between the different circular references.
We're using hash-it
to compute cache keys and ran into situations where objects to be hashed look like this
{ a: 1, b: undefined }
{ a: 1 }
We can pass it through JSON.stringify()
beforehand locally but it would be better if this utility would handle that
So is there any way to exclude object keys where their value is undefined
?
Hi when am I supposed to use it ? From the example code, I assume that objects that have same set of key/ value will produce same hash, so it's quite of verification stuff ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.