Skip to content

Commit

Permalink
refactor: use stringToHslaColor from '@mll-lab/js-utils'
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Bigelmayr authored and spawnia committed Aug 20, 2024
1 parent 9443699 commit 242241c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 80 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"dependencies": {
"@ant-design/icons": "^5.0.1",
"@dnd-kit/core": "^6.0.8",
"@mll-lab/js-utils": "^2.17.1",
"@mll-lab/js-utils": "^2.36.1",
"antd": "4.20.5",
"date-fns": "^2.29.3",
"lodash": "^4.17.21",
Expand Down
3 changes: 1 addition & 2 deletions src/Avatar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { UserOutlined } from '@ant-design/icons';
import { stringToHslaColor } from '@mll-lab/js-utils';
import { Avatar as AntdAvatar, AvatarProps as AntdAvatarProps } from 'antd';
import React from 'react';

import { stringToHslaColor } from './utils';

export const Avatar: typeof AntdAvatar = AntdAvatar;
export type AvatarProps = AntdAvatarProps;

Expand Down
34 changes: 0 additions & 34 deletions src/Avatar/utils.test.tsx

This file was deleted.

30 changes: 0 additions & 30 deletions src/Avatar/utils.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
function hashCode(string: string): number {
let hash = 0;
// eslint-disable-next-line no-plusplus
for (let i = 0; i < string.length; i++) {
// eslint-disable-next-line no-bitwise
hash = string.charCodeAt(i) + ((hash << 5) - hash);
}

return hash;
}

function getHslColor(hash: number): string {
const h = range(hash, 0, 360);
const s = range(hash, 50, 100);
const l = range(hash, 20, 50);

return `hsla(${h}, ${s}%, ${l}%, 1)`;
}

function range(hash: number, min: number, max: number): number {
const diff = max - min;
const x = ((hash % diff) + diff) % diff;

return x + min;
}

export function stringToHslaColor(string: string): string {
return getHslColor(hashCode(string));
}

/**
* Generates a random string with a length of 1-11 chars.
*/
Expand Down
26 changes: 13 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2399,15 +2399,15 @@ __metadata:
languageName: node
linkType: hard

"@mll-lab/js-utils@npm:^2.17.1":
version: 2.20.0
resolution: "@mll-lab/js-utils@npm:2.20.0"
"@mll-lab/js-utils@npm:^2.36.1":
version: 2.36.1
resolution: "@mll-lab/js-utils@npm:2.36.1"
dependencies:
date-fns: "npm:2.0.0-alpha.26"
date-fns: "npm:3.0.1"
lodash: "npm:4.17.15"
peerDependencies:
"@babel/runtime": ">=7.13.6"
checksum: 10c0/df62e1942246f38dc4d958736b353256477f0dff27fea7a7801f512dccdf9971208495784fce8eb335f9f43be2472b0d023fa61c3d0929bc3adb79e6d28a8afd
checksum: 10c0/c71446f60cb1f30e9ffa0e2e2b357465468df8d17d6b7febdb2d2dd75847434fc2fb5bdf7e864bb61edc5c94382fa38e6f3ab95d3cf8193aecfe79c60b060462
languageName: node
linkType: hard

Expand All @@ -2432,7 +2432,7 @@ __metadata:
"@dnd-kit/core": "npm:^6.0.8"
"@mll-lab/eslint-config": "npm:^3.1.0"
"@mll-lab/eslint-plugin": "npm:^1.3.4"
"@mll-lab/js-utils": "npm:^2.17.1"
"@mll-lab/js-utils": "npm:^2.36.1"
"@mll-lab/prettier-config": "npm:^1.0.0"
"@mll-lab/tsconfig": "npm:^3.0.0"
"@rollup/plugin-babel": "npm:^6.0.4"
Expand Down Expand Up @@ -8436,13 +8436,6 @@ __metadata:
languageName: node
linkType: hard

"date-fns@npm:2.0.0-alpha.26":
version: 2.0.0-alpha.26
resolution: "date-fns@npm:2.0.0-alpha.26"
checksum: 10c0/95da7a6ef83bb67580466aa4eb92b6f826343a201dfd3e1f194cded0b96d95fe6e4fa82203b8822913b32f433513aad20eba174220aee2f579b541c2243cc39f
languageName: node
linkType: hard

"date-fns@npm:2.x, date-fns@npm:^2.0.1, date-fns@npm:^2.29.3":
version: 2.30.0
resolution: "date-fns@npm:2.30.0"
Expand All @@ -8452,6 +8445,13 @@ __metadata:
languageName: node
linkType: hard

"date-fns@npm:3.0.1":
version: 3.0.1
resolution: "date-fns@npm:3.0.1"
checksum: 10c0/d840a0b25ba4c262868df804d1f4939e809326b6fbdacc04f7f5184c2af9891de84ca5620777a98b077fad52777928032bb2cd8cd3a99d22eb1c75220f0aae34
languageName: node
linkType: hard

"dayjs@npm:1.x":
version: 1.11.10
resolution: "dayjs@npm:1.11.10"
Expand Down

0 comments on commit 242241c

Please sign in to comment.