65 lines
1.5 KiB
TypeScript
65 lines
1.5 KiB
TypeScript
export function encodePassphrase(passphrase: string) {
|
|
return encodeURIComponent(passphrase);
|
|
}
|
|
|
|
export function decodePassphrase(base64String: string) {
|
|
return decodeURIComponent(base64String);
|
|
}
|
|
|
|
export function generateRoomId(): string {
|
|
return `${randomString(4)}-${randomString(4)}`;
|
|
}
|
|
|
|
export function randomString(length: number): string {
|
|
let result = '';
|
|
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
const charactersLength = characters.length;
|
|
for (let i = 0; i < length; i++) {
|
|
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function mergeClasses(...classes: (string | undefined | null | false)[]): string {
|
|
return classes.filter(Boolean).join(' ');
|
|
}
|
|
|
|
export function getAvatarColor(identity: string): string {
|
|
const colors = [
|
|
'#4CAF50',
|
|
'#8BC34A',
|
|
'#CDDC39',
|
|
'#FFC107',
|
|
'#FF9800',
|
|
'#FF5722',
|
|
'#F44336',
|
|
'#E91E63',
|
|
'#9C27B0',
|
|
'#673AB7',
|
|
'#3F51B5',
|
|
'#2196F3',
|
|
'#03A9F4',
|
|
'#00BCD4',
|
|
'#009688',
|
|
];
|
|
|
|
let hash = 0;
|
|
for (let i = 0; i < identity.length; i++) {
|
|
hash = identity.charCodeAt(i) + ((hash << 5) - hash);
|
|
}
|
|
|
|
const index = Math.abs(hash) % colors.length;
|
|
return colors[index];
|
|
}
|
|
|
|
export function getInitials(name: string): string {
|
|
if (!name) return '?';
|
|
|
|
const parts = name.split(' ');
|
|
if (parts.length === 1) {
|
|
return parts[0].charAt(0).toUpperCase();
|
|
}
|
|
|
|
return (parts[0].charAt(0) + parts[parts.length - 1].charAt(0)).toUpperCase();
|
|
}
|