royaltm / node-murmurhash-native

MurmurHash native bindings for node
MIT License
48 stars 7 forks source link

wrong function header definitions #20

Closed ShynRou closed 5 years ago

ShynRou commented 5 years ago

Hey,

I stumbled about an error: When trying to use the defined function header from the documentation murmurHash(data{string}, encoding|output_type[, seed][, callback])

It won't work and return most of the time 0 But if you use the following and put the seed between the data and the encoding you get the right results. murmurHash(data, seed, output_type[, callback])

royaltm commented 5 years ago

Could you write me the exact arguments you have used and their types? Or better yet, make a gist with a complete erroneous example?

royaltm commented 5 years ago

Ok, I've got it: murmurHash('foo','hex') 0

royaltm commented 5 years ago

Thanks for reporting that.

royaltm commented 5 years ago

However the behavior is correct. It's not an error. I will add a remark to the readme perhaps to clarify.

This is because the argument is encoding|output_type The encoding has higher priority.

The possible encodings are: "ascii"|"base64"|"binary"|"hex"|"ucs-2"|"ucs2"|"utf-16le"|"utf-8"|"utf16le"|"utf8" The possible output_types are: "base64"|"binary"|"buffer"|"hex"|"number". So in case of encoding|output_type the only output_type recognized will be "buffer" and "number" because these are not valid encodings.

You can see them here in ts declarations.

murmurHash('foo', 'buffer')
<Buffer f6 a5 c4 20>
murmurHash('foo', 'number')
4138058784
murmurHash('foo', 'buffer',2)
<Buffer 20 8c 7f 62>

To fully disambiguate between encoding and output_type you need to put a seed or encoding argument prior to output_type argument:

murmurHash('foo', 0, 'hex')
'f6a5c420'
murmurHash('foo', 'utf8', 'hex')
'f6a5c420'
royaltm commented 5 years ago

I hope it clarifies: https://github.com/royaltm/node-murmurhash-native/commit/fc53743bc3b02b69afce790bdb08168aecf3c3d2

ShynRou commented 5 years ago

Ah, okey. I would suggest reducing the complexity in the function overloading.