(str: string)
| 6 | * @returns A positive 32-bit integer hash |
| 7 | */ |
| 8 | export function stringHash(str: string): number { |
| 9 | let hash = 0; |
| 10 | for (let i = 0; i < str.length; i++) { |
| 11 | const char = str.charCodeAt(i); |
| 12 | hash = (hash << 5) - hash + char; |
| 13 | hash &= hash; // Convert to 32bit integer |
| 14 | } |
| 15 | return Math.abs(hash); |
| 16 | } |