Comparing version 1.2.1 to 1.3.0
# hashIt CHANGELOG | ||
#### 1.3.0 | ||
* Add hashIt.isUndefined, hashIt.isNull, and hashIt.isEmpty methods | ||
* Reorder switch statements in replacer and getValueForStringification to reflect most likely to least likely (improves performance a touch) | ||
* Remove "Number" from number stringification | ||
* Leverage prependTypeToString whereever possible | ||
* Include Arguments object class | ||
#### 1.2.1 | ||
@@ -4,0 +11,0 @@ * Calculation of Math hashCode now uses properties |
@@ -71,2 +71,13 @@ var hashIt = | ||
var UNDEFINED_HASH = hash(undefined); | ||
var NULL_HASH = hash(null); | ||
var EMPTY_ARRAY_HASH = hash([]); | ||
var EMPTY_MAP_HASH = hash(new Map()); | ||
var EMPTY_NUMBER_HASH = hash(0); | ||
var EMPTY_OBJECT_HASH = hash({}); | ||
var EMPTY_SET_HASH = hash(new Set()); | ||
var EMPTY_STRING_HASH = hash(''); | ||
/** | ||
@@ -96,2 +107,35 @@ * determine if all objects passed are equal in value to one another | ||
/** | ||
* determine if object is empty, meaning it is an array / object / map / set with values populated, | ||
* or is a string with no length, or is undefined or null | ||
* | ||
* @param {any} object | ||
* @returns {boolean} | ||
*/ | ||
hash.isEmpty = function (object) { | ||
var objectHash = hash(object); | ||
return objectHash === UNDEFINED_HASH || objectHash === NULL_HASH || objectHash === EMPTY_ARRAY_HASH || objectHash === EMPTY_MAP_HASH || objectHash === EMPTY_NUMBER_HASH || objectHash === EMPTY_OBJECT_HASH || objectHash === EMPTY_SET_HASH || objectHash === EMPTY_STRING_HASH; | ||
}; | ||
/** | ||
* determine if object is null | ||
* | ||
* @param {any} object | ||
* @returns {boolean} | ||
*/ | ||
hash.isNull = function (object) { | ||
return hash(object) === NULL_HASH; | ||
}; | ||
/** | ||
* determine if object is undefined | ||
* | ||
* @param {any} object | ||
* @returns {boolean} | ||
*/ | ||
hash.isUndefined = function (object) { | ||
return hash(object) === UNDEFINED_HASH; | ||
}; | ||
exports.default = hash; | ||
@@ -170,23 +214,21 @@ module.exports = exports['default']; | ||
switch (toStringType) { | ||
case _toString.types.ARRAY_BUFFER: | ||
case _toString.types.NUMBER: | ||
case _toString.types.STRING: | ||
return { | ||
v: prependTypeToString(arrayBufferToString(object), type) | ||
v: object | ||
}; | ||
case _toString.types.DATA_VIEW: | ||
case _toString.types.ARGUMENTS: | ||
case _toString.types.ARRAY: | ||
case _toString.types.OBJECT: | ||
return { | ||
v: prependTypeToString(arrayBufferToString(object.buffer), type) | ||
v: !!object ? object : prependTypeToString(object, type) | ||
}; | ||
case _toString.types.FLOAT_32_ARRAY: | ||
case _toString.types.FLOAT_64_ARRAY: | ||
case _toString.types.INT_8_ARRAY: | ||
case _toString.types.INT_16_ARRAY: | ||
case _toString.types.INT_32_ARRAY: | ||
case _toString.types.UINT_8_ARRAY: | ||
case _toString.types.UINT_8_CLAMPED_ARRAY: | ||
case _toString.types.UINT_16_ARRAY: | ||
case _toString.types.UINT_32_ARRAY: | ||
case _toString.types.ERROR: | ||
case _toString.types.NULL: | ||
case _toString.types.REGEXP: | ||
case _toString.types.UNDEFINED: | ||
return { | ||
v: type + ' [' + object.join(',') + ']' | ||
v: prependTypeToString(object, type) | ||
}; | ||
@@ -209,11 +251,14 @@ | ||
case _toString.types.ERROR: | ||
case _toString.types.NULL: | ||
case _toString.types.NUMBER: | ||
case _toString.types.REGEXP: | ||
case _toString.types.UNDEFINED: | ||
case _toString.types.SYMBOL: | ||
return { | ||
v: prependTypeToString(object, type) | ||
v: object.toString() | ||
}; | ||
case _toString.types.PROMISE: | ||
case _toString.types.WEAKMAP: | ||
case _toString.types.WEAKSET: | ||
return { | ||
v: prependTypeToString('NOT_ENUMERABLE', type) | ||
}; | ||
case _toString.types.MAP: | ||
@@ -231,12 +276,25 @@ case _toString.types.SET: | ||
case _toString.types.OBJECT: | ||
case _toString.types.ARRAY_BUFFER: | ||
return { | ||
v: !!object ? object : prependTypeToString(object, type) | ||
v: prependTypeToString(arrayBufferToString(object), type) | ||
}; | ||
case _toString.types.SYMBOL: | ||
case _toString.types.DATA_VIEW: | ||
return { | ||
v: object.toString() | ||
v: prependTypeToString(arrayBufferToString(object.buffer), type) | ||
}; | ||
case _toString.types.FLOAT_32_ARRAY: | ||
case _toString.types.FLOAT_64_ARRAY: | ||
case _toString.types.INT_8_ARRAY: | ||
case _toString.types.INT_16_ARRAY: | ||
case _toString.types.INT_32_ARRAY: | ||
case _toString.types.UINT_8_ARRAY: | ||
case _toString.types.UINT_8_CLAMPED_ARRAY: | ||
case _toString.types.UINT_16_ARRAY: | ||
case _toString.types.UINT_32_ARRAY: | ||
return { | ||
v: prependTypeToString(object.join(','), type) | ||
}; | ||
case _toString.types.MATH: | ||
@@ -253,9 +311,2 @@ var mathObject = {}; | ||
case _toString.types.PROMISE: | ||
case _toString.types.WEAKMAP: | ||
case _toString.types.WEAKSET: | ||
return { | ||
v: type + '--NOT_ENUMERABLE' | ||
}; | ||
default: | ||
@@ -287,2 +338,27 @@ return { | ||
switch (type) { | ||
case _toString.types.NUMBER: | ||
case _toString.types.STRING: | ||
return value; | ||
case _toString.types.ARGUMENTS: | ||
case _toString.types.ARRAY: | ||
case _toString.types.OBJECT: | ||
if (!value) { | ||
return prependTypeToString(value, type); | ||
} | ||
if (++recursiveCounter > 255) { | ||
return 'Undefined undefined'; | ||
} | ||
index = stack.indexOf(value); | ||
if (!~index) { | ||
stack.push(value); | ||
return value; | ||
} | ||
return '*Recursive-' + index; | ||
case _toString.types.ARRAY_BUFFER: | ||
@@ -315,22 +391,2 @@ case _toString.types.DATA_VIEW: | ||
case _toString.types.ARRAY: | ||
case _toString.types.OBJECT: | ||
if (!value) { | ||
return prependTypeToString(value, type); | ||
} | ||
if (++recursiveCounter > 255) { | ||
return 'Undefined undefined'; | ||
} | ||
index = stack.indexOf(value); | ||
if (!~index) { | ||
stack.push(value); | ||
return value; | ||
} | ||
return '*Recursive-' + index; | ||
default: | ||
@@ -386,5 +442,3 @@ return value; | ||
var stringify = function stringify(object) { | ||
var value = getValueForStringification(object); | ||
return tryCatch(value); | ||
return tryCatch(getValueForStringification(object)); | ||
}; | ||
@@ -556,2 +610,3 @@ | ||
exports.__esModule = true; | ||
var ARGUMENTS = '[object Arguments]'; | ||
var ARRAY = '[object Array]'; | ||
@@ -590,2 +645,3 @@ var ARRAY_BUFFER = '[object ArrayBuffer]'; | ||
var TYPES = { | ||
ARGUMENTS: ARGUMENTS, | ||
ARRAY: ARRAY, | ||
@@ -592,0 +648,0 @@ ARRAY_BUFFER: ARRAY_BUFFER, |
@@ -1,1 +0,1 @@ | ||
var hashIt=function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){e.exports=r(1)},function(e,t,r){"use strict";t.__esModule=!0;var n=r(2),o=function(e){return(0,n.getIntegerHashValue)((0,n.stringify)(e))};o.isEqual=function(){var e=arguments.length;if(1===e)throw new Error("isEqual requires at least two objects to be passed for comparison.");for(var t=0;++t<e;)if(o(arguments.length<=t-1+0?void 0:arguments[t-1+0])!==o(arguments.length<=t+0?void 0:arguments[t+0]))return!1;return!0},t.default=o,e.exports=t.default},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}t.__esModule=!0,t.stringify=t.replacer=t.getIntegerHashValue=void 0;var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},s=r(3),c=n(s),u=r(4),a=/\[object (HTML(.*)Element)\]/,i=["E","LN2","LN10","LOG2E","LOG10E","PI","SQRT1_2","SQRT2"],A=function(e){return"undefined"==typeof Uint16Array?"":String.fromCharCode.apply(null,new Uint16Array(e))},f=function(e){return e.replace(/^\[object (.+)\]$/,"$1")},y=function(e,t){return t+" "+e},p=function(e){var t=(0,u.toString)(e),r=f(t),n=function(){switch(t){case u.types.ARRAY_BUFFER:return{v:y(A(e),r)};case u.types.DATA_VIEW:return{v:y(A(e.buffer),r)};case u.types.FLOAT_32_ARRAY:case u.types.FLOAT_64_ARRAY:case u.types.INT_8_ARRAY:case u.types.INT_16_ARRAY:case u.types.INT_32_ARRAY:case u.types.UINT_8_ARRAY:case u.types.UINT_8_CLAMPED_ARRAY:case u.types.UINT_16_ARRAY:case u.types.UINT_32_ARRAY:return{v:r+" ["+e.join(",")+"]"};case u.types.DATE:return{v:y(e.valueOf(),r)};case u.types.FUNCTION:return{v:(0,u.toFunctionString)(e)};case u.types.GENERATOR:return{v:(0,u.toFunctionString)(e,!0)};case u.types.ERROR:case u.types.NULL:case u.types.NUMBER:case u.types.REGEXP:case u.types.UNDEFINED:return{v:y(e,r)};case u.types.MAP:case u.types.SET:var n=[r];return e.forEach(function(e,t){n.push([t,e])}),{v:n};case u.types.OBJECT:return{v:e?e:y(e,r)};case u.types.SYMBOL:return{v:e.toString()};case u.types.MATH:var o={};return i.forEach(function(t){o[t]=e[t]}),{v:o};case u.types.PROMISE:case u.types.WEAKMAP:case u.types.WEAKSET:return{v:r+"--NOT_ENUMERABLE"};default:return{v:a.test(t)?"HTMLElement "+e.textContent:e}}}();if("object"===("undefined"==typeof n?"undefined":o(n)))return n.v},R=function(e,t,r,n){return function(t,o){if(""===t)return e=[o],r=0,o;var s=(0,u.toString)(o);switch(s){case u.types.ARRAY_BUFFER:case u.types.DATA_VIEW:case u.types.DATE:case u.types.FLOAT_32_ARRAY:case u.types.FLOAT_64_ARRAY:case u.types.FUNCTION:case u.types.GENERATOR:case u.types.INT_8_ARRAY:case u.types.INT_16_ARRAY:case u.types.INT_32_ARRAY:case u.types.ERROR:case u.types.MAP:case u.types.MATH:case u.types.NULL:case u.types.PROMISE:case u.types.REGEXP:case u.types.SET:case u.types.SYMBOL:case u.types.UINT_8_ARRAY:case u.types.UINT_8_CLAMPED_ARRAY:case u.types.UINT_16_ARRAY:case u.types.UINT_32_ARRAY:case u.types.UNDEFINED:case u.types.WEAKMAP:case u.types.WEAKSET:return p(o);case u.types.ARRAY:case u.types.OBJECT:return o?++r>255?"Undefined undefined":(n=e.indexOf(o),~n?"*Recursive-"+n:(e.push(o),o)):y(o,s);default:return o}}}(),_=function(e){if(!e)return 0;for(var t=5381,r=e.length;r;)t=33*t^e.charCodeAt(--r);return t>>>0},l=function(e){try{return JSON.stringify(e,R)}catch(t){return c.default.prune(e)}},E=function(e){var t=p(e);return l(t)};t.getIntegerHashValue=_,t.replacer=R,t.stringify=E},function(e,t,r){"use strict";t.__esModule=!0;var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},o=r(4),s=6,c=50,u="*Recursive",a=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,i={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},A=void 0,f=function(e,t){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t(r)},y=function(e){a.lastIndex=0;var t=void 0;return a.test(e)?'"'+e.replace(a,function(e){return t=i[e],(0,o.toString)(t)===o.types.STRING?t:"\\u"+("0000"+e.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+e+'"'},p=function(e){A=[];var t=function r(e,t,s){var a=t[e],i=[],p=void 0;switch("undefined"==typeof a?"undefined":n(a)){case"string":return y(a);case"boolean":case"null":case"number":case"undefined":return""+a;case"function":return(0,o.toFunctionString)(a);case"object":if(!a)return""+a;var R=A.indexOf(a);if(s<=0||~R)return u+"-"+R;switch((0,o.toString)(a)){case o.types.ARRAY:A.push(a);for(var _=Math.min(a.length,c),l=-1;++l<_;)i[l]=r(l,a,s-1);return p="["+i.join(",")+"]";case o.types.DATE:return""+a.valueOf();default:return A.push(a),f(a,function(e){try{p=r(e,a,s-1),p&&i.push(y(e)+":"+p)}catch(t){}}),"{"+i.join(",")+"}"}}};return t("",{"":e},s)};t.default={prune:p},e.exports=t.default},function(e,t){"use strict";t.__esModule=!0;var r="[object Array]",n="[object ArrayBuffer]",o="[object Boolean]",s="[object DataView]",c="[object Date]",u="[object Error]",a="[object Float32Array]",i="[object Float64Array]",A="[object Function]",f="[object GeneratorFunction]",y="[object Int8Array]",p="[object Int16Array]",R="[object Int32Array]",_="[object Map]",l="[object Math]",E="[object Null]",b="[object Number]",T="[object Object]",d="[object Promise]",N="[object RegExp]",v="[object Set]",I="[object String]",j="[object Symbol]",S="[object Uint8Array]",O="[object Uint8ClampedArray]",Y="[object Uint16Array]",g="[object Uint32Array]",U="[object Undefined]",h="[object WeakMap]",M="[object WeakSet]",F="[object Window]",L={ARRAY:r,ARRAY_BUFFER:n,BOOLEAN:o,DATA_VIEW:s,DATE:c,ERROR:u,FLOAT_32_ARRAY:a,FLOAT_64_ARRAY:i,FUNCTION:A,GENERATOR:f,INT_8_ARRAY:y,INT_16_ARRAY:p,INT_32_ARRAY:R,MAP:_,MATH:l,NULL:E,NUMBER:b,OBJECT:T,PROMISE:d,REGEXP:N,SET:v,STRING:I,SYMBOL:j,UINT_8_ARRAY:S,UINT_8_CLAMPED_ARRAY:O,UINT_16_ARRAY:Y,UINT_32_ARRAY:g,UNDEFINED:U,WEAKMAP:h,WEAKSET:M,WINDOW:F},m=function(e){var t=!(arguments.length<=1||void 0===arguments[1])&&arguments[1];return"function"+(t?"*":"")+" "+(e.name||"anonymous")+"("+new Array(e.length+1).join(",arg").slice(1)+"){}"},D=function(e){return Object.prototype.toString.call(e)};t.toFunctionString=m,t.toString=D,t.types=L}]); | ||
var hashIt=function(e){function t(n){if(r[n])return r[n].exports;var s=r[n]={exports:{},id:n,loaded:!1};return e[n].call(s.exports,s,s.exports,t),s.loaded=!0,s.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){e.exports=r(1)},function(e,t,r){"use strict";t.__esModule=!0;var n=r(2),s=function(e){return(0,n.getIntegerHashValue)((0,n.stringify)(e))},c=s(void 0),o=s(null),u=s([]),a=s(new Map),i=s(0),A=s({}),f=s(new Set),y=s("");s.isEqual=function(){var e=arguments.length;if(1===e)throw new Error("isEqual requires at least two objects to be passed for comparison.");for(var t=0;++t<e;)if(s(arguments.length<=t-1+0?void 0:arguments[t-1+0])!==s(arguments.length<=t+0?void 0:arguments[t+0]))return!1;return!0},s.isEmpty=function(e){var t=s(e);return t===c||t===o||t===u||t===a||t===i||t===A||t===f||t===y},s.isNull=function(e){return s(e)===o},s.isUndefined=function(e){return s(e)===c},t.default=s,e.exports=t.default},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}t.__esModule=!0,t.stringify=t.replacer=t.getIntegerHashValue=void 0;var s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},c=r(3),o=n(c),u=r(4),a=/\[object (HTML(.*)Element)\]/,i=["E","LN2","LN10","LOG2E","LOG10E","PI","SQRT1_2","SQRT2"],A=function(e){return"undefined"==typeof Uint16Array?"":String.fromCharCode.apply(null,new Uint16Array(e))},f=function(e){return e.replace(/^\[object (.+)\]$/,"$1")},y=function(e,t){return t+" "+e},p=function(e){var t=(0,u.toString)(e),r=f(t),n=function(){switch(t){case u.types.NUMBER:case u.types.STRING:return{v:e};case u.types.ARGUMENTS:case u.types.ARRAY:case u.types.OBJECT:return{v:e?e:y(e,r)};case u.types.ERROR:case u.types.NULL:case u.types.REGEXP:case u.types.UNDEFINED:return{v:y(e,r)};case u.types.DATE:return{v:y(e.valueOf(),r)};case u.types.FUNCTION:return{v:(0,u.toFunctionString)(e)};case u.types.GENERATOR:return{v:(0,u.toFunctionString)(e,!0)};case u.types.SYMBOL:return{v:e.toString()};case u.types.PROMISE:case u.types.WEAKMAP:case u.types.WEAKSET:return{v:y("NOT_ENUMERABLE",r)};case u.types.MAP:case u.types.SET:var n=[r];return e.forEach(function(e,t){n.push([t,e])}),{v:n};case u.types.ARRAY_BUFFER:return{v:y(A(e),r)};case u.types.DATA_VIEW:return{v:y(A(e.buffer),r)};case u.types.FLOAT_32_ARRAY:case u.types.FLOAT_64_ARRAY:case u.types.INT_8_ARRAY:case u.types.INT_16_ARRAY:case u.types.INT_32_ARRAY:case u.types.UINT_8_ARRAY:case u.types.UINT_8_CLAMPED_ARRAY:case u.types.UINT_16_ARRAY:case u.types.UINT_32_ARRAY:return{v:y(e.join(","),r)};case u.types.MATH:var s={};return i.forEach(function(t){s[t]=e[t]}),{v:s};default:return{v:a.test(t)?"HTMLElement "+e.textContent:e}}}();if("object"===("undefined"==typeof n?"undefined":s(n)))return n.v},R=function(e,t,r,n){return function(t,s){if(""===t)return e=[s],r=0,s;var c=(0,u.toString)(s);switch(c){case u.types.NUMBER:case u.types.STRING:return s;case u.types.ARGUMENTS:case u.types.ARRAY:case u.types.OBJECT:return s?++r>255?"Undefined undefined":(n=e.indexOf(s),~n?"*Recursive-"+n:(e.push(s),s)):y(s,c);case u.types.ARRAY_BUFFER:case u.types.DATA_VIEW:case u.types.DATE:case u.types.FLOAT_32_ARRAY:case u.types.FLOAT_64_ARRAY:case u.types.FUNCTION:case u.types.GENERATOR:case u.types.INT_8_ARRAY:case u.types.INT_16_ARRAY:case u.types.INT_32_ARRAY:case u.types.ERROR:case u.types.MAP:case u.types.MATH:case u.types.NULL:case u.types.PROMISE:case u.types.REGEXP:case u.types.SET:case u.types.SYMBOL:case u.types.UINT_8_ARRAY:case u.types.UINT_8_CLAMPED_ARRAY:case u.types.UINT_16_ARRAY:case u.types.UINT_32_ARRAY:case u.types.UNDEFINED:case u.types.WEAKMAP:case u.types.WEAKSET:return p(s);default:return s}}}(),l=function(e){if(!e)return 0;for(var t=5381,r=e.length;r;)t=33*t^e.charCodeAt(--r);return t>>>0},E=function(e){try{return JSON.stringify(e,R)}catch(t){return o.default.prune(e)}},_=function(e){return E(p(e))};t.getIntegerHashValue=l,t.replacer=R,t.stringify=_},function(e,t,r){"use strict";t.__esModule=!0;var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},s=r(4),c=6,o=50,u="*Recursive",a=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,i={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},A=void 0,f=function(e,t){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t(r)},y=function(e){a.lastIndex=0;var t=void 0;return a.test(e)?'"'+e.replace(a,function(e){return t=i[e],(0,s.toString)(t)===s.types.STRING?t:"\\u"+("0000"+e.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+e+'"'},p=function(e){A=[];var t=function r(e,t,c){var a=t[e],i=[],p=void 0;switch("undefined"==typeof a?"undefined":n(a)){case"string":return y(a);case"boolean":case"null":case"number":case"undefined":return""+a;case"function":return(0,s.toFunctionString)(a);case"object":if(!a)return""+a;var R=A.indexOf(a);if(c<=0||~R)return u+"-"+R;switch((0,s.toString)(a)){case s.types.ARRAY:A.push(a);for(var l=Math.min(a.length,o),E=-1;++E<l;)i[E]=r(E,a,c-1);return p="["+i.join(",")+"]";case s.types.DATE:return""+a.valueOf();default:return A.push(a),f(a,function(e){try{p=r(e,a,c-1),p&&i.push(y(e)+":"+p)}catch(t){}}),"{"+i.join(",")+"}"}}};return t("",{"":e},c)};t.default={prune:p},e.exports=t.default},function(e,t){"use strict";t.__esModule=!0;var r="[object Arguments]",n="[object Array]",s="[object ArrayBuffer]",c="[object Boolean]",o="[object DataView]",u="[object Date]",a="[object Error]",i="[object Float32Array]",A="[object Float64Array]",f="[object Function]",y="[object GeneratorFunction]",p="[object Int8Array]",R="[object Int16Array]",l="[object Int32Array]",E="[object Map]",_="[object Math]",T="[object Null]",b="[object Number]",N="[object Object]",d="[object Promise]",v="[object RegExp]",S="[object Set]",I="[object String]",j="[object Symbol]",U="[object Uint8Array]",O="[object Uint8ClampedArray]",M="[object Uint16Array]",Y="[object Uint32Array]",g="[object Undefined]",h="[object WeakMap]",m="[object WeakSet]",F="[object Window]",L={ARGUMENTS:r,ARRAY:n,ARRAY_BUFFER:s,BOOLEAN:c,DATA_VIEW:o,DATE:u,ERROR:a,FLOAT_32_ARRAY:i,FLOAT_64_ARRAY:A,FUNCTION:f,GENERATOR:y,INT_8_ARRAY:p,INT_16_ARRAY:R,INT_32_ARRAY:l,MAP:E,MATH:_,NULL:T,NUMBER:b,OBJECT:N,PROMISE:d,REGEXP:v,SET:S,STRING:I,SYMBOL:j,UINT_8_ARRAY:U,UINT_8_CLAMPED_ARRAY:O,UINT_16_ARRAY:M,UINT_32_ARRAY:Y,UNDEFINED:g,WEAKMAP:h,WEAKSET:m,WINDOW:F},D=function(e){var t=!(arguments.length<=1||void 0===arguments[1])&&arguments[1];return"function"+(t?"*":"")+" "+(e.name||"anonymous")+"("+new Array(e.length+1).join(",arg").slice(1)+"){}"},P=function(e){return Object.prototype.toString.call(e)};t.toFunctionString=D,t.toString=P,t.types=L}]); |
@@ -17,2 +17,13 @@ 'use strict'; | ||
var UNDEFINED_HASH = hash(undefined); | ||
var NULL_HASH = hash(null); | ||
var EMPTY_ARRAY_HASH = hash([]); | ||
var EMPTY_MAP_HASH = hash(new Map()); | ||
var EMPTY_NUMBER_HASH = hash(0); | ||
var EMPTY_OBJECT_HASH = hash({}); | ||
var EMPTY_SET_HASH = hash(new Set()); | ||
var EMPTY_STRING_HASH = hash(''); | ||
/** | ||
@@ -42,3 +53,36 @@ * determine if all objects passed are equal in value to one another | ||
/** | ||
* determine if object is empty, meaning it is an array / object / map / set with values populated, | ||
* or is a string with no length, or is undefined or null | ||
* | ||
* @param {any} object | ||
* @returns {boolean} | ||
*/ | ||
hash.isEmpty = function (object) { | ||
var objectHash = hash(object); | ||
return objectHash === UNDEFINED_HASH || objectHash === NULL_HASH || objectHash === EMPTY_ARRAY_HASH || objectHash === EMPTY_MAP_HASH || objectHash === EMPTY_NUMBER_HASH || objectHash === EMPTY_OBJECT_HASH || objectHash === EMPTY_SET_HASH || objectHash === EMPTY_STRING_HASH; | ||
}; | ||
/** | ||
* determine if object is null | ||
* | ||
* @param {any} object | ||
* @returns {boolean} | ||
*/ | ||
hash.isNull = function (object) { | ||
return hash(object) === NULL_HASH; | ||
}; | ||
/** | ||
* determine if object is undefined | ||
* | ||
* @param {any} object | ||
* @returns {boolean} | ||
*/ | ||
hash.isUndefined = function (object) { | ||
return hash(object) === UNDEFINED_HASH; | ||
}; | ||
exports.default = hash; | ||
module.exports = exports['default']; |
'use strict'; | ||
exports.__esModule = true; | ||
var ARGUMENTS = '[object Arguments]'; | ||
var ARRAY = '[object Array]'; | ||
@@ -37,2 +38,3 @@ var ARRAY_BUFFER = '[object ArrayBuffer]'; | ||
var TYPES = { | ||
ARGUMENTS: ARGUMENTS, | ||
ARRAY: ARRAY, | ||
@@ -39,0 +41,0 @@ ARRAY_BUFFER: ARRAY_BUFFER, |
118
lib/utils.js
@@ -67,23 +67,21 @@ 'use strict'; | ||
switch (toStringType) { | ||
case _toString.types.ARRAY_BUFFER: | ||
case _toString.types.NUMBER: | ||
case _toString.types.STRING: | ||
return { | ||
v: prependTypeToString(arrayBufferToString(object), type) | ||
v: object | ||
}; | ||
case _toString.types.DATA_VIEW: | ||
case _toString.types.ARGUMENTS: | ||
case _toString.types.ARRAY: | ||
case _toString.types.OBJECT: | ||
return { | ||
v: prependTypeToString(arrayBufferToString(object.buffer), type) | ||
v: !!object ? object : prependTypeToString(object, type) | ||
}; | ||
case _toString.types.FLOAT_32_ARRAY: | ||
case _toString.types.FLOAT_64_ARRAY: | ||
case _toString.types.INT_8_ARRAY: | ||
case _toString.types.INT_16_ARRAY: | ||
case _toString.types.INT_32_ARRAY: | ||
case _toString.types.UINT_8_ARRAY: | ||
case _toString.types.UINT_8_CLAMPED_ARRAY: | ||
case _toString.types.UINT_16_ARRAY: | ||
case _toString.types.UINT_32_ARRAY: | ||
case _toString.types.ERROR: | ||
case _toString.types.NULL: | ||
case _toString.types.REGEXP: | ||
case _toString.types.UNDEFINED: | ||
return { | ||
v: type + ' [' + object.join(',') + ']' | ||
v: prependTypeToString(object, type) | ||
}; | ||
@@ -106,11 +104,14 @@ | ||
case _toString.types.ERROR: | ||
case _toString.types.NULL: | ||
case _toString.types.NUMBER: | ||
case _toString.types.REGEXP: | ||
case _toString.types.UNDEFINED: | ||
case _toString.types.SYMBOL: | ||
return { | ||
v: prependTypeToString(object, type) | ||
v: object.toString() | ||
}; | ||
case _toString.types.PROMISE: | ||
case _toString.types.WEAKMAP: | ||
case _toString.types.WEAKSET: | ||
return { | ||
v: prependTypeToString('NOT_ENUMERABLE', type) | ||
}; | ||
case _toString.types.MAP: | ||
@@ -128,12 +129,25 @@ case _toString.types.SET: | ||
case _toString.types.OBJECT: | ||
case _toString.types.ARRAY_BUFFER: | ||
return { | ||
v: !!object ? object : prependTypeToString(object, type) | ||
v: prependTypeToString(arrayBufferToString(object), type) | ||
}; | ||
case _toString.types.SYMBOL: | ||
case _toString.types.DATA_VIEW: | ||
return { | ||
v: object.toString() | ||
v: prependTypeToString(arrayBufferToString(object.buffer), type) | ||
}; | ||
case _toString.types.FLOAT_32_ARRAY: | ||
case _toString.types.FLOAT_64_ARRAY: | ||
case _toString.types.INT_8_ARRAY: | ||
case _toString.types.INT_16_ARRAY: | ||
case _toString.types.INT_32_ARRAY: | ||
case _toString.types.UINT_8_ARRAY: | ||
case _toString.types.UINT_8_CLAMPED_ARRAY: | ||
case _toString.types.UINT_16_ARRAY: | ||
case _toString.types.UINT_32_ARRAY: | ||
return { | ||
v: prependTypeToString(object.join(','), type) | ||
}; | ||
case _toString.types.MATH: | ||
@@ -150,9 +164,2 @@ var mathObject = {}; | ||
case _toString.types.PROMISE: | ||
case _toString.types.WEAKMAP: | ||
case _toString.types.WEAKSET: | ||
return { | ||
v: type + '--NOT_ENUMERABLE' | ||
}; | ||
default: | ||
@@ -184,2 +191,27 @@ return { | ||
switch (type) { | ||
case _toString.types.NUMBER: | ||
case _toString.types.STRING: | ||
return value; | ||
case _toString.types.ARGUMENTS: | ||
case _toString.types.ARRAY: | ||
case _toString.types.OBJECT: | ||
if (!value) { | ||
return prependTypeToString(value, type); | ||
} | ||
if (++recursiveCounter > 255) { | ||
return 'Undefined undefined'; | ||
} | ||
index = stack.indexOf(value); | ||
if (!~index) { | ||
stack.push(value); | ||
return value; | ||
} | ||
return '*Recursive-' + index; | ||
case _toString.types.ARRAY_BUFFER: | ||
@@ -212,22 +244,2 @@ case _toString.types.DATA_VIEW: | ||
case _toString.types.ARRAY: | ||
case _toString.types.OBJECT: | ||
if (!value) { | ||
return prependTypeToString(value, type); | ||
} | ||
if (++recursiveCounter > 255) { | ||
return 'Undefined undefined'; | ||
} | ||
index = stack.indexOf(value); | ||
if (!~index) { | ||
stack.push(value); | ||
return value; | ||
} | ||
return '*Recursive-' + index; | ||
default: | ||
@@ -283,5 +295,3 @@ return value; | ||
var stringify = function stringify(object) { | ||
var value = getValueForStringification(object); | ||
return tryCatch(value); | ||
return tryCatch(getValueForStringification(object)); | ||
}; | ||
@@ -288,0 +298,0 @@ |
@@ -83,3 +83,3 @@ { | ||
}, | ||
"version": "1.2.1" | ||
"version": "1.3.0" | ||
} |
@@ -5,3 +5,3 @@ # hashIt | ||
#### Installation | ||
### Installation | ||
@@ -12,3 +12,3 @@ ``` | ||
#### Usage | ||
### Usage | ||
@@ -33,3 +33,3 @@ ```javascript | ||
#### Overview | ||
### Overview | ||
@@ -53,2 +53,3 @@ `hashIt` has a simple goal: provide a fast and consistent hashCode for any object type that is uniquely based on its values. This has a number of uses such as duplication prevention or fast equality comparisons. | ||
Here is the list of object classes that have been tested and shown to produce unique hashCodes: | ||
* `Arguments` | ||
* `Array` | ||
@@ -87,6 +88,21 @@ * `ArrayBuffer` | ||
#### Additional functions | ||
### Utility functions | ||
* isEqual(object1, object2[, object3, ..., objectN]) *returns `Boolean`* | ||
* Compares all objects passed to it to determine if they are equal to one another based on hashCode | ||
**isEmpty(object)** *returns `Boolean`* | ||
Determines if object is empty based on hashCode, with empty defined as: | ||
* Empty array (`[]`) | ||
* Empty map (`new Map()`) | ||
* Empty number, or zero (`0`) | ||
* Empty object (`{}`) | ||
* Empty set (`new Set()`) | ||
* Empty string (`''`) | ||
* Undefined (`undefined`) | ||
* Null (`null`) | ||
This differs from the implementation by `lodash`, where a value is considered empty if they it is not an `Array`, `Object`, `Map`, or `Set`. Think of this definition of empty as "having no value(s)". | ||
**isEqual(object1, object2[, object3, ..., objectN])** *returns `Boolean`* | ||
Compares all objects passed to it to determine if they are equal to one another based on hashCode | ||
@@ -105,7 +121,15 @@ ```javascript | ||
#### Gotchas | ||
**isNull(object)** *returns `Boolean`* | ||
Determines if object is null based on hashCode | ||
**isUndefined(object)** *returns `Boolean`* | ||
Determines if object is undefined based on hashCode | ||
### Gotchas | ||
While the hashes will be consistent when calculated within the same browser environment, there is no guarantee that the hashCode will be the same across different browsers due to browser-specific implementations of features. A vast majority of the time things line up, but there are some edge cases that cause differences, so just something to be mindful of. | ||
#### Support | ||
### Support | ||
@@ -121,3 +145,3 @@ `hashIt` has been tested on the following browsers: | ||
#### Development | ||
### Development | ||
@@ -124,0 +148,0 @@ Standard stuff, clone the repo and `npm i` to get the dependencies. npm scripts available: |
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
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
75511
1099
152