Skip to content

Commit

Permalink
Depend on string-hash for djb2 hashing algorithm
Browse files Browse the repository at this point in the history
This is where I copied the code for this algorithm from, seems like we
might as well just bring in the dependency for it.
  • Loading branch information
lencioni committed Mar 7, 2017
1 parent c64e57d commit 4866e61
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 22 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
},
"dependencies": {
"asap": "^2.0.3",
"inline-style-prefixer": "^2.0.0"
"inline-style-prefixer": "^2.0.0",
"string-hash": "^1.1.3"
},
"tonicExampleFilename": "examples/runkit.js"
}
23 changes: 2 additions & 21 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* @flow */
import stringHash from 'string-hash';

/* ::
type Pair = [ string, any ];
Expand Down Expand Up @@ -155,26 +156,6 @@ export const stringifyValue = (
}
};

/**
* JS Implementation of djb2
*
* @see https://github.com/darkskyapp/string-hash
*/

function djb2Hash(str) {
let hash = 5381;
let i = str.length;

while(i) {
hash = (hash * 33) ^ str.charCodeAt(--i);
}

/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed
* integers. Since we want the results to be always positive, convert the
* signed int to an unsigned by doing an unsigned bitshift. */
return hash >>> 0;
}

// Hash a javascript object using JSON.stringify. This is very fast, about 3
// microseconds on my computer for a sample object:
// http://jsperf.com/test-hashfnv32a-hash/5
Expand All @@ -183,7 +164,7 @@ function djb2Hash(str) {
// this to produce consistent hashes browsers need to have a consistent
// ordering of objects. Ben Alpert says that Facebook depends on this, so we
// can probably depend on this too.
export const hashObject = (object /* : ObjectMap */) /* : number */ => djb2Hash(JSON.stringify(object));
export const hashObject = (object /* : ObjectMap */) /* : number */ => stringHash(JSON.stringify(object));


const IMPORTANT_RE = /^([^:]+:.*?)( !important)?;$/;
Expand Down

0 comments on commit 4866e61

Please sign in to comment.