1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- "use strict";
- const FNV_64_THRESHOLD = 1 << 24;
- const FNV_OFFSET_32 = 2166136261;
- const FNV_PRIME_32 = 16777619;
- const MASK_31 = 0x7fffffff;
- const FNV_OFFSET_64 = BigInt("0xCBF29CE484222325");
- const FNV_PRIME_64 = BigInt("0x100000001B3");
- function fnv1a32(str) {
- let hash = FNV_OFFSET_32;
- for (let i = 0, len = str.length; i < len; i++) {
- hash ^= str.charCodeAt(i);
-
- hash = Math.imul(hash, FNV_PRIME_32);
- }
-
- return hash & MASK_31;
- }
- function fnv1a64(str) {
- let hash = FNV_OFFSET_64;
- for (let i = 0, len = str.length; i < len; i++) {
- hash ^= BigInt(str.charCodeAt(i));
- hash = BigInt.asUintN(64, hash * FNV_PRIME_64);
- }
- return hash;
- }
- module.exports = (str, range) => {
- if (range < FNV_64_THRESHOLD) {
- return fnv1a32(str) % range;
- }
- return Number(fnv1a64(str) % BigInt(range));
- };
|