node-object-hash
Version:
Node.js object hash library with properties/arrays sorting to provide constant hashes
452 lines • 11.9 kB
JavaScript
"use strict";
/* 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;
const typeGuess_1 = require("./typeGuess");
/**
* Prefixes that used when type coercion is disabled
*/
exports.PREFIX = {
string: '<:s>',
number: '<:n>',
bigint: '<:bi>',
boolean: '<:b>',
symbol: '<:smbl>',
undefined: '<:undf>',
null: '<:null>',
function: '<:func>',
array: '',
date: '<:date>',
set: '<:set>',
map: '<:map>',
};
/**
* Converts Hashable to string
* @private
* @param obj object to convert
* @returns object string representation
*/
function _hashable(obj) {
return obj.toHashableString();
}
exports._hashable = _hashable;
/**
* Converts string to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _stringCoerce(obj) {
return obj;
}
exports._stringCoerce = _stringCoerce;
/**
* Converts string to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _string(obj) {
return exports.PREFIX.string + ':' + obj;
}
exports._string = _string;
/**
* Converts string to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _stringTrimCoerce(obj) {
return obj.replace(/(\s+|\t|\r\n|\n|\r)/gm, ' ').trim();
}
exports._stringTrimCoerce = _stringTrimCoerce;
/**
* Converts string to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _stringTrim(obj) {
return exports.PREFIX.string + ':' + obj.replace(/(\s+|\t|\r\n|\n|\r)/gm, ' ').trim();
}
exports._stringTrim = _stringTrim;
/**
* Converts number to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _numberCoerce(obj) {
return obj.toString();
}
exports._numberCoerce = _numberCoerce;
/**
* Converts number to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _number(obj) {
return `${exports.PREFIX.number}:${obj}`;
}
exports._number = _number;
/**
* Converts BigInt to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _bigIntCoerce(obj) {
return obj.toString();
}
exports._bigIntCoerce = _bigIntCoerce;
/**
* Converts BigInt to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _bigInt(obj) {
return `${exports.PREFIX.bigint}:${obj.toString()}`;
}
exports._bigInt = _bigInt;
/**
* Converts boolean to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _booleanCoerce(obj) {
return obj ? '1' : '0';
}
exports._booleanCoerce = _booleanCoerce;
/**
* Converts boolean to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _boolean(obj) {
return exports.PREFIX.boolean + ':' + obj.toString();
}
exports._boolean = _boolean;
/**
* Converts symbol to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _symbolCoerce() {
return exports.PREFIX.symbol;
}
exports._symbolCoerce = _symbolCoerce;
/**
* Converts symbol to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _symbol(obj) {
return exports.PREFIX.symbol + ':' + obj.toString();
}
exports._symbol = _symbol;
/**
* Converts undefined to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _undefinedCoerce() {
return '';
}
exports._undefinedCoerce = _undefinedCoerce;
/**
* Converts undefined to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _undefined() {
return exports.PREFIX.undefined;
}
exports._undefined = _undefined;
/**
* Converts null to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _nullCoerce() {
return '';
}
exports._nullCoerce = _nullCoerce;
/**
* Converts null to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _null() {
return exports.PREFIX.null;
}
exports._null = _null;
/**
* Converts function to string
* @private
* @param obj object to convert
* @return object string representation
*/
// eslint-disable-next-line @typescript-eslint/ban-types
function _functionCoerce(obj) {
return obj.name + '=>' + obj.toString();
}
exports._functionCoerce = _functionCoerce;
/**
* Converts function to string
* @private
* @param obj object to convert
* @return object string representation
*/
// eslint-disable-next-line @typescript-eslint/ban-types
function _function(obj) {
return exports.PREFIX.function + ':' + obj.name + '=>' + obj.toString();
}
exports._function = _function;
/**
* Converts function to string
* @private
* @param obj object to convert
* @return object string representation
*/
// eslint-disable-next-line @typescript-eslint/ban-types
function _functionTrimCoerce(obj) {
return (obj.name +
'=>' +
obj
.toString()
.replace(/(\s+|\t|\r\n|\n|\r)/gm, ' ')
.trim());
}
exports._functionTrimCoerce = _functionTrimCoerce;
/**
* Converts function to string
* @private
* @param obj object to convert
* @return object string representation
*/
// eslint-disable-next-line @typescript-eslint/ban-types
function _functionTrim(obj) {
return (exports.PREFIX.function +
':' +
obj.name +
'=>' +
obj
.toString()
.replace(/(\s+|\t|\r\n|\n|\r)/gm, ' ')
.trim());
}
exports._functionTrim = _functionTrim;
/**
* Converts date to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _dateCoerce(obj) {
return obj.toISOString();
}
exports._dateCoerce = _dateCoerce;
/**
* Converts date to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _date(obj) {
return exports.PREFIX.date + ':' + obj.toISOString();
}
exports._date = _date;
/**
* Converts array to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _arraySort(obj) {
const stringifiers = this;
return ('[' +
obj
.map((item) => {
return stringifiers[(0, typeGuess_1.guessType)(item)](item);
})
.sort()
.toString() +
']');
}
exports._arraySort = _arraySort;
/**
* Converts array to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _array(obj) {
const stringifiers = this;
return ('[' +
obj
.map((item) => {
return stringifiers[(0, typeGuess_1.guessType)(item)](item);
})
.toString() +
']');
}
exports._array = _array;
/**
* Converts TypedArray to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _typedArraySort(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((num) => {
return stringifiers[(0, typeGuess_1.guessType)(num)](num);
})
.sort()
.toString() +
']');
}
exports._typedArraySort = _typedArraySort;
/**
* Converts TypedArray to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _typedArray(obj) {
const stringifiers = this;
const values = Array.prototype.slice.call(obj);
return ('[' +
values
.map((num) => {
return stringifiers[(0, typeGuess_1.guessType)(num)](num);
})
.toString() +
']');
}
exports._typedArray = _typedArray;
/**
* Converts set to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _setSortCoerce(obj) {
return _arraySort.call(this, Array.from(obj));
}
exports._setSortCoerce = _setSortCoerce;
/**
* Converts set to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _setSort(obj) {
return `${exports.PREFIX.set}:${_arraySort.call(this, Array.from(obj))}`;
}
exports._setSort = _setSort;
/**
* Converts set to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _set(obj) {
return `${exports.PREFIX.set}:${_array.call(this, Array.from(obj))}`;
}
exports._set = _set;
/**
* Converts set to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _setCoerce(obj) {
return _array.call(this, Array.from(obj));
}
exports._setCoerce = _setCoerce;
/**
* Converts object to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _object(obj) {
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));
}
return '{' + objArray.toString() + '}';
}
exports._object = _object;
/**
* Converts object to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _objectSort(obj) {
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));
}
return '{' + objArray.toString() + '}';
}
exports._objectSort = _objectSort;
/**
* Converts map to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _map(obj) {
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)]);
}
return '[' + mapped.join(';') + ']';
}
exports._map = _map;
/**
* Converts map to string
* @private
* @param obj object to convert
* @return object string representation
*/
function _mapSort(obj) {
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)]);
}
return '[' + mapped.sort().join(';') + ']';
}
exports._mapSort = _mapSort;
//# sourceMappingURL=stringifiers.js.map