Skip to content

Commit ca070f8

Browse files
committed
fix(usernameToColor): revert to server-comatible algorithm
- restore previous test snapshots Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
1 parent e430228 commit ca070f8

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

src/functions/usernameToColor/index.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import type { Color } from '../../utils/colors.ts'
77

8+
import md5 from 'md5'
89
import { generatePalette } from '../../utils/colors.ts'
910

1011
/**
@@ -13,18 +14,23 @@ import { generatePalette } from '../../utils/colors.ts'
1314
* @param str - The string to hash
1415
*/
1516
function hashCode(str: string): number {
16-
let hash = 0
17+
let hash = str
1718

18-
if (str.length === 0) {
19-
return hash
19+
// Hash a given string, if it is not md5 hash already
20+
if (str.match(/^([0-9a-f]{4}-?){8}$/) === null) {
21+
hash = md5(str)
2022
}
2123

22-
for (let i = 0; i < str.length; i++) {
23-
const chr = str.charCodeAt(i)
24-
hash = ((hash << 5) - hash) + chr
24+
hash = hash.replace(/[^0-9a-f]/g, '')
25+
26+
let finalInt = 0
27+
28+
for (let i = 0; i < hash.length; i++) {
29+
// chars in md5 are [0-9a-f] (base-16)
30+
finalInt += parseInt(hash.charAt(i), 16)
2531
}
2632

27-
return Math.abs(hash)
33+
return finalInt
2834
}
2935

3036
/**
Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`usernameToColor > has the proper color 1`] = `"#b6469d"`;
3+
exports[`usernameToColor > has the proper color 1`] = `"#0082c9"`;
44

5-
exports[`usernameToColor > , has the proper color 1`] = `"#93b27b"`;
5+
exports[`usernameToColor > , has the proper color 1`] = `"#1e78c1"`;
66

7-
exports[`usernameToColor > . has the proper color 1`] = `"#499aa2"`;
7+
exports[`usernameToColor > . has the proper color 1`] = `"#c98879"`;
88

9-
exports[`usernameToColor > 123e4567-e89b-12d3-a456-426614174000 has the proper color 1`] = `"#795aab"`;
9+
exports[`usernameToColor > 123e4567-e89b-12d3-a456-426614174000 has the proper color 1`] = `"#bc5c91"`;
1010

11-
exports[`usernameToColor > Akeel Robertson has the proper color 1`] = `"#499aa2"`;
11+
exports[`usernameToColor > Akeel Robertson has the proper color 1`] = `"#9750a4"`;
1212

13-
exports[`usernameToColor > Alishia Ann Lowry has the proper color 1`] = `"#bc5c91"`;
13+
exports[`usernameToColor > Alishia Ann Lowry has the proper color 1`] = `"#d09e6d"`;
1414

15-
exports[`usernameToColor > Arham Johnson has the proper color 1`] = `"#795aab"`;
15+
exports[`usernameToColor > Arham Johnson has the proper color 1`] = `"#0082c9"`;
1616

17-
exports[`usernameToColor > Brayden Truong has the proper color 1`] = `"#c37285"`;
17+
exports[`usernameToColor > Brayden Truong has the proper color 1`] = `"#d09e6d"`;
1818

19-
exports[`usernameToColor > Daphne Roy has the proper color 1`] = `"#795aab"`;
19+
exports[`usernameToColor > Daphne Roy has the proper color 1`] = `"#9750a4"`;
2020

21-
exports[`usernameToColor > Ellena Wright Frederic Conway has the proper color 1`] = `"#6ea68f"`;
21+
exports[`usernameToColor > Ellena Wright Frederic Conway has the proper color 1`] = `"#c37285"`;
2222

23-
exports[`usernameToColor > Gianluca Hills has the proper color 1`] = `"#3c6eba"`;
23+
exports[`usernameToColor > Gianluca Hills has the proper color 1`] = `"#d6b461"`;
2424

25-
exports[`usernameToColor > Haseeb Stephens has the proper color 1`] = `"#795aab"`;
25+
exports[`usernameToColor > Haseeb Stephens has the proper color 1`] = `"#d6b461"`;
2626

27-
exports[`usernameToColor > Idris Mac has the proper color 1`] = `"#ddcb55"`;
27+
exports[`usernameToColor > Idris Mac has the proper color 1`] = `"#9750a4"`;
2828

29-
exports[`usernameToColor > Kristi Fisher has the proper color 1`] = `"#9750a4"`;
29+
exports[`usernameToColor > Kristi Fisher has the proper color 1`] = `"#0082c9"`;
3030

31-
exports[`usernameToColor > Lillian Wall has the proper color 1`] = `"#b8be68"`;
31+
exports[`usernameToColor > Lillian Wall has the proper color 1`] = `"#bc5c91"`;
3232

33-
exports[`usernameToColor > Lorelai Taylor has the proper color 1`] = `"#248eb5"`;
33+
exports[`usernameToColor > Lorelai Taylor has the proper color 1`] = `"#ddcb55"`;
3434

35-
exports[`usernameToColor > Madina Knight has the proper color 1`] = `"#bc5c91"`;
35+
exports[`usernameToColor > Madina Knight has the proper color 1`] = `"#9750a4"`;
3636

37-
exports[`usernameToColor > Meeting has the proper color 1`] = `"#248eb5"`;
37+
exports[`usernameToColor > Meeting has the proper color 1`] = `"#c98879"`;
3838

39-
exports[`usernameToColor > Private Circle has the proper color 1`] = `"#c98879"`;
39+
exports[`usernameToColor > Private Circle has the proper color 1`] = `"#c37285"`;
4040

41-
exports[`usernameToColor > Rae Hope has the proper color 1`] = `"#ddcb55"`;
41+
exports[`usernameToColor > Rae Hope has the proper color 1`] = `"#795aab"`;
4242

4343
exports[`usernameToColor > Santiago Singleton has the proper color 1`] = `"#bc5c91"`;
4444

45-
exports[`usernameToColor > Sid Combs has the proper color 1`] = `"#c37285"`;
45+
exports[`usernameToColor > Sid Combs has the proper color 1`] = `"#d09e6d"`;
4646

47-
exports[`usernameToColor > TestCircle has the proper color 1`] = `"#c37285"`;
47+
exports[`usernameToColor > TestCircle has the proper color 1`] = `"#499aa2"`;
4848

49-
exports[`usernameToColor > Tom Mörtel has the proper color 1`] = `"#3c6eba"`;
49+
exports[`usernameToColor > Tom Mörtel has the proper color 1`] = `"#248eb5"`;
5050

51-
exports[`usernameToColor > Vivienne Jacobs has the proper color 1`] = `"#93b27b"`;
51+
exports[`usernameToColor > Vivienne Jacobs has the proper color 1`] = `"#1e78c1"`;
5252

53-
exports[`usernameToColor > Zaki Cortes has the proper color 1`] = `"#5b64b3"`;
53+
exports[`usernameToColor > Zaki Cortes has the proper color 1`] = `"#6ea68f"`;
5454

55-
exports[`usernameToColor > a user has the proper color 1`] = `"#795aab"`;
55+
exports[`usernameToColor > a user has the proper color 1`] = `"#5b64b3"`;
5656

57-
exports[`usernameToColor > admin has the proper color 1`] = `"#9750a4"`;
57+
exports[`usernameToColor > admin has the proper color 1`] = `"#d09e6d"`;
5858

59-
exports[`usernameToColor > admin@cloud.example.com has the proper color 1`] = `"#c98879"`;
59+
exports[`usernameToColor > admin@cloud.example.com has the proper color 1`] = `"#9750a4"`;
6060

61-
exports[`usernameToColor > another user has the proper color 1`] = `"#499aa2"`;
61+
exports[`usernameToColor > another user has the proper color 1`] = `"#ddcb55"`;
6262

63-
exports[`usernameToColor > asd has the proper color 1`] = `"#ddcb55"`;
63+
exports[`usernameToColor > asd has the proper color 1`] = `"#248eb5"`;
6464

65-
exports[`usernameToColor > bar has the proper color 1`] = `"#6ea68f"`;
65+
exports[`usernameToColor > bar has the proper color 1`] = `"#0082c9"`;
6666

67-
exports[`usernameToColor > foo has the proper color 1`] = `"#b6469d"`;
67+
exports[`usernameToColor > foo has the proper color 1`] = `"#d09e6d"`;
6868

69-
exports[`usernameToColor > wasd has the proper color 1`] = `"#9750a4"`;
69+
exports[`usernameToColor > wasd has the proper color 1`] = `"#b6469d"`;
7070

71-
exports[`usernameToColor > مرحبا بالعالم has the proper color 1`] = `"#d09e6d"`;
71+
exports[`usernameToColor > مرحبا بالعالم has the proper color 1`] = `"#c98879"`;
7272

73-
exports[`usernameToColor > 🙈 has the proper color 1`] = `"#b8be68"`;
73+
exports[`usernameToColor > 🙈 has the proper color 1`] = `"#b6469d"`;

0 commit comments

Comments
 (0)