@alwatr/hash-string
Version:
A simple utility to generate a hash string.
48 lines (43 loc) • 1.34 kB
JavaScript
/* @alwatr/hash-string v5.2.0 */
// src/main.ts
import { packageTracer } from "@alwatr/package-tracer";
// src/djb2-hash.ts
function djb2Hash(str) {
let hashValue = 5381;
for (let i = str.length - 1; i >= 0; i--) {
hashValue = (hashValue << 5) + hashValue ^ str.charCodeAt(i);
}
return hashValue >>> 0;
}
// src/nano-hash.ts
function nanoHash(str, prefix, repeat = 1) {
if (repeat < 1) {
throw new Error("The repeat parameter must be greater than or equal to 1");
}
let hash1 = 3735928559;
let hash2 = 1103547991;
if (typeof str === "number") {
str = str.toString();
}
const len = str.length;
for (let i = 0; i < len; i++) {
const char = str.charCodeAt(i);
hash1 = Math.imul(hash1 ^ char, 2654435761);
hash2 = Math.imul(hash2 ^ char, 1597334677);
}
hash1 = Math.imul(hash1 ^ hash1 >>> 16, 2246822507) ^ Math.imul(hash2 ^ hash2 >>> 13, 3266489909);
hash2 = Math.imul(hash2 ^ hash2 >>> 16, 2246822507) ^ Math.imul(hash1 ^ hash1 >>> 13, 3266489909);
const result = prefix + (hash1 >>> 0).toString(36) + (hash2 >>> 0).toString(36);
if (repeat === 1) {
return result;
} else {
return nanoHash(result, prefix, repeat - 1);
}
}
// src/main.ts
__dev_mode__: packageTracer.add("@alwatr/hash-string", "5.2.0");
export {
djb2Hash,
nanoHash
};
//# sourceMappingURL=main.mjs.map