2014-06-04 19:52:08 +00:00
|
|
|
// Adapt encodings to work with Buffer or Uint8Array, they expect the latter
|
|
|
|
function decode(buf) {
|
2014-06-06 15:14:52 +00:00
|
|
|
return buf.toString('utf8');
|
2014-06-04 19:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function encode(string) {
|
2014-06-06 15:14:52 +00:00
|
|
|
return new Buffer(string, 'utf8');
|
2014-06-04 19:52:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:33:45 +00:00
|
|
|
// https://github.com/darkskyapp/string-hash
|
|
|
|
function hash32(string) {
|
|
|
|
var hash = 5381;
|
|
|
|
var i = string.length;
|
|
|
|
|
|
|
|
while(i) {
|
|
|
|
hash = (hash * 33) ^ string.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;
|
|
|
|
}
|
|
|
|
|
2014-06-04 19:52:08 +00:00
|
|
|
module.exports = {
|
|
|
|
encode: encode,
|
2018-05-15 17:33:45 +00:00
|
|
|
decode: decode,
|
|
|
|
hash32: hash32
|
2014-06-04 19:52:08 +00:00
|
|
|
};
|