life-towers/src/app/utils/hash.ts
Andras Schmelczer d612678682 more refactor
2019-09-06 23:16:05 +02:00

12 lines
356 B
TypeScript

export const hash = (text: string): number => {
// Return number from [0, 1)
if (!text) {
return 0;
}
const hashValue = Array.prototype.reduce.call(
text, // tslint:disable-next-line:no-bitwise
(value, char) => ((value << 5) - value + (char.charCodeAt(0) as number)) | 0,
7
);
return hashValue / (Math.pow(2, 32) - 2) + 0.5;
};