@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
45 lines (44 loc) • 1.12 kB
JavaScript
// Deprecated hashing function, used for split bucketing. Replaced by murmur3
Object.defineProperty(exports, "__esModule", { value: true });
exports.bucket = exports.hash = void 0;
//
// JAVA reference implementation for the hashing function.
//
// int h = 0;
// for (int i = 0; i < key.length(); i++) {
// h = 31 * h + key.charAt(i);
// }
// return h ^ seed; // XOR the hash and seed
//
function ToInteger(x) {
x = Number(x);
return x < 0 ? Math.ceil(x) : Math.floor(x);
}
function modulo(a, b) {
return a - Math.floor(a / b) * b;
}
function ToUint32(x) {
return modulo(ToInteger(x), Math.pow(2, 32));
}
function ToInt32(x) {
var uint32 = ToUint32(x);
if (uint32 >= Math.pow(2, 31)) {
return uint32 - Math.pow(2, 32);
}
else {
return uint32;
}
}
function hash(str, seed) {
var h = 0;
for (var i = 0; i < str.length; i++) {
h = ToInt32(ToInt32(31 * h) + str.charCodeAt(i));
}
return ToInt32(h ^ seed);
}
exports.hash = hash;
function bucket(str, seed) {
return Math.abs(hash(str, seed) % 100) + 1;
}
exports.bucket = bucket;
;