This repository has been archived by the owner on Apr 27, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
now using sub level and custom match_mapper, also changing matchers c…
…auses reindex - new applyChanges method - now has callback
- Loading branch information
Showing
6 changed files
with
465 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var crypto = require('crypto') | ||
|
||
module.exports = function(object, algo){ | ||
var string = canonicalJSON(object) | ||
if (algo === 'djb2'){ | ||
return djb2(string).toString(36) | ||
} else { | ||
return crypto.createHash(algo || 'sha1').update(string).digest('base64') | ||
} | ||
} | ||
|
||
function djb2(str) { | ||
var hash = 5381 | ||
for (i = 0; i < str.length; i++) { | ||
char = str.charCodeAt(i) | ||
hash = ((hash << 5) + hash) + char | ||
} | ||
return hash | ||
} | ||
|
||
function canonicalJSON(object){ | ||
if (object == null){ | ||
return 'null' | ||
} | ||
|
||
var object = object.valueOf() | ||
|
||
if (object instanceof Array){ | ||
var result = "[" | ||
|
||
object.forEach(function(value, i){ | ||
if (i > 0) result += ',' | ||
result += canonicalJSON(value) | ||
}) | ||
|
||
return result + ']' | ||
} else if (object instanceof Object){ | ||
var result = "{" | ||
|
||
Object.keys(object).sort().forEach(function(key, i){ | ||
var value = object[key] | ||
if (i > 0) result += ',' | ||
result += JSON.stringify(key) + ':' + canonicalJSON(value) | ||
}) | ||
|
||
return result + '}' | ||
} else { | ||
return JSON.stringify(object) | ||
} | ||
} |
Oops, something went wrong.