node-object-hash
Advanced tools
Comparing version 1.4.2 to 2.0.0-rc.1
# Changelog | ||
## v1.3.X -> 1.4.X | ||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. | ||
* Add support for objects without constructor #11 [PR @futpib](https://github.com/SkeLLLa/node-object-hash/pull/12) | ||
* Simplify eslint rules, update codestyle | ||
* Fix npm links issues in readme | ||
* Update dev dependencies | ||
## [2.0.0-rc.1](https://gitlab.com/m03geek/node-object-hash/compare/v2.0.0-rc.0...v2.0.0-rc.1) (2019-09-03) | ||
## v1.2.X -> 1.3.X | ||
## [2.0.0-rc.0](https://gitlab.com/m03geek/node-object-hash/compare/v1.4.2...v2.0.0-rc.0) (2019-09-03) | ||
* Add definition types to support typescript | ||
* Add >=node-8.0.0 support in tests. | ||
## v1.1.X -> 1.2.X | ||
### ⚠ BREAKING CHANGES | ||
Sorter refactoring and performance improvements | ||
* See readme | ||
- Added typed arrays support | ||
- Added primitive type constructors support | ||
- Add more docs about type mapping and type coercion | ||
### Bug Fixes | ||
## v1.0.X -> v1.1.X | ||
* **hasher:** fix options ([05241ca](https://gitlab.com/m03geek/node-object-hash/commit/05241ca)) | ||
Mainly all changes affected codestyle and documentation to provide better | ||
experience using this library. There are no changes that should affect | ||
functionality. | ||
- Renamed `sortObject` function to `sort` (old one is still present in code | ||
for backward compatibility). | ||
- Performed some refactoring for better codestyle and documentation. | ||
- Old version (`0.X.X`) moved to subfolder (`./v0`). | ||
- Advanced API reference added: [link](API.md). | ||
### Features | ||
## v0.X.X -> v1.0.X | ||
- 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);``` | ||
In case if you still need an old 0.x.x version it's available in `hash.js` | ||
file. | ||
* major refactor ([450471e](https://gitlab.com/m03geek/node-object-hash/commit/450471e)) |
{ | ||
"name": "node-object-hash", | ||
"version": "1.4.2", | ||
"version": "2.0.0-rc.1", | ||
"description": "Node.js object hash library with properties/arrays sorting to provide constant hashes", | ||
"main": "index.js", | ||
"homepage": "https://github.com/SkeLLLa/node-object-hash", | ||
"main": "dist/hasher.js", | ||
"types": "dist/hasher.d.ts", | ||
"directories": { | ||
"test": "test" | ||
"test": "test", | ||
"lib": "dist", | ||
"doc": "docs" | ||
}, | ||
"files": [ | ||
"dist/**/*.js", | ||
"dist/**/*.ts", | ||
"dist/**/*.map" | ||
], | ||
"husky": { | ||
"hooks": { | ||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS" | ||
} | ||
}, | ||
"standard-version": { | ||
"scripts": { | ||
"prerelease": "npm run test", | ||
"postbump": "echo Version: v$npm_package_version", | ||
"postchangelog": "replace \"https://github.com/SkeLLLa/\" \"https://gitlab.com/m03geek/\" CHANGELOG.md", | ||
"posttag": "git push --follow-tags origin $npm_package_config_releaseBranch" | ||
} | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@types/node": "^10.0.0", | ||
"benchmark": "^2.1.4", | ||
"chai": "^4.1.2", | ||
"eslint": "^4.19.1", | ||
"eslint-config-google": "^0.9.1", | ||
"faker": "^4.1.0", | ||
"hash-object": "^0.1.7", | ||
"istanbul": "^0.4.5", | ||
"jsdoc-to-markdown": "^4.0.0", | ||
"mocha": "^5.2.0", | ||
"object-hash": "^1.3.0", | ||
"typescript": "^3.2.4" | ||
"@commitlint/cli": "^8.0.0", | ||
"@commitlint/config-conventional": "^8.0.0", | ||
"@types/jest": "^24.0.15", | ||
"@types/node": "^12.7.3", | ||
"codecov": "^3.5.0", | ||
"eslint": "^6.3.0", | ||
"eslint-config-google": "^0.14.0", | ||
"faker": "^4.0.0", | ||
"husky": "^3.0.5", | ||
"jest": "^24.7.1", | ||
"replace": "^1.1.0", | ||
"standard-version": "^7.0.0", | ||
"ts-jest": "^24.0.2", | ||
"ts-node": "^8.3.0", | ||
"tslint": "^5.18.0", | ||
"tslint-eslint-rules": "^5.4.0", | ||
"typedoc": "^0.15.0", | ||
"typedoc-plugin-markdown": "^2.1.9", | ||
"typescript": "^3.6.2" | ||
}, | ||
"scripts": { | ||
"test": "mocha", | ||
"lint": "eslint .", | ||
"cover": "istanbul cover _mocha", | ||
"bench": "node --expose-gc ./bench/index.js", | ||
"bench2": "node --expose-gc ./bench/bench.js", | ||
"jsdoc": "jsdoc2md -t ./README.hbs --private 'objectSorter.js' 'index.js' > README.md && jsdoc2md --private 'objectSorter.js' 'index.js' > API.md", | ||
"tsc": "tsc index.d.ts objectSorter.d.ts --noImplicitAny", | ||
"outdated": "npm outdate || true" | ||
"lint:eslint": "eslint .", | ||
"lint:tslint": "tslint -p ./ && tslint -p ./test", | ||
"lint": "npm run lint:eslint && npm run lint:tslint", | ||
"lint:fix": "eslint . --fix && tslint -p ./ --fix && tslint -p ./test --fix", | ||
"test": "npm audit || true && npm run lint && npm run unit", | ||
"unit": "jest --coverage", | ||
"postunit": "codecov", | ||
"prebenchmark": "npm i --no-save hash-object object-hash benchmark", | ||
"benchmark": "node benchmark/bench.js", | ||
"prebenchmark:custom": "npm i --no-save hash-object object-hash benchmark", | ||
"benchmark:custom": "node --expose-gc benchmark/custom.js", | ||
"build": "npm run build:node", | ||
"build:node": "tsc -p tsconfig.json", | ||
"prerelease": "npm run typedoc && git add -A ./docs", | ||
"release": "git add -A && standard-version -a", | ||
"typedoc": "rm -rf ./docs/* && typedoc --excludeExternals --name \"$npm_package_name\" --readme none --mode file --includeDeclarations --excludePrivate --theme markdown --out ./docs" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:SkeLLLa/node-object-hash.git" | ||
"url": "https://github.com/SkeLLLa/node-object-hash.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/SkeLLLa/node-object-hash/issues" | ||
"url": "https://gitlab.com/m03geek/node-object-hash/issues" | ||
}, | ||
"homepage": "https://gitlab.com/m03geek/node-object-hash#README", | ||
"keywords": [ | ||
@@ -49,8 +86,4 @@ "hash", | ||
], | ||
"author": { | ||
"name": "Alexander Kureniov", | ||
"email": "s@zxc.pp.ua", | ||
"url": "https://github.com/SkeLLLa" | ||
}, | ||
"license": "ISC", | ||
"author": "m03geek", | ||
"license": "MIT", | ||
"engines": { | ||
@@ -57,0 +90,0 @@ "node": ">=0.10.0" |
374
README.md
@@ -7,20 +7,80 @@ # node-object-hash | ||
Hashes are built on top of node's crypto module | ||
(so for using in browser use something like [browserify-crypto](https://github.com/crypto-browserify/crypto-browserify) or some kind of crypto functions polyfills). Or you can use only `objectSorter` ([source](https://github.com/SkeLLLa/node-object-hash/blob/master/objectSorter.js)) for getting your objects' string representation and compare or pass them to your own hash function. | ||
Hashes are built on top of node's crypto module. If you want to use it in browser it's recommented to use `objectSorter` only. It will provide you with unique string representation of your object. Afterwards you may use some hash library to reduce string size. Also you may use something like [browserify-crypto](https://github.com/crypto-browserify/crypto-browserify) or some kind of crypto functions polyfills. | ||
[![node](https://img.shields.io/node/v/node-object-hash.svg?maxAge=21600&style=flat-square)](https://nodejs.org/download/release/latest)[![NPM](https://img.shields.io/npm/v/node-object-hash.svg?maxAge=21600&style=flat-square)](https://npmjs.org/package/node-object-hash)[![NPM Downloads](https://img.shields.io/npm/dt/node-object-hash.svg?maxAge=21600&style=flat-square)](https://npmjs.org/package/node-object-hash)[![npms.io Score](https://badges.npms.io/node-object-hash.svg?style=flat-square)](https://npms.io/search?q=node-object-hash)[![Build Status](https://img.shields.io/travis/SkeLLLa/node-object-hash.svg?maxAge=21600&branch=master&style=flat-square)](https://travis-ci.org/SkeLLLa/node-object-hash)[![Known Vulnerabilities](https://snyk.io/test/github/SkeLLLa/node-object-hash/badge.svg?maxAge=21600&style=flat-square)](https://snyk.io/test/github/skellla/node-object-hash)[![Coverage](https://api.codeclimate.com/v1/badges/199467889caf98d59690/test_coverage)](https://codeclimate.com/github/SkeLLLa/node-object-hash/test_coverage)[![Maintainability](https://api.codeclimate.com/v1/badges/199467889caf98d59690/maintainability)](https://codeclimate.com/github/SkeLLLa/node-object-hash/maintainability)[![Inline docs](http://inch-ci.org/github/SkeLLLa/node-object-hash.svg?branch=master&style=flat-square)](http://inch-ci.org/github/SkeLLLa/node-object-hash)[![Analytics](https://ga-beacon.appspot.com/UA-90571586-1/node-object-hash?pixel&useReferer)](https://github.com/igrigorik/ga-beacon) | ||
[![Node](https://img.shields.io/node/v/node-object-hash.svg)](https://nodejs.org/download/release/latest) | ||
[![NPM Version](https://img.shields.io/npm/v/node-object-hash.svg)](https://www.npmjs.com/package/node-object-hash) | ||
[![Downloads Count](https://img.shields.io/npm/dm/node-object-hash.svg)](https://www.npmjs.com/package/node-object-hash) | ||
[![Vunerabilities Count](https://snyk.io/test/npm/node-object-hash/badge.svg)](https://www.npmjs.com/package/node-object-hash) | ||
[![Npms.io Score](https://badges.npms.io/node-object-hash.svg)](https://npms.io/search?q=node-object-hash) | ||
[![Build Status](https://gitlab.com/m03geek/node-object-hash/badges/master/pipeline.svg)](https://gitlab.com/m03geek/node-object-hash/commits/master) | ||
[![License](https://img.shields.io/npm/l/node-object-hash.svg)](https://gitlab.com/m03geek/node-object-hash/blob/master/LICENSE) | ||
[![Codecov Coverage](https://img.shields.io/codecov/c/gl/m03geek/node-object-hash.svg)](https://codecov.io/gl/m03geek/node-object-hash) | ||
[![LGTM Alerts](https://img.shields.io/lgtm/alerts/github/SkeLLLa/node-object-hash.svg)](https://lgtm.com/projects/g/SkeLLLa/node-object-hash/) | ||
[![LGTM Grade](https://img.shields.io/lgtm/grade/javascript/github/SkeLLLa/node-object-hash.svg)](https://lgtm.com/projects/g/SkeLLLa/node-object-hash/) | ||
# Installation | ||
## ToC | ||
- [node-object-hash](#node-object-hash) | ||
- [ToC](#toc) | ||
- [What's new in v2.0.0](#whats-new-in-v200) | ||
- [Breaking changes](#breaking-changes) | ||
- [New features](#new-features) | ||
- [Installation](#installation) | ||
- [Features](#features) | ||
- [Type map](#type-map) | ||
- [Coercion map](#coercion-map) | ||
- [Changes](#changes) | ||
- [Docs](#docs) | ||
- [API overview](#api-overview) | ||
- [Constructor](#constructor) | ||
- [API methods](#api-methods) | ||
- [`hash(object[, options])`](#hashobject-options) | ||
- [`sort(object)`](#sortobject) | ||
- [Requirements](#requirements) | ||
- [version \>=1.0.0](#version-100) | ||
- [version \>=0.1.0 && <1.0.0](#version-010--100) | ||
- [Examples](#examples) | ||
- [Benchmarks](#benchmarks) | ||
- [Usage](#usage) | ||
- [Results](#results) | ||
- [Custom benchmark (code)](#custom-benchmark-code) | ||
- [Benchmark suite module (code)](#benchmark-suite-module-code) | ||
- [Links](#links) | ||
- [License](#license) | ||
## What's new in v2.0.0 | ||
### Breaking changes | ||
* Library rewritten in typescript that could cause some side-effects, but it should not. | ||
* With `coerce=false` `Set`s will no longer generate the same hashes as `Array`s. In order to restore previous behavior set `coerce.set=true`. | ||
* With `coerce=false` `Symbol`s will generate hash based on symbol `.toString` value. That's useful for `Symbol.for('smth')`. If `coerce.symbol=true` all `Symbols`s will have equal hashes. | ||
TLDR; If you use library with `Set`s or `Symbol`s with `coerce=false` in order to keep hashes the same as in `v1.X.X` you should use following constructor: | ||
``` | ||
const hasher = require('node-object-hash')({coerce: {set: true, symbol: true}}) | ||
``` | ||
* Object sorter sources moved to `dist` directory. If you required it directly via `require('node-object-hash/objectSorter')` you should change it to require('node-object-hash/dist/objectSorter'). | ||
* Removed old `v0` version from code. | ||
* Changed license to MIT. | ||
### New features | ||
* New granular options. Now you can specify what types need to be sorted or coerced. | ||
* Add new `trim` option. It can be used to remove unncecessary spaces in `string`s or `function` bodies. | ||
* Library rewritten to typescript, so it may have better ts compatibility. | ||
## Installation | ||
`npm i node-object-hash -S` | ||
# Features | ||
## Features | ||
- Supports object property sorting for constant hashes for objects with same properties, but different order. | ||
- Supports ES6 Maps and Sets. | ||
- Supports type coercion (see table below) | ||
- Supports all hashes and encodings of crypto library | ||
- Supports large objects and arrays | ||
- Very fast comparing to other libs (see [Benchmarks](#benchmarks) section) | ||
- Supports type coercion (see table below). | ||
- Supports all hashes and encodings of crypto library. | ||
- Supports large objects and arrays. | ||
- Has granular options that allows to control what should be sorted or coerced. | ||
- Very fast comparing to other libs (see [Benchmarks](#benchmarks) section). | ||
## Type map | ||
### Type map | ||
@@ -73,3 +133,3 @@ This map displays what types will have identical string representation (e.g. new Set([1, 2, 3]) and [1, 2, 3] will have | ||
## Coercion map | ||
### Coercion map | ||
| Initial "type" | Coerced type | Example | | ||
@@ -83,10 +143,18 @@ |----------------|----------------|--------------| | ||
# Changes | ||
## Changes | ||
See [changelog](CHANGELOG.md) | ||
# API overview | ||
## Docs | ||
## Constructor `require('node-object-hash')([options])` | ||
Full API docs could be found in [docs](./docs/README.md). | ||
### API overview | ||
#### Constructor | ||
```js | ||
require('node-object-hash')([options]) | ||
``` | ||
Returns preconfigured object with API | ||
@@ -96,11 +164,12 @@ | ||
* `options`:`object` - object with hasher config options | ||
* `options.coerce`:`boolean` - if true performs type coercion (default: `true`); | ||
* `options.coerce`:`boolean|object` - 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.sort`:`boolean|object` - if true performs sorting on objects, arrays, etc. (default: `true`); | ||
* `options.trim`:`boolean|object` - if true performs trim of spaces and replaces space-like characters with single space (default: `false`); | ||
* `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; | ||
## API methods | ||
### API methods | ||
### `hash(object[, options])` | ||
#### `hash(object[, options])` | ||
@@ -113,3 +182,3 @@ Returns hash string. | ||
### `sort(object)` | ||
#### `sort(object)` | ||
@@ -119,233 +188,13 @@ Returns sorted string generated from object (can be used for object comparison) | ||
# Full API docs ([separate page](API.md)) | ||
## Requirements | ||
## Modules | ||
<dl> | ||
<dt><a href="#module_node-object-hash/objectSorter">node-object-hash/objectSorter</a> : <code><a href="#module_node-object-hash/objectSorter..makeObjectSorter..objectToString">objectToString</a></code></dt> | ||
<dd><p>Object sorter module. | ||
It provides object sorter function constructor.</p> | ||
</dd> | ||
<dt><a href="#module_node-object-hash">node-object-hash</a> : <code><a href="#module_node-object-hash..apiConstructor">apiConstructor</a></code></dt> | ||
<dd><p>Node object hash module. | ||
It provides a methods that return object hash or sorted object string.</p> | ||
</dd> | ||
</dl> | ||
<a name="module_node-object-hash/objectSorter"></a> | ||
## node-object-hash/objectSorter : [<code>objectToString</code>](#module_node-object-hash/objectSorter..makeObjectSorter..objectToString) | ||
Object sorter module. | ||
It provides object sorter function constructor. | ||
* [node-object-hash/objectSorter](#module_node-object-hash/objectSorter) : [<code>objectToString</code>](#module_node-object-hash/objectSorter..makeObjectSorter..objectToString) | ||
* [~_guessObjectType(obj)](#module_node-object-hash/objectSorter.._guessObjectType) ⇒ <code>string</code> ℗ | ||
* [~_guessType(obj)](#module_node-object-hash/objectSorter.._guessType) ⇒ <code>string</code> ℗ | ||
* [~makeObjectSorter([options])](#module_node-object-hash/objectSorter..makeObjectSorter) ⇒ [<code>objectToString</code>](#module_node-object-hash/objectSorter..makeObjectSorter..objectToString) ℗ | ||
* [~objectToString(obj)](#module_node-object-hash/objectSorter..makeObjectSorter..objectToString) ⇒ <code>string</code> ℗ | ||
<a name="module_node-object-hash/objectSorter.._guessObjectType"></a> | ||
### node-object-hash/objectSorter~_guessObjectType(obj) ⇒ <code>string</code> ℗ | ||
Guesses object's type | ||
**Kind**: inner method of [<code>node-object-hash/objectSorter</code>](#module_node-object-hash/objectSorter) | ||
**Returns**: <code>string</code> - Object type | ||
**Access**: private | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| obj | <code>Object</code> | Object to guess type | | ||
**Example** | ||
```js | ||
var a = []; | ||
_guessObjectType(a) === 'array'; // true | ||
``` | ||
<a name="module_node-object-hash/objectSorter.._guessType"></a> | ||
### node-object-hash/objectSorter~_guessType(obj) ⇒ <code>string</code> ℗ | ||
Guesses variable type | ||
**Kind**: inner method of [<code>node-object-hash/objectSorter</code>](#module_node-object-hash/objectSorter) | ||
**Returns**: <code>string</code> - Variable type | ||
**Access**: private | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| obj | <code>\*</code> | Variable to guess type | | ||
**Example** | ||
```js | ||
var a = ''; | ||
_guessType(a) === 'string'; // true | ||
``` | ||
<a name="module_node-object-hash/objectSorter..makeObjectSorter"></a> | ||
### node-object-hash/objectSorter~makeObjectSorter([options]) ⇒ [<code>objectToString</code>](#module_node-object-hash/objectSorter..makeObjectSorter..objectToString) ℗ | ||
Creates object sorter function | ||
**Kind**: inner method of [<code>node-object-hash/objectSorter</code>](#module_node-object-hash/objectSorter) | ||
**Returns**: [<code>objectToString</code>](#module_node-object-hash/objectSorter..makeObjectSorter..objectToString) - Object sorting function | ||
**Access**: private | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [options] | <code>Object</code> | | Sorter options | | ||
| [options.coerce] | <code>boolean</code> | <code>true</code> | Performs type coercion | | ||
| [options.sort] | <code>boolean</code> | <code>true</code> | Performs array, object, etc. sorting | | ||
**Example** | ||
```js | ||
// with coercion | ||
var sorter = makeObjectSorter({coerce: true, sort: false}); | ||
sorter(1) === "1"; // true | ||
// with sort | ||
var sorter = makeObjectSorter({coerce: false, sort: true}); | ||
sorter([2, 3, 1]) === [1, 2, 3]; // true | ||
``` | ||
<a name="module_node-object-hash/objectSorter..makeObjectSorter..objectToString"></a> | ||
#### makeObjectSorter~objectToString(obj) ⇒ <code>string</code> ℗ | ||
Object sorting function | ||
**Kind**: inner method of [<code>makeObjectSorter</code>](#module_node-object-hash/objectSorter..makeObjectSorter) | ||
**Returns**: <code>string</code> - Sorted string | ||
**Access**: private | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| obj | <code>Object</code> | Object to sort | | ||
<a name="module_node-object-hash"></a> | ||
## node-object-hash : [<code>apiConstructor</code>](#module_node-object-hash..apiConstructor) | ||
Node object hash module. | ||
It provides a methods that return object hash or sorted object string. | ||
* [node-object-hash](#module_node-object-hash) : [<code>apiConstructor</code>](#module_node-object-hash..apiConstructor) | ||
* _instance_ | ||
* [.sort(obj)](#module_node-object-hash+sort) ⇒ <code>string</code> | ||
* [.hash(obj, [opts])](#module_node-object-hash+hash) ⇒ <code>string</code> | ||
* _inner_ | ||
* [~apiConstructor([options])](#module_node-object-hash..apiConstructor) ⇒ [<code>API</code>](#module_node-object-hash..API) | ||
* [~API](#module_node-object-hash..API) : <code>Object</code> | ||
<a name="module_node-object-hash+sort"></a> | ||
### node-object-hash.sort(obj) ⇒ <code>string</code> | ||
Creates sorted string from given object | ||
**Kind**: instance method of [<code>node-object-hash</code>](#module_node-object-hash) | ||
**Returns**: <code>string</code> - Sorted object string | ||
**Access**: public | ||
**See**: [objectToString](#module_node-object-hash/objectSorter..makeObjectSorter..objectToString) | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| obj | <code>\*</code> | JS object to be sorted | | ||
**Example** | ||
```js | ||
var apiConstructor = require('node-object-hash'); | ||
var sorter = apiConstructor({sort:true, coerce:true}).sort; | ||
sort({b: {b: 1, d: 'x'}, c: 2, a: [3, 5, 1]}); | ||
// "{a:[1,3,5],b:{b:1,d:x},c:2}" | ||
``` | ||
<a name="module_node-object-hash+hash"></a> | ||
### node-object-hash.hash(obj, [opts]) ⇒ <code>string</code> | ||
Creates hash from given object | ||
**Kind**: instance method of [<code>node-object-hash</code>](#module_node-object-hash) | ||
**Returns**: <code>string</code> - Object hash value | ||
**Access**: public | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| obj | <code>\*</code> | | JS object to hash | | ||
| [opts] | <code>Object</code> | | Options | | ||
| [opts.alg] | <code>string</code> | <code>"sha256"</code> | Crypto algorithm to use | | ||
| [opts.enc] | <code>string</code> | <code>"hex"</code> | Hash string encoding | | ||
**Example** | ||
```js | ||
var apiConstructor = require('node-object-hash'); | ||
var hasher = apiConstructor({sort:true, coerce:true}).hash; | ||
hash({b: {b: 1, d: 'x'}, c: 2, a: [3, 5, 1]}); | ||
// "4c18ce0dcb1696b329c8568d94a9830da810437d8c9e6cecf5d969780335a26b" | ||
``` | ||
<a name="module_node-object-hash..apiConstructor"></a> | ||
### node-object-hash~apiConstructor([options]) ⇒ [<code>API</code>](#module_node-object-hash..API) | ||
Generates node-object-hash API object | ||
**Kind**: inner method of [<code>node-object-hash</code>](#module_node-object-hash) | ||
**Returns**: [<code>API</code>](#module_node-object-hash..API) - Node object hash API instance | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [options] | <code>Object</code> | | Library options | | ||
| [options.coerce] | <code>boolean</code> | <code>true</code> | Performs type coercion | | ||
| [options.sort] | <code>boolean</code> | <code>true</code> | Performs array, object, etc. sorting | | ||
| [options.alg] | <code>string</code> | <code>"sha256"</code> | Default crypto algorithm to use (can be overridden) | | ||
| [options.enc] | <code>string</code> | <code>"hex"</code> | Hash string encoding (can be overridden) | | ||
**Example** | ||
```js | ||
var apiConstructor = require('node-object-hash'); | ||
var hashSortCoerce = apiConstructor({sort:true, coerce:true}); | ||
// or | ||
var hashSort = apiConstructor({sort:true, coerce:false}); | ||
// or | ||
var hashCoerce = apiConstructor({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] | ||
}; | ||
hashSortCoerce.hash(objects.a) === hashSortCoerce.hash(objects.b); | ||
// returns true | ||
hashSortCoerce.sort(object.c); | ||
// returns '[0,1,2,3,4]' | ||
``` | ||
<a name="module_node-object-hash..API"></a> | ||
### node-object-hash~API : <code>Object</code> | ||
Node object hash API object | ||
**Kind**: inner typedef of [<code>node-object-hash</code>](#module_node-object-hash) | ||
**Properties** | ||
| Name | Type | Description | | ||
| --- | --- | --- | | ||
| hash | <code>function</code> | Returns object hash string (see [hash](#module_node-object-hash+hash)) | | ||
| sort | <code>function</code> | Returns sorted object string (see [sort](#module_node-object-hash+sort)) | | ||
# Requirements | ||
## version \>=1.0.0 | ||
### version \>=1.0.0 | ||
- `>=nodejs-0.10.0` | ||
## version \>=0.1.0 && <1.0.0 | ||
### version \>=0.1.0 && <1.0.0 | ||
- `>=nodejs-6.0.0` | ||
- `>=nodejs-4.0.0` (requires to run node with `--harmony` flag) | ||
## browsers | ||
- nodejs `crypto` module for browsers (e.g. [browserify-crypto](https://github.com/crypto-browserify/crypto-browserify)). | ||
## Examples | ||
# Example | ||
```js | ||
@@ -381,17 +230,16 @@ var hasher = require('node-object-hash'); | ||
For more examples you can see [tests file](https://github.com/SkeLLLa/node-object-hash/blob/master/test/hash2.js) | ||
or try it out online at [runkit](https://runkit.com/skellla/node-object-hash-example) | ||
For more examples you can see [tests](./test) or try it out online at [runkit](https://runkit.com/skellla/node-object-hash-example) | ||
# Benchmarks | ||
## Benchmarks | ||
Bench data - array of 100000 complex objects | ||
## Usage | ||
### Usage | ||
* `npm run bench` to run custom benchmark | ||
* `npm run bench2` to run benchmark suite | ||
* `npm run benchmark` to run benchmark suite | ||
## Results | ||
### Results | ||
### Custom benchmark ([code](bench/index.js)) | ||
#### Custom benchmark ([code](bench/index.js)) | ||
@@ -404,2 +252,3 @@ | Library | Time (ms) | Memory (Mb) | | ||
| node-object-hash-1.2.X (node v7) | 2390.752 | 28 | | ||
| node-object-hash-2.X.X (node v12) | 1990.622 | 24 | | ||
| object-hash-1.1.5 (node v7) | 28115.553 | 39 | | ||
@@ -410,26 +259,17 @@ | object-hash-1.1.4 | 534528.254 | 41 | | ||
### Benchmark suite module ([code](bench/bench.js)) | ||
#### Benchmark suite module ([code](bench/bench.js)) | ||
| Library (node v7) | Perf (ops/s) | | ||
| Library (node v12) | Perf (ops/s) | | ||
|------------------------|--------------| | ||
| node-object-hash-0.2.1 | 540 ±1.34% | | ||
| node-object-hash-1.1.X | 844 ±2.51% | | ||
| node-object-hash-1.2.X | 1021 ±1.81% | | ||
| object-hash-1.1.5 | 106 ±0.88% | | ||
| hash-object-0.1.7 | 305 ±1.66% | | ||
| node-object-hash-2.0.0 | 2087 ±0.59% | | ||
| object-hash-1.3.1 | 239 ±0.39% | | ||
| hash-object-0.1.7 | 711 ±0.18% | | ||
| Library (node v10) | Perf (ops/s) | | ||
|------------------------|--------------| | ||
| node-object-hash-0.X.X | 758 ±1.40% | | ||
| node-object-hash-1.4.X | 1266 ±1.44% | | ||
| object-hash-1.3.0 | 132 ±4.08% | | ||
| hash-object-0.1.7 | 378 ±1.36% | | ||
### Links | ||
## Links | ||
* [object-hash](https://www.npmjs.com/package/object-hash) - Slow, useful for browsers because it not uses node's crypto library | ||
* [hash-object](https://www.npmjs.com/package/hash-object) - no ES6 types support | ||
# License | ||
## License | ||
ISC | ||
MIT |
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
61439
20
1070
19
1
267
1