Comparing version 1.0.2 to 2.0.0
@@ -40,3 +40,3 @@ 'use strict'; | ||
} | ||
if (typeof value === 'object') { | ||
if (typeof value === 'object' || typeof value === 'function') { | ||
if (seen.indexOf(value) !== -1) { | ||
@@ -46,3 +46,14 @@ return fold(hash, '[Circular]' + key); | ||
seen.push(value); | ||
return foldObject(hash, value, seen); | ||
var objHash = foldObject(hash, value, seen) | ||
if (!('valueOf' in value) || typeof value.valueOf !== 'function') { | ||
return objHash; | ||
} | ||
try { | ||
return fold(objHash, String(value.valueOf())) | ||
} catch (err) { | ||
return fold(objHash, '[valueOf exception]' + (err.stack || err.message)) | ||
} | ||
} | ||
@@ -49,0 +60,0 @@ return fold(hash, value.toString()); |
{ | ||
"name": "hash-sum", | ||
"description": "Blazing fast unique hash generator", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/bevacqua/hash-sum", | ||
@@ -25,4 +25,5 @@ "authors": [ | ||
"jshint-stylish": "0.2.0", | ||
"lodash": "4.17.11", | ||
"tape": "3.0.3" | ||
} | ||
} |
@@ -27,13 +27,14 @@ # hash-sum | ||
# creates unique hashes | ||
creates unique hashes | ||
4d237d49 from: [ 0, 1, 2, 3 ] | ||
766ec173 from: { url: 12 } | ||
2f473108 from: { headers: 12 } | ||
23308836 from: { headers: 122 } | ||
062bce44 from: { headers: '122' } | ||
acb9f66e from: { headers: { accept: 'text/plain' } } | ||
1c365a2d from: { payload: [ 0, 1, 2, 3 ], headers: [ { a: 'b' } ] } | ||
7319ae9d from: { a: [Function] } | ||
8a3a0e86 from: { b: [Function] } | ||
b6d7f5d4 from: { b: [Function] } | ||
00a34759 from: [ 0, 1, 2, 3 ] | ||
a8996f0c from: { '0': 0, '1': 1, '2': 2, '3': 3 } | ||
5b4c2116 from: { '0': 0, '1': 1, '2': 2, '3': 3, length: 4 } | ||
2c937c45 from: { url: 12 } | ||
31d55010 from: { headers: 12 } | ||
2d2e11bc from: { headers: 122 } | ||
ec99d958 from: { headers: '122' } | ||
18c00eee from: { headers: { accept: 'text/plain' } } | ||
6cb332c8 from: { payload: [ 0, 1, 2, 3 ], headers: [ { a: 'b' } ] } | ||
12ff55db from: { a: [Function: a] } | ||
46f806d2 from: { b: [Function: b] } | ||
0660d9c4 from: { b: [Function: b] } | ||
6c95fc65 from: function () {} | ||
@@ -55,11 +56,17 @@ 2941766e from: function (a) {} | ||
01e34ba8 from: true | ||
8a8f9624 from: Infinity | ||
0315bf8f from: -Infinity | ||
64a48b16 from: NaN | ||
1a96284a from: 0 | ||
1a96284b from: 1 | ||
29172c1a from: undefined | ||
4505230f from: {} | ||
3718c6e8 from: { a: {}, b: {} } | ||
59322f29 from: {} | ||
095b3a22 from: { a: {}, b: {} } | ||
63be56dd from: { valueOf: [Function: valueOf] } | ||
63be4f5c from: { valueOf: [Function: valueOf] } | ||
5d844489 from: [] | ||
938eaaf0 from: Tue Jul 14 2015 15:35:36 GMT-0300 (ART) | ||
dfe5fb2e from: global | ||
ok 1 should be equal | ||
ba0bfa14 from: 2019-06-28T21:24:31.215Z | ||
49324d16 from: 2019-06-28T03:00:00.000Z | ||
434c9188 from: 1988-06-09T03:00:00.000Z | ||
ce1b5e44 from: global | ||
``` | ||
@@ -66,0 +73,0 @@ |
95
test.js
@@ -8,43 +8,59 @@ 'use strict'; | ||
test('creates unique hashes', function (t) { | ||
var results = []; | ||
sub([0,1,2,3]); | ||
sub({url:12}); | ||
sub({headers:12}); | ||
sub({headers:122}); | ||
sub({headers:'122'}); | ||
sub({headers:{accept:'text/plain'}}); | ||
sub({payload:[0,1,2,3],headers:[{a:'b'}]}); | ||
sub({a:function () {}}); | ||
sub({b:function () {}}); | ||
sub({b:function (a) {}}); | ||
sub(function () {}); | ||
sub(function (a) {}); | ||
sub(function (b) {}); | ||
sub(function (a) { return a;}); | ||
sub(function (a) {return a;}); | ||
sub(''); | ||
sub('null'); | ||
sub('false'); | ||
sub('true'); | ||
sub('0'); | ||
sub('1'); | ||
sub('void 0'); | ||
sub('undefined'); | ||
sub(null); | ||
sub(false); | ||
sub(true); | ||
sub(0); | ||
sub(1); | ||
sub(void 0); | ||
sub({}); | ||
sub({a:{},b:{}}); | ||
sub([]); | ||
sub(new Date()); | ||
sub(global, 'global'); | ||
t.equal(results.length, _.uniq(results).length); | ||
var cases = []; | ||
test_case([0,1,2,3]); | ||
test_case({0:0,1:1,2:2,3:3}); | ||
test_case({0:0,1:1,2:2,3:3,length:4}); | ||
test_case({url:12}); | ||
test_case({headers:12}); | ||
test_case({headers:122}); | ||
test_case({headers:'122'}); | ||
test_case({headers:{accept:'text/plain'}}); | ||
test_case({payload:[0,1,2,3],headers:[{a:'b'}]}); | ||
test_case({a:function () {}}); | ||
test_case({b:function () {}}); | ||
test_case({b:function (a) {}}); | ||
test_case(function () {}); | ||
test_case(function (a) {}); | ||
test_case(function (b) {}); | ||
test_case(function (a) { return a;}); | ||
test_case(function (a) {return a;}); | ||
test_case('', '\'\''); | ||
test_case('null', '\'null\''); | ||
test_case('false', '\'false\''); | ||
test_case('true', '\'true\''); | ||
test_case('0', '\'0\''); | ||
test_case('1', '\'1\''); | ||
test_case('void 0', '\'void 0\''); | ||
test_case('undefined', '\'undefined\''); | ||
test_case(null); | ||
test_case(false); | ||
test_case(true); | ||
test_case(Infinity); | ||
test_case(-Infinity); | ||
test_case(NaN); | ||
test_case(0); | ||
test_case(1); | ||
test_case(void 0); | ||
test_case({}); | ||
test_case({a:{},b:{}}); | ||
test_case({valueOf(){return 1}}); | ||
test_case({valueOf(){return 2}}); | ||
test_case([]); | ||
test_case(new Date()); | ||
test_case(new Date(2019, 5, 28)); | ||
test_case(new Date(1988, 5, 9)); | ||
test_case(global, 'global'); | ||
const uniqCases = _.uniqBy(cases, 'hash') | ||
_.uniqBy(cases, 'hash').forEach(function (expected) { | ||
var matches = _.filter(cases, { hash: expected.hash }) | ||
t.equal(matches.length, 1, expected.hash + ': ' + _.map(matches, 'value').join(' ')) | ||
}) | ||
t.end(); | ||
function sub (value, name) { | ||
function test_case(value, name) { | ||
var hash = sum(value); | ||
results.push(hash); | ||
cases.push({ value, hash }); | ||
console.log('%s from:', hash, name || value); | ||
@@ -55,4 +71,7 @@ } | ||
test('hashes clash if same properties', function (t) { | ||
equals(function () {}, function () {}); | ||
equals(function (a) {}, function (a) {}); | ||
equals({a:'1'},{a:'1'}); | ||
equals({a:'1',b:1},{b:1,a:'1'}); | ||
equals({valueOf(){return 1}},{valueOf(){return 1}}); | ||
t.end(); | ||
@@ -59,0 +78,0 @@ |
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
8128
131
75
4
9