feat(Avatar): simplified code

This commit is contained in:
Saúl Ibarra Corretgé 2018-07-31 11:45:04 +02:00 committed by Paweł Domas
parent 2b9ce40533
commit f5a667ad9e
1 changed files with 6 additions and 17 deletions

View File

@ -186,31 +186,20 @@ export default class Avatar extends Component<Props, State> {
*/ */
_getBackgroundColor({ uri }) { _getBackgroundColor({ uri }) {
if (!uri) { if (!uri) {
// @lyubomir: I'm leaving @saghul's implementation which picks up a
// random color bellow so that we have it in the source code in
// case we decide to use it in the future. However, I think at the
// time of this writing that the randomness reduces the
// predictability which React is supposed to bring to our app.
return ColorPalette.white; return ColorPalette.white;
} }
let hash = 0; let hash = 0;
if (typeof uri === 'string') { /* eslint-disable no-bitwise */
/* eslint-disable no-bitwise */
for (let i = 0; i < uri.length; i++) { for (let i = 0; i < uri.length; i++) {
hash = uri.charCodeAt(i) + ((hash << 5) - hash); hash = uri.charCodeAt(i) + ((hash << 5) - hash);
hash |= 0; // Convert to 32-bit integer hash |= 0; // Convert to 32-bit integer
}
/* eslint-enable no-bitwise */
} else {
// @saghul: If we have no URI yet, we have no data to hash from. So
// use a random value.
hash = Math.floor(Math.random() * 360);
} }
/* eslint-enable no-bitwise */
return `hsl(${hash % 360}, 100%, 75%)`; return `hsl(${hash % 360}, 100%, 75%)`;
} }