All files / utils avatarInitialsAndColor.js

100% Statements 7/7
33.33% Branches 1/3
100% Functions 1/1
100% Lines 7/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16      1x   1x 1x   1x   1x 1x   1x    
import { AVATAR_COLORS } from '../constants/colors';
 
export default function(username = '') {
	const position = username.length % AVATAR_COLORS.length;
 
	const color = AVATAR_COLORS[position];
	username = username.replace(/[^A-Za-z0-9]/g, '.').replace(/\.+/g, '.').replace(/(^\.)|(\.$)/g, '');
 
	const usernameParts = username.split('.');
 
	let initials = usernameParts.length > 1 ? usernameParts[0][0] + usernameParts[usernameParts.length - 1][0] : username.replace(/[^A-Za-z0-9]/g, '').substr(0, 2);
	initials = initials.toUpperCase();
 
	return { initials, color };
}