Skip to content

Commit

Permalink
Add a hash util function.
Browse files Browse the repository at this point in the history
Not particularly secure or anything, just looking for something consistent.

Used the same one as card-web.

Part of #55. Part of #25.
  • Loading branch information
jkomoros committed Jul 23, 2023
1 parent f9f4023 commit dad8cae
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,15 @@ export const setObjectProperty = (obj : NormalObject, path : string, value : unk
const subObject = isNaN(parseInt(parts[1])) ? {} : [];
obj[parts[0]] = subObject;
setObjectProperty(obj[parts[0]] as NormalObject, parts.slice(1).join('.'), value);
};

export const hash = (str : string) => {
//Adapted from https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
let hash = 0, i, chr;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};

0 comments on commit dad8cae

Please sign in to comment.