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