@alwatr/hash-string
Version:
A simple utility to generate a hash string.
73 lines (68 loc) • 2.4 kB
JavaScript
/* @alwatr/hash-string v5.2.0 */
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/main.ts
var main_exports = {};
__export(main_exports, {
djb2Hash: () => djb2Hash,
nanoHash: () => nanoHash
});
module.exports = __toCommonJS(main_exports);
var import_package_tracer = require("@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__: import_package_tracer.packageTracer.add("@alwatr/hash-string", "5.2.0");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
djb2Hash,
nanoHash
});
//# sourceMappingURL=main.cjs.map