Skip to content

Commit

Permalink
Add string hash
Browse files Browse the repository at this point in the history
  • Loading branch information
zjkmxy committed Sep 29, 2024
1 parent 06c83c0 commit 3064dac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ucla-irl/ndnts-aux",
"version": "3.0.4",
"version": "3.0.5",
"description": "NDNts Auxiliary Package for Web and Deno",
"scripts": {
"test": "deno test --no-check",
Expand Down
19 changes: 19 additions & 0 deletions src/utils/fnv-hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Calculate a 32 bit FNV-1a hash
* Found here: https://gist.github.com/vaiorabbit/5657561
* Ref.: http://isthe.com/chongo/tech/comp/fnv/
*
* @param str Input string to hash
* @param seed The seed. By default `0x811c9dc5` is used.
* @returns The FNV-1a hash in a 32bit number.
*/
export const hashFnv32a = (str: string, seed?: number): number => {
/*jshint bitwise:false */
let hval = seed ?? 0x811c9dc5;

for (let i = 0, l = str.length; i < l; i++) {
hval ^= str.charCodeAt(i);
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
}
return hval >>> 0;
};
1 change: 1 addition & 0 deletions src/utils/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './disposable-stacks.ts';
export * from './responder.ts';
export * from './event-chain.ts';
export * from './random-uuid.ts';
export * from './fnv-hash.ts';

0 comments on commit 3064dac

Please sign in to comment.