6bithash
Version:
**Hash function package** for hashing id's ( in positive integer ) to an alphanumeric short string using a 6 bit map ( User can also prvide thier own Custom 6 bit map )
27 lines (22 loc) • 858 B
text/typescript
import { customMap } from "./customMap";
import { GenerateHashOptions } from "./types"
export function generateHash(dbid: number , options : GenerateHashOptions = {}): string {
const { myMap = customMap } = options;
if (dbid < 0 || !Number.isInteger(dbid)) {
throw new Error('Input must be a non-negative integer.');
}
let binary = dbid.toString(2);
const padding = '0'.repeat((6 - (binary.length % 6)) % 6);
binary = padding + binary;
const chunks = binary.match(/.{1,6}/g);
if (!chunks) {
throw new Error('Failed to split binary string into chunks.');
}
const hash = chunks.map(chunk => {
if (!(chunk in myMap)) {
throw new Error(`Mapping not found for binary chunk: ${chunk}`);
}
return myMap[chunk];
}).join('');
return hash;
}