Skip to content
This repository has been archived by the owner on Apr 27, 2019. It is now read-only.

Commit

Permalink
now using sub level and custom match_mapper, also changing matchers c…
Browse files Browse the repository at this point in the history
…auses reindex

- new applyChanges method - now has callback
  • Loading branch information
mmckegg committed Mar 19, 2013
1 parent e4a5cef commit 15fc7cc
Show file tree
Hide file tree
Showing 6 changed files with 465 additions and 169 deletions.
50 changes: 50 additions & 0 deletions hash_object.js
@@ -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)
}
}

0 comments on commit 15fc7cc

Please sign in to comment.