Skip to content

Commit c870903

Browse files
committed
refactor: drop md5 dependency for usernameToColor and migrate to TS
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 9c1a843 commit c870903

File tree

6 files changed

+45
-92
lines changed

6 files changed

+45
-92
lines changed

package-lock.json

Lines changed: 2 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
"floating-vue": "^5.2.2",
9595
"focus-trap": "^7.6.4",
9696
"linkify-string": "^4.2.0",
97-
"md5": "^2.3.0",
9897
"p-queue": "^8.0.1",
9998
"rehype-external-links": "^3.0.0",
10099
"rehype-highlight": "^7.0.2",

src/functions/usernameToColor/index.js renamed to src/functions/usernameToColor/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
export { default } from './usernameToColor.js'
6+
export { usernameToColor as default } from './usernameToColor.js'

src/functions/usernameToColor/usernameToColor.js

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { GenColors } from '../../utils/GenColors.js'
7+
8+
/**
9+
* Create a simple hash from a string
10+
* @param str - The string to hash
11+
*/
12+
function hashCode(str: string): number {
13+
let hash = 0
14+
15+
if (str.length === 0) {
16+
return hash
17+
}
18+
19+
for (let i = 0; i < str.length; i++) {
20+
const chr = str.charCodeAt(i)
21+
hash = ((hash << 5) - hash) + chr
22+
hash |= 0 // 32bit integer
23+
}
24+
25+
return hash
26+
}
27+
28+
/**
29+
* Generate a color from a username
30+
*
31+
* @param username - Display name or user id to generate from
32+
* @return The RGB color
33+
*/
34+
export function usernameToColor(username: string): { r: number, g: number, b: number } {
35+
const steps = 6
36+
const finalPalette = GenColors(steps)
37+
const hash = hashCode(username.toLocaleLowerCase())
38+
39+
return finalPalette[hash % (steps * 3)]
40+
}

vite.config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ import { globSync } from 'glob'
99
import { join, resolve } from 'node:path'
1010
import { defineConfig } from 'vite'
1111

12-
import md5 from 'md5'
13-
12+
import crypto from 'node:crypto'
1413
import l10nPlugin from './build/l10n-plugin.mjs'
1514

1615
const appVersion = JSON.stringify(process.env.npm_package_version || 'nextcloud-vue')
17-
const versionHash = md5(appVersion).slice(0, 7) as string
16+
const versionHash = crypto.createHash('md5').update(appVersion).digest('hex').slice(0, 7)
1817
const SCOPE_VERSION = JSON.stringify(versionHash)
1918

2019
// Entry points which we build using vite

0 commit comments

Comments
 (0)