API.md 8.5 KB

Modules

node-object-hash/objectSorter : objectToString

Object sorter module. It provides object sorter function constructor.

node-object-hash : apiConstructor

Node object hash module. It provides a methods that return object hash or sorted object string.

node-object-hash/objectSorter : objectToString

Object sorter module. It provides object sorter function constructor.

node-object-hash/objectSorter~_guessObjectType(obj) ⇒ string

Guesses object's type

Kind: inner method of node-object-hash/objectSorter
Returns: string - Object type
Access: private

Param Type Description
obj Object Object to guess type

Example

var a = [];
_guessObjectType(a) === 'array'; // true

node-object-hash/objectSorter~_guessType(obj) ⇒ string

Guesses variable type

Kind: inner method of node-object-hash/objectSorter
Returns: string - Variable type
Access: private

Param Type Description
obj * Variable to guess type

Example

var a = '';
_guessType(a) === 'string'; // true

node-object-hash/objectSorter~makeObjectSorter([options]) ⇒ objectToString

Creates object sorter function

Kind: inner method of node-object-hash/objectSorter
Returns: objectToString - Object sorting function
Access: private

Param Type Default Description
[options] Object Sorter options
[options.coerce] boolean true Performs type coercion
[options.sort] boolean true Performs array, object, etc. sorting

Example

// 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

makeObjectSorter~objectToString(obj) ⇒ string

Object sorting function

Kind: inner method of makeObjectSorter
Returns: string - Sorted string
Access: private

Param Type Description
obj Object Object to sort

node-object-hash : apiConstructor

Node object hash module. It provides a methods that return object hash or sorted object string.

node-object-hash.sort(obj) ⇒ string

Creates sorted string from given object

Kind: instance method of node-object-hash
Returns: string - Sorted object string
Access: public
See: objectToString

Param Type Description
obj * JS object to be sorted

Example

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}"

node-object-hash.hash(obj, [opts]) ⇒ string

Creates hash from given object

Kind: instance method of node-object-hash
Returns: string - Object hash value
Access: public

Param Type Default Description
obj * JS object to hash
[opts] Object Options
[opts.alg] string "sha256" Crypto algorithm to use
[opts.enc] string "hex" Hash string encoding

Example

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"

node-object-hash~apiConstructor([options]) ⇒ API

Generates node-object-hash API object

Kind: inner method of node-object-hash
Returns: API - Node object hash API instance

Param Type Default Description
[options] Object Library options
[options.coerce] boolean true Performs type coercion
[options.sort] boolean true Performs array, object, etc. sorting
[options.alg] string "sha256" Default crypto algorithm to use (can be overridden)
[options.enc] string "hex" Hash string encoding (can be overridden)

Example

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]'

node-object-hash~API : Object

Node object hash API object

Kind: inner typedef of node-object-hash
Properties

Name Type Description
hash function Returns object hash string (see hash)
sort function Returns sorted object string (see sort)