Skip to content

Commit

Permalink
feat: hashcode
Browse files Browse the repository at this point in the history
  • Loading branch information
Benricheson101 committed Oct 21, 2024
1 parent 523830e commit ffe5e64
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
27 changes: 27 additions & 0 deletions lib/misc/hashCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/** Jenkins one_at_a_time hash https://en.wikipedia.org/wiki/Jenkins_hash_function */
export const jenkinsHash = (s: string) => {
let hash = 0;

for (let i = 0; i < s.length; i++) {
hash += s.charCodeAt(i);
hash += hash << 10;
hash ^= hash >>> 6;
}

hash += hash << 3;
hash ^= hash >>> 11;
hash += hash << 15;

return hash;
};

/** Java hashCode() i think */
export const hashCode = (s: string) => {
let hash = 0;
for (let i = 0; i < s.length; i++) {
hash = (hash << 5) - hash + s.charCodeAt(i);
hash |= 0;
}

return hash;
};
1 change: 1 addition & 0 deletions lib/misc/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './hashCode';
export * from './luhnCheck';
12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,12 @@
"fmt": "biome check . --write",
"test": "node --disable-warning=ExperimentalWarning --test --enable-source-maps"
},
"keywords": [
"util",
"utility"
],
"keywords": ["util", "utility"],
"license": "MIT",
"devDependencies": {
"@biomejs/biome": "^1.9.1",
"@types/node": "^20.11.16",
"typescript": "^5.3.3"
},
"files": [
"build",
"biome.json",
"tsconfig.json",
"LICENSE"
]
"files": ["build", "biome.json", "tsconfig.json", "LICENSE"]
}

0 comments on commit ffe5e64

Please sign in to comment.