@saulx/utils
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -18,10 +18,10 @@ "use strict"; | ||
if (type === 'string') { | ||
hash = (exports.stringHash(field, hash) * 33) ^ exports.stringHash(key, hash); | ||
hash = (exports.stringHash(field, hash) * 33) ^ (exports.stringHash(key, hash) * 33); | ||
} | ||
else if (type === 'number') { | ||
hash = (((hash * 33) ^ field) * 33) ^ exports.stringHash(key, hash); | ||
hash = (((hash * 33) ^ field) * 33) ^ (exports.stringHash(key, hash) * 33); | ||
} | ||
else if (type === 'object') { | ||
if (field === null) { | ||
hash = 5381 ^ exports.stringHash(key, hash); | ||
hash = 5381 ^ (exports.stringHash(key, hash) * 33); | ||
} | ||
@@ -31,3 +31,3 @@ else { | ||
(exports.hashObjectIgnoreKeyOrderNest(field, hash) * 33) ^ | ||
exports.stringHash(key, hash); | ||
(exports.stringHash(key, hash) * 33); | ||
} | ||
@@ -38,3 +38,3 @@ } | ||
(((hash * 33) ^ (field === true ? 1 : 0)) * 33) ^ | ||
exports.stringHash(key, hash); | ||
(exports.stringHash(key, hash) * 33); | ||
} | ||
@@ -51,10 +51,10 @@ } | ||
if (type === 'string') { | ||
hash = (exports.stringHash(field, hash) * 33) ^ exports.stringHash(key, hash); | ||
hash = (exports.stringHash(field, hash) * 33) ^ (exports.stringHash(key, hash) * 33); | ||
} | ||
else if (type === 'number') { | ||
hash = (((hash * 33) ^ field) * 33) ^ exports.stringHash(key, hash); | ||
hash = (((hash * 33) ^ field) * 33) ^ (exports.stringHash(key, hash) * 33); | ||
} | ||
else if (type === 'object') { | ||
if (field === null) { | ||
hash = 5381 ^ exports.stringHash(key, hash); | ||
hash = 5381 ^ (exports.stringHash(key, hash) * 33); | ||
} | ||
@@ -64,3 +64,3 @@ else { | ||
(exports.hashObjectIgnoreKeyOrderNest(field, hash) * 33) ^ | ||
exports.stringHash(key, hash); | ||
(exports.stringHash(key, hash) * 33); | ||
} | ||
@@ -71,3 +71,3 @@ } | ||
(((hash * 33) ^ (field === true ? 1 : 0)) * 33) ^ | ||
exports.stringHash(key, hash); | ||
(exports.stringHash(key, hash) * 33); | ||
} | ||
@@ -79,3 +79,2 @@ } | ||
exports.hashObjectNest = (obj, hash = 5381) => { | ||
// if (obj.constructor === Array) { | ||
for (let key in obj) { | ||
@@ -85,13 +84,13 @@ const field = obj[key]; | ||
if (type === 'string') { | ||
hash = (exports.stringHash(field, hash) * 33) ^ exports.stringHash(key, hash); | ||
hash = (exports.stringHash(field, hash) * 33) ^ (exports.stringHash(key, hash) * 33); | ||
} | ||
else if (type === 'number') { | ||
hash = (((hash * 33) ^ field) * 33) ^ exports.stringHash(key, hash); | ||
hash = (((hash * 33) ^ field) * 33) ^ (exports.stringHash(key, hash) * 33); | ||
} | ||
else if (type === 'object') { | ||
if (field === null) { | ||
hash = 5381 ^ exports.stringHash(key, hash); | ||
hash = 5381 ^ (exports.stringHash(key, hash) * 33); | ||
} | ||
else { | ||
hash = (exports.hashObjectNest(field, hash) * 33) ^ exports.stringHash(key, hash); | ||
hash = (exports.hashObjectNest(field, hash) * 33) ^ (exports.stringHash(key, hash) * 33); | ||
} | ||
@@ -101,28 +100,6 @@ } | ||
hash = | ||
(((hash * 33) ^ (field === true ? 1 : 0)) * 33) ^ exports.stringHash(key, hash); | ||
(((hash * 33) ^ (field === true ? 1 : 0)) * 33) ^ | ||
(exports.stringHash(key, hash) * 33); | ||
} | ||
} | ||
// } else { | ||
// const keys = Object.keys(obj).sort() | ||
// for (let i = 0; i < keys.length; i++) { | ||
// const key = keys[i] | ||
// const field = obj[key] | ||
// const type = typeof field | ||
// if (type === 'string') { | ||
// hash = (stringHash(field, hash) * 33) ^ stringHash(key, hash) | ||
// } else if (type === 'number') { | ||
// hash = (((hash * 33) ^ field) * 33) ^ stringHash(key, hash) | ||
// } else if (type === 'object') { | ||
// if (field === null) { | ||
// hash = 5381 ^ stringHash(key, hash) | ||
// } else { | ||
// hash = (hashObjectNest(field, hash) * 33) ^ stringHash(key, hash) | ||
// } | ||
// } else if (type === 'boolean') { | ||
// hash = | ||
// (((hash * 33) ^ (field === true ? 1 : 0)) * 33) ^ | ||
// stringHash(key, hash) | ||
// } | ||
// } | ||
// } | ||
return hash; | ||
@@ -129,0 +106,0 @@ }; |
{ | ||
"name": "@saulx/utils", | ||
"main": "./dist/index.js", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"scripts": { | ||
@@ -6,0 +6,0 @@ "build": "tsc", |
@@ -17,12 +17,12 @@ export const stringHash = (str, hash = 5381): number => { | ||
if (type === 'string') { | ||
hash = (stringHash(field, hash) * 33) ^ stringHash(key, hash) | ||
hash = (stringHash(field, hash) * 33) ^ (stringHash(key, hash) * 33) | ||
} else if (type === 'number') { | ||
hash = (((hash * 33) ^ field) * 33) ^ stringHash(key, hash) | ||
hash = (((hash * 33) ^ field) * 33) ^ (stringHash(key, hash) * 33) | ||
} else if (type === 'object') { | ||
if (field === null) { | ||
hash = 5381 ^ stringHash(key, hash) | ||
hash = 5381 ^ (stringHash(key, hash) * 33) | ||
} else { | ||
hash = | ||
(hashObjectIgnoreKeyOrderNest(field, hash) * 33) ^ | ||
stringHash(key, hash) | ||
(stringHash(key, hash) * 33) | ||
} | ||
@@ -32,3 +32,3 @@ } else if (type === 'boolean') { | ||
(((hash * 33) ^ (field === true ? 1 : 0)) * 33) ^ | ||
stringHash(key, hash) | ||
(stringHash(key, hash) * 33) | ||
} | ||
@@ -44,12 +44,12 @@ } | ||
if (type === 'string') { | ||
hash = (stringHash(field, hash) * 33) ^ stringHash(key, hash) | ||
hash = (stringHash(field, hash) * 33) ^ (stringHash(key, hash) * 33) | ||
} else if (type === 'number') { | ||
hash = (((hash * 33) ^ field) * 33) ^ stringHash(key, hash) | ||
hash = (((hash * 33) ^ field) * 33) ^ (stringHash(key, hash) * 33) | ||
} else if (type === 'object') { | ||
if (field === null) { | ||
hash = 5381 ^ stringHash(key, hash) | ||
hash = 5381 ^ (stringHash(key, hash) * 33) | ||
} else { | ||
hash = | ||
(hashObjectIgnoreKeyOrderNest(field, hash) * 33) ^ | ||
stringHash(key, hash) | ||
(stringHash(key, hash) * 33) | ||
} | ||
@@ -59,3 +59,3 @@ } else if (type === 'boolean') { | ||
(((hash * 33) ^ (field === true ? 1 : 0)) * 33) ^ | ||
stringHash(key, hash) | ||
(stringHash(key, hash) * 33) | ||
} | ||
@@ -68,3 +68,2 @@ } | ||
export const hashObjectNest = (obj, hash = 5381): number => { | ||
// if (obj.constructor === Array) { | ||
for (let key in obj) { | ||
@@ -74,40 +73,17 @@ const field = obj[key] | ||
if (type === 'string') { | ||
hash = (stringHash(field, hash) * 33) ^ stringHash(key, hash) | ||
hash = (stringHash(field, hash) * 33) ^ (stringHash(key, hash) * 33) | ||
} else if (type === 'number') { | ||
hash = (((hash * 33) ^ field) * 33) ^ stringHash(key, hash) | ||
hash = (((hash * 33) ^ field) * 33) ^ (stringHash(key, hash) * 33) | ||
} else if (type === 'object') { | ||
if (field === null) { | ||
hash = 5381 ^ stringHash(key, hash) | ||
hash = 5381 ^ (stringHash(key, hash) * 33) | ||
} else { | ||
hash = (hashObjectNest(field, hash) * 33) ^ stringHash(key, hash) | ||
hash = (hashObjectNest(field, hash) * 33) ^ (stringHash(key, hash) * 33) | ||
} | ||
} else if (type === 'boolean') { | ||
hash = | ||
(((hash * 33) ^ (field === true ? 1 : 0)) * 33) ^ stringHash(key, hash) | ||
(((hash * 33) ^ (field === true ? 1 : 0)) * 33) ^ | ||
(stringHash(key, hash) * 33) | ||
} | ||
} | ||
// } else { | ||
// const keys = Object.keys(obj).sort() | ||
// for (let i = 0; i < keys.length; i++) { | ||
// const key = keys[i] | ||
// const field = obj[key] | ||
// const type = typeof field | ||
// if (type === 'string') { | ||
// hash = (stringHash(field, hash) * 33) ^ stringHash(key, hash) | ||
// } else if (type === 'number') { | ||
// hash = (((hash * 33) ^ field) * 33) ^ stringHash(key, hash) | ||
// } else if (type === 'object') { | ||
// if (field === null) { | ||
// hash = 5381 ^ stringHash(key, hash) | ||
// } else { | ||
// hash = (hashObjectNest(field, hash) * 33) ^ stringHash(key, hash) | ||
// } | ||
// } else if (type === 'boolean') { | ||
// hash = | ||
// (((hash * 33) ^ (field === true ? 1 : 0)) * 33) ^ | ||
// stringHash(key, hash) | ||
// } | ||
// } | ||
// } | ||
return hash | ||
@@ -114,0 +90,0 @@ } |
@@ -72,1 +72,51 @@ import test from 'ava' | ||
}) | ||
test('hash weird equality hashObjectIgnoreKeyOrder', async t => { | ||
const a = { | ||
type: 'folder', | ||
title: '', | ||
id: 'fo1', | ||
name: '', | ||
children: [ | ||
{ | ||
buttonText: 'my ballz', | ||
type: 'match', | ||
name: '', | ||
id: 'ma1', | ||
aliases: [], | ||
published: false | ||
} | ||
], | ||
aliases: [] | ||
} | ||
const b = { | ||
type: 'folder', | ||
title: '', | ||
id: 'fo1', | ||
name: '', | ||
children: [ | ||
{ | ||
buttonText: 'my ballzzzz', | ||
type: 'match', | ||
name: '', | ||
id: 'ma1', | ||
aliases: [], | ||
published: false | ||
} | ||
], | ||
aliases: [] | ||
} | ||
const hashA1 = hashObject(a) | ||
const hashB1 = hashObject(b) | ||
const hashA = hashObjectIgnoreKeyOrder(a) | ||
const hashB = hashObjectIgnoreKeyOrder(b) | ||
const hashStrA = hash('my ballz') | ||
const hashStrB = hash('my ballzzzz') | ||
t.true(hashStrA !== hashStrB) | ||
t.true(hashA1 !== hashB1) | ||
t.true(hashA !== hashB) | ||
}) |
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
19882
405