feat: some moving around
This commit is contained in:
11
app/src/lib/helpers/fastHash.test.ts
Normal file
11
app/src/lib/helpers/fastHash.test.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { test, expect } from 'vitest';
|
||||
import fastHash from './fastHash';
|
||||
|
||||
test('Hashes dont clash', () => {
|
||||
const hashA = fastHash('abcdef');
|
||||
const hashB = fastHash('abcde');
|
||||
const hashC = fastHash('abcde');
|
||||
|
||||
expect(hashA).not.toEqual(hashB);
|
||||
expect(hashB).toEqual(hashC);
|
||||
});
|
14
app/src/lib/helpers/fastHash.ts
Normal file
14
app/src/lib/helpers/fastHash.ts
Normal file
@ -0,0 +1,14 @@
|
||||
// Shamelessly copied from
|
||||
// https://stackoverflow.com/a/8831937
|
||||
|
||||
export default function (input: string) {
|
||||
if (input.length === 0) return 0;
|
||||
|
||||
let hash = 0;
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
hash = (hash << 5) - hash + input.charCodeAt(i);
|
||||
hash = hash & hash;
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
Reference in New Issue
Block a user