Socket
Socket
Sign inDemoInstall

node-object-hash

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-object-hash - npm Package Compare versions

Comparing version 2.3.10 to 3.0.0

87

dist/hasher.d.ts
/// <reference types="node" />
import objectSorter from './objectSorter';
import { BinaryToTextEncoding } from 'crypto';
declare namespace hasher {
import type { BinaryToTextEncoding } from 'crypto';
import type { SorterOptions } from './objectSorter';
/**
* Object hasher options
*/
export interface HasherOptions extends SorterOptions {
/**
* Object hasher options
* Hash algorithm to use
* @default 'sha256'
*/
interface HasherOptions extends objectSorter.SorterOptions {
/**
* Hash algorithm to use
* @default 'sha256'
*/
alg?: string;
/**
* String encoding for hash
* @default 'hex'
*/
enc?: BinaryToTextEncoding;
}
alg?: string;
/**
* If object implements Hashable interface then value from toHash
* will be used for hash function. It means that the different objects
* with the function toHash that return the same value will have the same hash
* String encoding for hash
* @default 'hex'
*/
interface Hashable {
toHashableString: () => string;
}
interface Hasher {
/**
* Create hash of an object
* @param object source object
* @returns hash string of an object
*/
hash(object: Hashable | any, opts?: hasher.HasherOptions): string;
/**
* Create sorted string from an object
* @param object source object
* @returns sorted string from an object
*/
sort(object: any): string;
/**
* Create sorted string from an object
* @param object source object
* @returns sorted string from an object
* @alias sort
*/
sortObject(object: any): string;
}
enc?: BinaryToTextEncoding;
}
/**
* If object implements Hashable interface then value from toHash
* will be used for hash function. It means that the different objects
* with the function toHash that return the same value will have the same hash
*/
export interface Hashable {
toHashableString: () => string;
}
export interface Hasher<T = unknown> {
/**
* Create hash of an object
* @param object source object
* @returns hash string of an object
*/
hash(object: Hashable | T, opts?: HasherOptions): string;
/**
* Create sorted string from an object
* @param object source object
* @returns sorted string from an object
*/
sort(object: T): string;
/**
* Create sorted string from an object
* @param object source object
* @returns sorted string from an object
* @alias sort
*/
sortObject(object: T): string;
}
/**
* Hasher constructor

@@ -55,4 +53,3 @@ * @param options hasher options

*/
declare function hasher(options?: hasher.HasherOptions): hasher.Hasher;
export = hasher;
export declare const hasher: (options?: HasherOptions) => Hasher;
//# sourceMappingURL=hasher.d.ts.map
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var objectSorter_1 = __importDefault(require("./objectSorter"));
var crypto_1 = __importDefault(require("crypto"));
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasher = void 0;
const node_crypto_1 = require("node:crypto");
const objectSorter_1 = require("./objectSorter");
/**
* Default hash algorithm
*/
var DEFAULT_ALG = 'sha256';
const DEFAULT_ALG = 'sha256';
/**
* Default hash string enoding
*/
var DEFAULT_ENC = 'hex';
const DEFAULT_ENC = 'hex';
/**

@@ -20,5 +19,4 @@ * Hasher constructor

*/
function hasher(options) {
if (options === void 0) { options = {}; }
var sortObject = (0, objectSorter_1.default)(options);
const hasher = (options) => {
const sortObject = (0, objectSorter_1.objectSorter)(options);
/**

@@ -30,16 +28,15 @@ * Object hash function

*/
function hashObject(obj, opts) {
if (opts === void 0) { opts = {}; }
var alg = opts.alg || options.alg || DEFAULT_ALG;
var enc = opts.enc || options.enc || DEFAULT_ENC;
var sorted = sortObject(obj);
return crypto_1.default.createHash(alg).update(sorted).digest(enc);
}
const hash = (obj, opts) => {
const alg = opts?.alg || options?.alg || DEFAULT_ALG;
const enc = opts?.enc || options?.enc || DEFAULT_ENC;
const sorted = sortObject(obj);
return (0, node_crypto_1.createHash)(alg).update(sorted).digest(enc);
};
return {
hash: hashObject,
hash,
sort: sortObject,
sortObject: sortObject,
sortObject,
};
}
module.exports = hasher;
};
exports.hasher = hasher;
//# sourceMappingURL=hasher.js.map

@@ -1,182 +0,180 @@

import { Hashable } from './hasher';
declare namespace objectSorter {
import type { Hashable } from './hasher';
/**
* Advanced coerce options
*/
export interface CoerceOptions {
/**
* Advanced coerce options
* If `true` converts booleans to string `1` and `0`
* @example
* // coerce.boolean = true
* true === 1;
* false === '0';
* @example
* // coerce.boolean = true
* true !== 1;
* false !== '0'
* @default true
*/
interface CoerceOptions {
/**
* If `true` converts booleans to string `1` and `0`
* @example
* // coerce.boolean = true
* true === 1;
* false === '0';
* @example
* // coerce.boolean = true
* true !== 1;
* false !== '0'
* @default true
*/
boolean?: boolean;
/**
* If `true` converts numbers to strings
* @example
* // coerce.number = true
* 1 === '1';
* @example
* // coerce.number = false
* 1 !== '1';
* @default true
*/
number?: boolean;
/**
* If `true` converts BigInt to string
* @example
* // coerce.bigint = true
* 1n === '1';
* @example
* // coerce.bigint = false
* 1n !== '1';
* @default true
*/
bigint?: boolean;
/**
* If `true` strings and coerced string will be equal to coerced numbers, booleans, etc
* @example
* // coerce.string = true
* '1' === true
* @example
* // coerce.string = false
* '1' !== 1
* @default true
*/
string?: boolean;
/**
* If `true` undefined will be equal to empty string
* @example
* // coerce.undefined = true
* undefined === ''
* @example
* // coerce.undefined = false
* undefined !== ''
* @default true
*/
undefined?: boolean;
/**
* If `true` null will be equal to empty string
* @example
* // coerce.null = true
* null === ''
* @example
* // coerce.null = false
* null !== ''
* @default true
*/
null?: boolean;
/**
* If `true` all symbols will have eual representation
* @example
* // coerce.symbol = true
* Symbol.for('a') === Symbol.for('b')
* @example
* // coerce.symbol = false
* Symbol.for('a') !== Symbol.for('b')
* @default true
*/
symbol?: boolean;
/**
* If `true` functions may equal the same formatted strings
* @example
* // coerce.function = true
* @example
* // coerce.function = false
* @default true
*/
function?: boolean;
/**
* If `true` dates may equal the same formatted strings
* @example
* // coerce.date = true
* @example
* // coerce.date = false
* @default true
*/
date?: boolean;
/**
* If `true` set will be coerced to array
* @example
* // coerce.set = true
* @example
* // coerce.set = false
* @default true
*/
set?: boolean;
}
boolean?: boolean;
/**
* Advanced sort options
* If `true` converts numbers to strings
* @example
* // coerce.number = true
* 1 === '1';
* @example
* // coerce.number = false
* 1 !== '1';
* @default true
*/
interface SortOptions {
/**
* If `true` sort array entries before hash
*/
array?: boolean;
/**
* If `true` sort TypedArray entries before hash
*/
typedArray?: boolean;
/**
* If `true` sort object entries before hash
*/
object?: boolean;
/**
* If `true` sort set entries before hash
*/
set?: boolean;
/**
* If `true` sort map entries before hash
*/
map?: boolean;
/**
* If `true` sort BigInt entries before hash
*/
bigint?: boolean;
}
number?: boolean;
/**
* Advanced trim options
* If `true` converts BigInt to string
* @example
* // coerce.bigint = true
* 1n === '1';
* @example
* // coerce.bigint = false
* 1n !== '1';
* @default true
*/
interface TrimOptions {
/**
* If `true` replaces multiple space with one and trims whitespaces in strings
*/
string?: boolean;
/**
* If `true` replaces multiple space with one and trims whitespaces in function body
*/
function?: boolean;
}
bigint?: boolean;
/**
* Object sorter options
* If `true` strings and coerced string will be equal to coerced numbers, booleans, etc
* @example
* // coerce.string = true
* '1' === true
* @example
* // coerce.string = false
* '1' !== 1
* @default true
*/
interface SorterOptions {
/**
* If `true` enables type coercion.
* Advanced coerce options could be provided as object
* @default true
*/
coerce?: boolean | CoerceOptions;
/**
* If `true` enables sorting.
* Advanced sorting options could be provided as object
* @default true
*/
sort?: boolean | SortOptions;
/**
* If `true` enables trimming and multiple whitespace replacement.
* Advanced sorting options could be provided as object.
* @default false
*/
trim?: boolean | TrimOptions;
}
type StringifyFn = (obj: Hashable | any) => string;
string?: boolean;
/**
* If `true` undefined will be equal to empty string
* @example
* // coerce.undefined = true
* undefined === ''
* @example
* // coerce.undefined = false
* undefined !== ''
* @default true
*/
undefined?: boolean;
/**
* If `true` null will be equal to empty string
* @example
* // coerce.null = true
* null === ''
* @example
* // coerce.null = false
* null !== ''
* @default true
*/
null?: boolean;
/**
* If `true` all symbols will have eual representation
* @example
* // coerce.symbol = true
* Symbol.for('a') === Symbol.for('b')
* @example
* // coerce.symbol = false
* Symbol.for('a') !== Symbol.for('b')
* @default true
*/
symbol?: boolean;
/**
* If `true` functions may equal the same formatted strings
* @example
* // coerce.function = true
* @example
* // coerce.function = false
* @default true
*/
function?: boolean;
/**
* If `true` dates may equal the same formatted strings
* @example
* // coerce.date = true
* @example
* // coerce.date = false
* @default true
*/
date?: boolean;
/**
* If `true` set will be coerced to array
* @example
* // coerce.set = true
* @example
* // coerce.set = false
* @default true
*/
set?: boolean;
}
/**
* Advanced sort options
*/
export interface SortOptions {
/**
* If `true` sort array entries before hash
*/
array?: boolean;
/**
* If `true` sort TypedArray entries before hash
*/
typedArray?: boolean;
/**
* If `true` sort object entries before hash
*/
object?: boolean;
/**
* If `true` sort set entries before hash
*/
set?: boolean;
/**
* If `true` sort map entries before hash
*/
map?: boolean;
/**
* If `true` sort BigInt entries before hash
*/
bigint?: boolean;
}
/**
* Advanced trim options
*/
export interface TrimOptions {
/**
* If `true` replaces multiple space with one and trims whitespaces in strings
*/
string?: boolean;
/**
* If `true` replaces multiple space with one and trims whitespaces in function body
*/
function?: boolean;
}
/**
* Object sorter options
*/
export interface SorterOptions {
/**
* If `true` enables type coercion.
* Advanced coerce options could be provided as object
* @default true
*/
coerce?: boolean | CoerceOptions | undefined;
/**
* If `true` enables sorting.
* Advanced sorting options could be provided as object
* @default true
*/
sort?: boolean | SortOptions | undefined;
/**
* If `true` enables trimming and multiple whitespace replacement.
* Advanced sorting options could be provided as object.
* @default false
*/
trim?: boolean | TrimOptions | undefined;
}
export type StringifyFn = <T>(obj: Hashable | T) => string;
/**
* Object sorter consturctor

@@ -186,4 +184,3 @@ * @param options object transformation options

*/
declare function objectSorter(options?: objectSorter.SorterOptions): objectSorter.StringifyFn;
export = objectSorter;
export declare const objectSorter: (options?: SorterOptions) => StringifyFn;
//# sourceMappingURL=objectSorter.d.ts.map
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {

@@ -32,4 +25,6 @@ if (k2 === undefined) k2 = k;

};
var typeGuess_1 = require("./typeGuess");
var str = __importStar(require("./stringifiers"));
Object.defineProperty(exports, "__esModule", { value: true });
exports.objectSorter = void 0;
const str = __importStar(require("./stringifiers"));
const typeGuess_1 = require("./typeGuess");
/**

@@ -40,40 +35,44 @@ * Object sorter consturctor

*/
function objectSorter(options) {
if (options === void 0) { options = {}; }
var _a = __assign({ sort: true, coerce: true, trim: false }, options), sort = _a.sort, coerce = _a.coerce, trim = _a.trim;
var stringifiers = {
const objectSorter = (options) => {
const { sort, coerce, trim } = {
sort: true,
coerce: true,
trim: false,
...options,
};
const sortOptions = {
array: typeof sort === 'boolean' ? sort : sort?.array ?? false,
typedArray: typeof sort === 'boolean' ? false : sort?.typedArray ?? false,
object: typeof sort === 'boolean' ? sort : sort?.object ?? false,
set: typeof sort === 'boolean' ? sort : sort?.set ?? false,
map: typeof sort === 'boolean' ? sort : sort?.map ?? false,
};
const coerceOptions = {
boolean: typeof coerce === 'boolean' ? coerce : coerce?.boolean ?? false,
number: typeof coerce === 'boolean' ? coerce : coerce?.number ?? false,
bigint: typeof coerce === 'boolean' ? coerce : coerce?.bigint ?? false,
string: typeof coerce === 'boolean' ? coerce : coerce?.string ?? false,
undefined: typeof coerce === 'boolean' ? coerce : coerce?.undefined ?? false,
null: typeof coerce === 'boolean' ? coerce : coerce?.null ?? false,
symbol: typeof coerce === 'boolean' ? coerce : coerce?.symbol ?? false,
function: typeof coerce === 'boolean' ? coerce : coerce?.function ?? false,
date: typeof coerce === 'boolean' ? coerce : coerce?.date ?? false,
set: typeof coerce === 'boolean' ? coerce : coerce?.set ?? false,
};
const trimOptions = {
string: typeof trim === 'boolean' ? trim : trim?.string ?? false,
function: typeof trim === 'boolean' ? trim : trim?.function ?? false,
};
const stringifiers = {
// eslint-disable-next-line @typescript-eslint/ban-types
unknown: function _unknown(obj) {
var _a, _b;
// `unknonw` - is a typo, saved for backward compatibility
var constructorName = (_b = (_a = obj.constructor) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : 'unknonw';
var objectName = typeof obj.toString === 'function' ? obj.toString() : 'unknown';
return "<:" + constructorName + ">:" + objectName;
const constructorName = obj.constructor?.name ?? 'unknonw';
const objectName = typeof obj.toString === 'function' ? obj.toString() : 'unknown';
return `<:${constructorName}>:${objectName}`;
},
};
var sortOptions = {
array: typeof sort === 'boolean' ? sort : sort.array,
typedArray: typeof sort === 'boolean' ? false : sort.typedArray,
object: typeof sort === 'boolean' ? sort : sort.object,
set: typeof sort === 'boolean' ? sort : sort.set,
map: typeof sort === 'boolean' ? sort : sort.map,
};
var coerceOptions = {
boolean: typeof coerce === 'boolean' ? coerce : coerce.boolean,
number: typeof coerce === 'boolean' ? coerce : coerce.number,
bigint: typeof coerce === 'boolean' ? coerce : coerce.bigint,
string: typeof coerce === 'boolean' ? coerce : coerce.string,
undefined: typeof coerce === 'boolean' ? coerce : coerce.undefined,
null: typeof coerce === 'boolean' ? coerce : coerce.null,
symbol: typeof coerce === 'boolean' ? coerce : coerce.symbol,
function: typeof coerce === 'boolean' ? coerce : coerce.function,
date: typeof coerce === 'boolean' ? coerce : coerce.date,
set: typeof coerce === 'boolean' ? coerce : coerce.set,
};
var trimOptions = {
string: typeof trim === 'boolean' ? trim : trim.string,
function: typeof trim === 'boolean' ? trim : trim.function,
};
stringifiers.hashable = str._hashable.bind(stringifiers);
stringifiers['hashable'] = str._hashable.bind(stringifiers);
if (trimOptions.string) {
stringifiers.string = coerceOptions.string
stringifiers['string'] = coerceOptions.string
? str._stringTrimCoerce.bind(stringifiers)

@@ -83,26 +82,26 @@ : str._stringTrim.bind(stringifiers);

else {
stringifiers.string = coerceOptions.string
stringifiers['string'] = coerceOptions.string
? str._stringCoerce.bind(stringifiers)
: str._string.bind(stringifiers);
}
stringifiers.number = coerceOptions.number
stringifiers['number'] = coerceOptions.number
? str._numberCoerce.bind(stringifiers)
: str._number.bind(stringifiers);
stringifiers.bigint = coerceOptions.bigint
stringifiers['bigint'] = coerceOptions.bigint
? str._bigIntCoerce.bind(stringifiers)
: str._bigInt.bind(stringifiers);
stringifiers.boolean = coerceOptions.boolean
stringifiers['boolean'] = coerceOptions.boolean
? str._booleanCoerce.bind(stringifiers)
: str._boolean.bind(stringifiers);
stringifiers.symbol = coerceOptions.symbol
stringifiers['symbol'] = coerceOptions.symbol
? str._symbolCoerce.bind(stringifiers)
: str._symbol.bind(stringifiers);
stringifiers.undefined = coerceOptions.undefined
stringifiers['undefined'] = coerceOptions.undefined
? str._undefinedCoerce.bind(stringifiers)
: str._undefined.bind(stringifiers);
stringifiers.null = coerceOptions.null
stringifiers['null'] = coerceOptions.null
? str._nullCoerce.bind(stringifiers)
: str._null.bind(stringifiers);
if (trimOptions.function) {
stringifiers.function = coerceOptions.function
stringifiers['function'] = coerceOptions.function
? str._functionTrimCoerce.bind(stringifiers)

@@ -112,17 +111,17 @@ : str._functionTrim.bind(stringifiers);

else {
stringifiers.function = coerceOptions.function
stringifiers['function'] = coerceOptions.function
? str._functionCoerce.bind(stringifiers)
: str._function.bind(stringifiers);
}
stringifiers.date = coerceOptions.date
stringifiers['date'] = coerceOptions.date
? str._dateCoerce.bind(stringifiers)
: str._date.bind(stringifiers);
stringifiers.array = sortOptions.array
stringifiers['array'] = sortOptions.array
? str._arraySort.bind(stringifiers)
: str._array.bind(stringifiers);
stringifiers.typedarray = sortOptions.typedArray
stringifiers['typedarray'] = sortOptions.typedArray
? str._typedArraySort.bind(stringifiers)
: str._typedArray.bind(stringifiers);
if (sortOptions.set) {
stringifiers.set = coerceOptions.set
stringifiers['set'] = coerceOptions.set
? str._setSortCoerce.bind(stringifiers)

@@ -132,10 +131,10 @@ : str._setSort.bind(stringifiers);

else {
stringifiers.set = coerceOptions.set
stringifiers['set'] = coerceOptions.set
? str._setCoerce.bind(stringifiers)
: str._set.bind(stringifiers);
}
stringifiers.object = sortOptions.object
stringifiers['object'] = sortOptions.object
? str._objectSort.bind(stringifiers)
: str._object.bind(stringifiers);
stringifiers.map = sortOptions.map
stringifiers['map'] = sortOptions.map
? str._mapSort.bind(stringifiers)

@@ -151,4 +150,4 @@ : str._map.bind(stringifiers);

return objectToString;
}
module.exports = objectSorter;
};
exports.objectSorter = objectSorter;
//# sourceMappingURL=objectSorter.js.map

@@ -0,1 +1,2 @@

/// <reference types="node" />
/**

@@ -5,4 +6,3 @@ * @private

*/
/// <reference types="node" />
import { Hashable } from './hasher';
import type { Hashable } from './hasher';
import TypedArray = NodeJS.TypedArray;

@@ -12,3 +12,3 @@ /**

*/
export declare type Stringifiers = {
export type Stringifiers = {
[key: string]: (obj: any) => string;

@@ -88,3 +88,3 @@ };

*/
export declare function _bigIntCoerce(obj: BigInt): string;
export declare function _bigIntCoerce(obj: bigint): string;
/**

@@ -96,3 +96,3 @@ * Converts BigInt to string

*/
export declare function _bigInt(obj: BigInt): string;
export declare function _bigInt(obj: bigint): string;
/**

@@ -202,3 +202,3 @@ * Converts boolean to string

*/
export declare function _arraySort(obj: Array<any>): string;
export declare function _arraySort(this: Stringifiers, obj: any[]): string;
/**

@@ -210,3 +210,3 @@ * Converts array to string

*/
export declare function _array(obj: Array<any>): string;
export declare function _array(this: Stringifiers, obj: any[]): string;
/**

@@ -218,3 +218,3 @@ * Converts TypedArray to string

*/
export declare function _typedArraySort(obj: TypedArray): string;
export declare function _typedArraySort(this: Stringifiers, obj: TypedArray): string;
/**

@@ -226,3 +226,3 @@ * Converts TypedArray to string

*/
export declare function _typedArray(obj: TypedArray): string;
export declare function _typedArray(this: Stringifiers, obj: TypedArray): string;
/**

@@ -234,3 +234,3 @@ * Converts set to string

*/
export declare function _setSortCoerce(obj: Set<any>): string;
export declare function _setSortCoerce(this: Stringifiers, obj: Set<any>): string;
/**

@@ -242,3 +242,3 @@ * Converts set to string

*/
export declare function _setSort(obj: Set<any>): string;
export declare function _setSort(this: Stringifiers, obj: Set<any>): string;
/**

@@ -250,3 +250,3 @@ * Converts set to string

*/
export declare function _set(obj: Set<any>): string;
export declare function _set(this: Stringifiers, obj: Set<any>): string;
/**

@@ -258,3 +258,3 @@ * Converts set to string

*/
export declare function _setCoerce(obj: Set<any>): string;
export declare function _setCoerce(this: Stringifiers, obj: Set<any>): string;
/**

@@ -266,3 +266,3 @@ * Converts object to string

*/
export declare function _object(obj: {
export declare function _object(this: Stringifiers, obj: {
[key: string]: any;

@@ -276,3 +276,3 @@ }): string;

*/
export declare function _objectSort(obj: {
export declare function _objectSort(this: Stringifiers, obj: {
[key: string]: any;

@@ -286,3 +286,3 @@ }): string;

*/
export declare function _map(obj: Map<any, any>): string;
export declare function _map(this: Stringifiers, obj: Map<any, any>): string;
/**

@@ -294,3 +294,3 @@ * Converts map to string

*/
export declare function _mapSort(obj: Map<any, any>): string;
export declare function _mapSort(this: Stringifiers, obj: Map<any, any>): string;
//# sourceMappingURL=stringifiers.d.ts.map
"use strict";
/**
* @private
* @inner
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
Object.defineProperty(exports, "__esModule", { value: true });
exports._mapSort = exports._map = exports._objectSort = exports._object = exports._setCoerce = exports._set = exports._setSort = exports._setSortCoerce = exports._typedArray = exports._typedArraySort = exports._array = exports._arraySort = exports._date = exports._dateCoerce = exports._functionTrim = exports._functionTrimCoerce = exports._function = exports._functionCoerce = exports._null = exports._nullCoerce = exports._undefined = exports._undefinedCoerce = exports._symbol = exports._symbolCoerce = exports._boolean = exports._booleanCoerce = exports._bigInt = exports._bigIntCoerce = exports._number = exports._numberCoerce = exports._stringTrim = exports._stringTrimCoerce = exports._string = exports._stringCoerce = exports._hashable = exports.PREFIX = void 0;
var typeGuess_1 = require("./typeGuess");
const typeGuess_1 = require("./typeGuess");
/**

@@ -93,3 +90,3 @@ * Prefixes that used when type coercion is disabled

function _number(obj) {
return exports.PREFIX.number + ":" + obj;
return `${exports.PREFIX.number}:${obj}`;
}

@@ -114,3 +111,3 @@ exports._number = _number;

function _bigInt(obj) {
return exports.PREFIX.bigint + ":" + obj.toString();
return `${exports.PREFIX.bigint}:${obj.toString()}`;
}

@@ -204,2 +201,3 @@ exports._bigInt = _bigInt;

*/
// eslint-disable-next-line @typescript-eslint/ban-types
function _functionCoerce(obj) {

@@ -215,2 +213,3 @@ return obj.name + '=>' + obj.toString();

*/
// eslint-disable-next-line @typescript-eslint/ban-types
function _function(obj) {

@@ -226,2 +225,3 @@ return exports.PREFIX.function + ':' + obj.name + '=>' + obj.toString();

*/
// eslint-disable-next-line @typescript-eslint/ban-types
function _functionTrimCoerce(obj) {

@@ -242,2 +242,3 @@ return (obj.name +

*/
// eslint-disable-next-line @typescript-eslint/ban-types
function _functionTrim(obj) {

@@ -281,6 +282,6 @@ return (exports.PREFIX.function +

function _arraySort(obj) {
var stringifiers = this;
const stringifiers = this;
return ('[' +
obj
.map(function (item) {
.map((item) => {
return stringifiers[(0, typeGuess_1.guessType)(item)](item);

@@ -300,6 +301,6 @@ })

function _array(obj) {
var stringifiers = this;
const stringifiers = this;
return ('[' +
obj
.map(function (item) {
.map((item) => {
return stringifiers[(0, typeGuess_1.guessType)(item)](item);

@@ -318,8 +319,8 @@ })

function _typedArraySort(obj) {
var stringifiers = this;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
var values = Array.prototype.slice.call(obj);
const stringifiers = this;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
const values = Array.prototype.slice.call(obj);
return ('[' +
values
.map(function (num) {
.map((num) => {
return stringifiers[(0, typeGuess_1.guessType)(num)](num);

@@ -339,8 +340,7 @@ })

function _typedArray(obj) {
var stringifiers = this;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
var values = Array.prototype.slice.call(obj);
const stringifiers = this;
const values = Array.prototype.slice.call(obj);
return ('[' +
values
.map(function (num) {
.map((num) => {
return stringifiers[(0, typeGuess_1.guessType)(num)](num);

@@ -369,3 +369,3 @@ })

function _setSort(obj) {
return exports.PREFIX.set + ":" + _arraySort.call(this, Array.from(obj));
return `${exports.PREFIX.set}:${_arraySort.call(this, Array.from(obj))}`;
}

@@ -380,3 +380,3 @@ exports._setSort = _setSort;

function _set(obj) {
return exports.PREFIX.set + ":" + _array.call(this, Array.from(obj));
return `${exports.PREFIX.set}:${_array.call(this, Array.from(obj))}`;
}

@@ -401,9 +401,8 @@ exports._set = _set;

function _object(obj) {
var stringifiers = this;
var keys = Object.keys(obj);
var objArray = [];
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
var key = keys_1[_i];
var val = obj[key];
var valT = (0, typeGuess_1.guessType)(val);
const stringifiers = this;
const keys = Object.keys(obj);
const objArray = [];
for (const key of keys) {
const val = obj[key];
const valT = (0, typeGuess_1.guessType)(val);
objArray.push(key + ':' + stringifiers[valT](val));

@@ -421,9 +420,8 @@ }

function _objectSort(obj) {
var stringifiers = this;
var keys = Object.keys(obj).sort();
var objArray = [];
for (var _i = 0, keys_2 = keys; _i < keys_2.length; _i++) {
var key = keys_2[_i];
var val = obj[key];
var valT = (0, typeGuess_1.guessType)(val);
const stringifiers = this;
const keys = Object.keys(obj).sort();
const objArray = [];
for (const key of keys) {
const val = obj[key];
const valT = (0, typeGuess_1.guessType)(val);
objArray.push(key + ':' + stringifiers[valT](val));

@@ -441,12 +439,8 @@ }

function _map(obj) {
var stringifiers = this;
var arr = Array.from(obj);
var mapped = [];
for (var _i = 0, arr_1 = arr; _i < arr_1.length; _i++) {
var item = arr_1[_i];
var _a = item, key = _a[0], value = _a[1];
mapped.push([
stringifiers[(0, typeGuess_1.guessType)(key)](key),
stringifiers[(0, typeGuess_1.guessType)(value)](value),
]);
const stringifiers = this;
const arr = Array.from(obj);
const mapped = [];
for (const item of arr) {
const [key, value] = item;
mapped.push([stringifiers[(0, typeGuess_1.guessType)(key)](key), stringifiers[(0, typeGuess_1.guessType)(value)](value)]);
}

@@ -463,12 +457,8 @@ return '[' + mapped.join(';') + ']';

function _mapSort(obj) {
var stringifiers = this;
var arr = Array.from(obj);
var mapped = [];
for (var _i = 0, arr_2 = arr; _i < arr_2.length; _i++) {
var item = arr_2[_i];
var _a = item, key = _a[0], value = _a[1];
mapped.push([
stringifiers[(0, typeGuess_1.guessType)(key)](key),
stringifiers[(0, typeGuess_1.guessType)(value)](value),
]);
const stringifiers = this;
const arr = Array.from(obj);
const mapped = [];
for (const item of arr) {
const [key, value] = item;
mapped.push([stringifiers[(0, typeGuess_1.guessType)(key)](key), stringifiers[(0, typeGuess_1.guessType)(value)](value)]);
}

@@ -475,0 +465,0 @@ return '[' + mapped.sort().join(';') + ']';

@@ -0,1 +1,2 @@

import type { Hashable } from './hasher';
/**

@@ -12,3 +13,3 @@ * Type mapping rules.

*/
export declare function guessObjectType(obj: unknown): string;
export declare const guessObjectType: <T>(obj: T) => string;
/**

@@ -19,3 +20,10 @@ * Guess variable type

*/
export declare function guessType(obj: unknown): string;
export declare const guessType: <T>(obj: T) => string;
/**
* Identify if object is instance of Hashable interface
* @param object analyzed variable
* @return true if object has toHashableString property and this property is function
* otherwise return false
*/
export declare const instanceOfHashable: <T>(object: T | Hashable) => boolean;
//# sourceMappingURL=typeGuess.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.guessType = exports.guessObjectType = exports.TYPE_MAP = void 0;
exports.instanceOfHashable = exports.guessType = exports.guessObjectType = exports.TYPE_MAP = void 0;
/**

@@ -35,13 +35,12 @@ * Type mapping rules.

*/
function guessObjectType(obj) {
var _a, _b;
const guessObjectType = (obj) => {
if (obj === null) {
return 'null';
}
if (instanceOfHashable(obj)) {
if ((0, exports.instanceOfHashable)(obj)) {
return 'hashable';
}
var type = (_b = (_a = obj.constructor) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : 'unknown';
const type = obj?.constructor?.name ?? 'unknown';
return exports.TYPE_MAP[type] || 'unknown';
}
};
exports.guessObjectType = guessObjectType;

@@ -53,6 +52,6 @@ /**

*/
function guessType(obj) {
var type = typeof obj;
return type !== 'object' ? type : guessObjectType(obj);
}
const guessType = (obj) => {
const type = typeof obj;
return type !== 'object' ? type : (0, exports.guessObjectType)(obj);
};
exports.guessType = guessType;

@@ -65,5 +64,6 @@ /**

*/
function instanceOfHashable(object) {
const instanceOfHashable = (object) => {
return typeof object.toHashableString === 'function';
}
};
exports.instanceOfHashable = instanceOfHashable;
//# sourceMappingURL=typeGuess.js.map
{
"name": "node-object-hash",
"version": "2.3.10",
"version": "3.0.0",
"description": "Node.js object hash library with properties/arrays sorting to provide constant hashes",

@@ -19,3 +19,3 @@ "keywords": [

"type": "git",
"url": "https://github.com/SkeLLLa/node-object-hash.git"
"url": "git@github.com:SkeLLLa/node-object-hash.git"
},

@@ -37,57 +37,67 @@ "license": "MIT",

"scripts": {
"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",
"audit": "pnpm audit --prod --audit-level=high",
"benchmark": "pnpm run build && pnpm run benchmark:regular && pnpm run benchmark:custom",
"benchmark:custom": "node --expose-gc benchmark/custom.js",
"build": "npm run build:node",
"build:node": "tsc -p tsconfig.json",
"get-changelog": "conventional-changelog -r 2 -p angular",
"lint": "npm run lint:eslint",
"benchmark:regular": "node benchmark/bench.js",
"build": "pnpm run build:node && pnpm run build:docs",
"build:docs": "typedoc --plugin typedoc-plugin-markdown --plugin typedoc-plugin-mdn-links && pretty-quick",
"build:node": "tsc -p tsconfig.build.json",
"lint": "pnpm run lint:eslint",
"lint:eslint": "eslint . --ext js,jsx,ts,tsx",
"prerelease": "npm run typedoc && git add -A ./docs",
"release": "git add -A && standard-version -a",
"test": "npm audit --production && npm run lint && npm run unit",
"typedoc": "rm -rf ./docs/* && typedoc && pretty-quick",
"unit": "jest --coverage",
"postunit": "codecov || true",
"version:update": "echo v$npm_package_version > VERSION"
"lint:typescript": "tsc",
"release": "semantic-release",
"test": "pnpm run audit && pnpm run lint && pnpm run test:unit",
"test:unit": "jest --coverage"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"devDependencies": {
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"@jest/globals": "^27.2.0",
"@types/node": "^16.9.1",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
"codecov": "^3.8.3",
"conventional-changelog-cli": "^2.1.1",
"eslint": "^7.32.0",
"@jest/globals": "^29.5.0",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/commit-analyzer": "^9.0.2",
"@semantic-release/exec": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"@semantic-release/github": "^8.0.7",
"@semantic-release/npm": "^10.0.3",
"@semantic-release/release-notes-generator": "^10.0.3",
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
"@tsconfig/node-lts": "^18.12.1",
"@types/node": "^18.15.11",
"@typescript-eslint/eslint-plugin": "^5.58.0",
"@typescript-eslint/parser": "^5.58.0",
"conventional-changelog-cli": "^2.2.2",
"conventional-changelog-conventionalcommits": "^5.0.0",
"eslint": "^8.38.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-jest": "^24.4.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-sort-requires": "^2.1.0",
"faker": "^5.5.3",
"husky": "^7.0.2",
"jest": "^27.2.0",
"prettier": "^2.4.0",
"prettier-plugin-packagejson": "^2.2.12",
"pretty-quick": "^3.1.1",
"replace": "^1.2.1",
"standard-version": "^9.3.1",
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"typedoc": "~0.21.0",
"typedoc-plugin-markdown": "^3.10.4",
"typescript": "~4.4.3"
"eslint-config-prettier": "^8.8.0",
"eslint-import-resolver-typescript": "^3.5.5",
"eslint-plugin-filenames": "^1.3.2",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-security": "^1.7.1",
"jest": "^29.5.0",
"prettier": "^2.8.7",
"prettier-plugin-packagejson": "^2.4.3",
"prettier-plugin-sh": "^0.12.8",
"prettier-plugin-sort-json": "^1.0.0",
"pretty-quick": "^3.1.3",
"semantic-release": "^21.0.1",
"semantic-release-mirror-version": "^1.1.2",
"ts-jest": "^29.1.0",
"typedoc": "^0.24.1",
"typedoc-plugin-markdown": "^3.15.1",
"typedoc-plugin-mdn-links": "^3.0.3",
"typescript": "^5.0.4"
},
"engines": {
"node": ">=0.10.0"
"node": ">=16",
"pnpm": ">=8"
},
"pnpm": {
"updateConfig": {
"ignoreDependencies": [
"faker"
]
}
}
}

@@ -20,4 +20,2 @@ # node-object-hash

[![Codecov Coverage](https://codecov.io/gh/SkeLLLa/node-object-hash/branch/master/graph/badge.svg?token=wLjMou8TT7)](https://codecov.io/gh/SkeLLLa/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/)

@@ -32,2 +30,3 @@ </div>

- [ToC](#toc)
- [What's new in v3.0.0](#whats-new-in-v300)
- [What's new in v2.0.0](#whats-new-in-v200)

@@ -50,3 +49,3 @@ - [Breaking changes](#breaking-changes)

- [version \>=1.0.0](#version-100)
- [version \>=0.1.0 && <1.0.0](#version-010--100)
- [version \>=0.1.0 \&\& \<1.0.0](#version-010--100)
- [Examples](#examples)

@@ -61,2 +60,13 @@ - [Benchmarks](#benchmarks)

## What's new in v3.0.0
**Disclaimer**: No new features or changes that may break hashes from previous versions. There's no need to update unless you're starting project from scratch.
- Refactor and migration to typescript 5.
- Drop old node support.
- Removed typescript namespaces.
- Updated exported functions and object structure.
- Removed faker and old benchmarks.
- New CI and release automation.
## What's new in v2.0.0

@@ -158,7 +168,8 @@

See [changelog](CHANGELOG.md)
See [changelog](docs/CHANGELOG.md)
For v2 changes see [changelog-v2](docs/CHANGELOG-v2.md)
## Docs
Full API docs could be found in [docs](./docs/README.md).
Full API docs could be found in [docs](./docs/api/README.md).

@@ -170,3 +181,3 @@ ### API overview

```js
require('node-object-hash')([options]);
require('node-object-hash').hasher([options]);
```

@@ -224,3 +235,3 @@

```js
var hasher = require('node-object-hash');
var { hasher } = require('node-object-hash');

@@ -227,0 +238,0 @@ var hashSortCoerce = hasher({ sort: true, coerce: true });

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc