index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. const farmhash = (function farmhashBinding () {
  3. try {
  4. return require('./build/Release/farmhash.node');
  5. } catch (e) {
  6. return require('./build/Debug/farmhash.node');
  7. }
  8. }());
  9. // Input validation
  10. function verifyInteger (input) {
  11. if (typeof input !== 'number' || (input % 1) !== 0) {
  12. throw new Error('Expected an integer for seed');
  13. }
  14. }
  15. module.exports = {
  16. // Hash methods - platform dependent
  17. hash32: function (input) {
  18. if (typeof input === 'string') {
  19. return farmhash.Hash32String(input);
  20. }
  21. if (Buffer.isBuffer(input)) {
  22. return farmhash.Hash32Buffer(input);
  23. }
  24. throw new Error('Expected a String or Buffer for input');
  25. },
  26. hash32WithSeed: function (input, seed) {
  27. verifyInteger(seed);
  28. if (typeof input === 'string') {
  29. return farmhash.Hash32WithSeedString(input, seed);
  30. }
  31. if (Buffer.isBuffer(input)) {
  32. return farmhash.Hash32WithSeedBuffer(input, seed);
  33. }
  34. throw new Error('Expected a String or Buffer for input');
  35. },
  36. hash64: function (input) {
  37. if (typeof input === 'string') {
  38. return farmhash.Hash64String(input);
  39. }
  40. if (Buffer.isBuffer(input)) {
  41. return farmhash.Hash64Buffer(input);
  42. }
  43. throw new Error('Expected a String or Buffer for input');
  44. },
  45. hash64WithSeed: function (input, seed) {
  46. verifyInteger(seed);
  47. if (typeof input === 'string') {
  48. return farmhash.Hash64WithSeedString(input, seed);
  49. }
  50. if (Buffer.isBuffer(input)) {
  51. return farmhash.Hash64WithSeedBuffer(input, seed);
  52. }
  53. throw new Error('Expected a String or Buffer for input');
  54. },
  55. hash64WithSeeds: function (input, seed1, seed2) {
  56. verifyInteger(seed1);
  57. verifyInteger(seed2);
  58. if (typeof input === 'string') {
  59. return farmhash.Hash64WithSeedsString(input, seed1, seed2);
  60. }
  61. if (Buffer.isBuffer(input)) {
  62. return farmhash.Hash64WithSeedsBuffer(input, seed1, seed2);
  63. }
  64. throw new Error('Expected a String or Buffer for input');
  65. },
  66. // Fingerprint methods - platform independent
  67. fingerprint32: function (input) {
  68. if (typeof input === 'string') {
  69. return farmhash.Fingerprint32String(input);
  70. }
  71. if (Buffer.isBuffer(input)) {
  72. return farmhash.Fingerprint32Buffer(input);
  73. }
  74. throw new Error('Expected a String or Buffer for input');
  75. },
  76. fingerprint64: function (input) {
  77. if (typeof input === 'string') {
  78. return farmhash.Fingerprint64String(input);
  79. }
  80. if (Buffer.isBuffer(input)) {
  81. return farmhash.Fingerprint64Buffer(input);
  82. }
  83. throw new Error('Expected a String or Buffer for input');
  84. }
  85. };