node-object-hash
Advanced tools
Comparing version 0.2.1 to 1.0.0
{ | ||
"name": "node-object-hash", | ||
"version": "0.2.1", | ||
"version": "1.0.0", | ||
"description": "Node.js object hash library with properties/arrays sorting to provide constant hashes", | ||
"main": "index.js", | ||
"main": "hash2.js", | ||
"directories": { | ||
@@ -12,6 +12,9 @@ "test": "test" | ||
"chai": "^3.5.0", | ||
"mocha": "^2.5.3" | ||
"faker": "^3.1.0", | ||
"mocha": "^2.5.3", | ||
"object-hash": "^1.1.4" | ||
}, | ||
"scripts": { | ||
"test": "mocha --harmony" | ||
"test": "mocha --harmony", | ||
"bench": "node --expose-gc ./bench/index.js" | ||
}, | ||
@@ -18,0 +21,0 @@ "repository": { |
104
README.md
# node-object-hash | ||
Node object hash library. Built on top of node's crypto module. | ||
Node.js object hash library with properties/arrays sorting to provide constant hashes. | ||
Built on top of node's crypto module (so for using in browser use something | ||
like browserify or use crypto functions polyfills). | ||
### Installation | ||
@@ -10,3 +13,2 @@ `npm i node-object-hash` | ||
- Supports object property sorting for constant hashes for objects with same properties, but different order. | ||
- NOTE: object arrays should be sorted manually if needed | ||
- Supports ES6 Maps and Sets. | ||
@@ -20,53 +22,89 @@ - Supports type coercion (e.g. 1 and "1" will be the same) | ||
### Changes | ||
#### v0.x.x -> v1.0.0 | ||
- Sorting mechanism rewritten form ES6 Maps to simple arrays | ||
(add <=node-4.0.0 support) | ||
- Performance optimization (~2 times faster than 0.x.x) | ||
- API changes: | ||
- Now module returns 'constructor' function, where you can set | ||
default parameters: ```var objectHash = require('node-object-hash')(options);``` | ||
### API | ||
#### Constructor `require('node-object-hash')([options])` | ||
Returns preconfigured object with API | ||
Parameters: | ||
* `options`:`<object>` - object with hasher config options | ||
* `options.coerce`:`<boolean>` - if true performs type coercion (default: `true`); | ||
e.g. `hash(true) == hash('1') == hash(1)`, `hash(false) == hash('0') == hash(0)` | ||
* `options.sort`:`<boolean>` - if true performs sorting on objects, arrays, etc. (default: `true`); | ||
* `options.alg`:`<string>` - sets default hash algorithm (default: `'sha256'`); can be overridden in `hash` method; | ||
* `options.enc`:`<string>` - sets default hash encoding (default: `'hex'`); can be overridden in `hash` method; | ||
#### `hash(object[, options])` | ||
Returns hash string. | ||
* `object` object for calculating hash; | ||
* `options` object with options; | ||
* `options.alg` hash algorithm (default: `'sha256'`); | ||
* `options.enc` hash encoding (default: `'hex'`); | ||
* `options.coerce` if true performs type coercion (default: `true`); | ||
* `options.sort` if true performs sorting on objects, arrays and Sets (default: `true`); | ||
* `object`:`<*>` object for calculating hash; | ||
* `options`:`<object>` object with options; | ||
* `options.alg`:`<string>` - hash algorithm (default: `'sha256'`); | ||
* `options.enc`:`<string>` - hash encoding (default: `'hex'`); | ||
#### `sortedObjectString(object[, options])` | ||
#### `sortObject(object)` | ||
Returns sorted string generated from object | ||
* `object` object for calculating hash; | ||
* `options` object with options; | ||
* `options.coerce` if true performs type coercion (default: `true`); | ||
* `options.sort` if true performs sorting on objects, arrays and Sets (default: `true`); | ||
* `object`:`<*>` - object for sorting; | ||
### Requirements | ||
- `>=nodejs-0.10.0` (starting from version 1.0.0) | ||
- `>=nodejs-6.0.0` | ||
- `>=nodejs-4.0.0` (requires to run node with `--harmony` flag) | ||
- nodejs `crypto` module | ||
### Example | ||
```js | ||
const {hash} = require('node-object-hash'); | ||
var hasher = require('node-object-hash'); | ||
const object = { | ||
x: new Map([['a', 1], ['c', 3], ['b', 2]]), | ||
z: new Set([4,3,2,1]), | ||
f: 2, | ||
e: [{a:2}, 3, {g:2, e:{d:1}}, "rt", "1", "2abc", "3d"], | ||
d: [3,5,6,1,2,3], | ||
c: "3ab", | ||
b: "2", | ||
a: 1 | ||
var hashSortCoerce = hasher({sort:true, coerce:true}); | ||
// or | ||
// var hashSortCoerce = hasher(); | ||
// or | ||
// var hashSort = hasher({sort:true, coerce:false}); | ||
// or | ||
// var hashCoerce = hasher({sort:false, coerce:true}); | ||
var objects = { | ||
a: { | ||
a: [{c: 2, a: 1, b: {a: 3, c: 2, b: 0}}], | ||
b: [1, 'a', {}, null], | ||
}, | ||
b: { | ||
b: ['a', 1, {}, undefined], | ||
a: [{c: '2', b: {b: false, c: 2, a: '3'}, a: true}] | ||
}, | ||
c: ['4', true, 0, 2, 3] | ||
}; | ||
const hashString = hash(object); | ||
const hashStringSha512 = hash(object, {alg:'sha512'}); | ||
hashSortCoerce.hash(objects.a) === hashSortCoerce.hash(objects.b); | ||
// returns true | ||
hashSortCoerce.sortObject(object.c) | ||
// returns '[0,1,2,3,4]' | ||
``` | ||
### Compared to same libraries | ||
### Benchmark results | ||
Bench data - array of 100000 complex objects | ||
#### Usage | ||
`npm run bench` | ||
#### Results | ||
| | node-object-hash-0.2.1 | node-object-hash-1.0.0 | object-hash-1.1.4 | | ||
|---|---|---|---| | ||
| Time | 5773.869ms | 2961.812ms | 534528.254ms | | ||
| Memory | ~35Mb | ~33Mb | ~41Mb | | ||
### Similar libraries | ||
* https://www.npmjs.com/package/object-hash | ||
* Pros: | ||
* node-object-hash has twice smaller memory footprint | ||
* five times faster | ||
* ~~no memory leak when using in sync flow~~ (fixed in last version of `object-hash`) | ||
* Cons: | ||
* Only `>=node-4.0.0` supported | ||
### License | ||
ISC |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
18207
8
466
1
109
4
1