@pivoto/core
Version:

23 lines (20 loc) • 446 B
text/typescript
const MAX = Math.pow(2, 32);
function stringHashCode(str: string) {
str = str || '';
let h: number = 0;
let len = str.length;
for (let i = 0; i < len; i++) {
let c = str.charCodeAt(i);
h = 31 * h + Number(c);
if (h > MAX) {
h = h % MAX;
}
}
return h;
}
export function hashCode(obj: any) {
if (typeof obj === 'string') {
return stringHashCode(obj);
}
return stringHashCode((obj || '').toString());
}