forked from lovell/farmhash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
102 lines (99 loc) · 3.02 KB
/
index.js
File metadata and controls
102 lines (99 loc) · 3.02 KB
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
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2014 Lovell Fuller and others.
// SPDX-License-Identifier: Apache-2.0
const farmhash = (function farmhashBinding() {
const platform = require('./platform');
try {
return require(`./src/build/Release/farmhash-${platform}.node`);
} catch (_err) {
try {
return require(`./build/farmhash-${platform}.node`);
} catch (_err) {
throw new Error(`Unsupported platform: ${platform}`);
}
}
})();
function verifySeed(seed) {
if (!Number.isInteger(seed)) {
throw new Error('Expected an integer for seed');
}
}
module.exports = {
// Hash methods - platform dependent
hash32: (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: (input, seed) => {
verifySeed(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: (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: (input, seed) => {
verifySeed(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: (input, seed1, seed2) => {
verifySeed(seed1);
verifySeed(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: (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: (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: (input) => {
if (typeof input === 'string') {
return BigInt.asIntN(64, farmhash.Fingerprint64String(input));
}
if (Buffer.isBuffer(input)) {
return BigInt.asIntN(64, farmhash.Fingerprint64Buffer(input));
}
throw new Error('Expected a String or Buffer for input');
},
};