-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
005fc8d
commit 6e27a04
Showing
6 changed files
with
61 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {hash} from './hash'; | ||
|
||
describe('hash', () => { | ||
it('should generate a hash from a string', () => { | ||
const result = hash('foo'); | ||
|
||
expect(result).toEqual('18cc6'); | ||
expect(result).toEqual(hash('foo')); | ||
}); | ||
|
||
it('should handle special characters', () => { | ||
expect(hash('✨')).toEqual('2728'); | ||
expect(hash('💥')).toEqual('d83d'); | ||
expect(hash('✨💥')).toEqual('59615'); | ||
}); | ||
|
||
it('should generate a hash from an empty string', () => { | ||
const result = hash(''); | ||
|
||
expect(result).toEqual('0'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export function hash(value: string): string { | ||
let code = 0; | ||
|
||
for (const char of value) { | ||
const charCode = char.charCodeAt(0); | ||
|
||
code = (code << 5) - code + charCode; | ||
code |= 0; // Convert to 32bit integer | ||
} | ||
|
||
return code.toString(16); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters