Skip to content

Commit

Permalink
Merge pull request #11400 from Expensify/aldo_hashcode-avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
roryabraham authored Oct 5, 2022
2 parents f710d78 + b8e34ae commit 23ab716
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 197 deletions.
6 changes: 3 additions & 3 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import CONST from '../CONST';
import * as Localize from './Localize';
import * as LocalePhoneNumber from './LocalePhoneNumber';
import * as Expensicons from '../components/Icon/Expensicons';
import md5 from './md5';
import hashCode from './hashCode';
import Navigation from './Navigation/Navigation';
import ROUTES from '../ROUTES';
import * as NumberUtils from './NumberUtils';
Expand Down Expand Up @@ -401,8 +401,8 @@ function formatReportLastMessageText(lastMessageText) {
*/
function getDefaultAvatar(login = '') {
// There are 8 possible default avatars, so we choose which one this user has based
// on a simple hash of their login (which is converted from HEX to INT)
const loginHashBucket = (parseInt(md5(login.toLowerCase()).substring(0, 4), 16) % 8) + 1;
// on a simple hash of their login
const loginHashBucket = (Math.abs(hashCode(login.toLowerCase())) % 8) + 1;
return `${CONST.CLOUDFRONT_URL}/images/avatars/avatar_${loginHashBucket}.png`;
}

Expand Down
19 changes: 19 additions & 0 deletions src/libs/hashCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable no-bitwise */
/**
* Simple string hashing function obtained from: https://stackoverflow.com/a/8831937/16434681
* Returns a hash code from a string
* @param {String} str The string to hash.
* @return {Number} A 32bit integer (can be negative)
* @see http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/
*/
function hashCode(str) {
let hash = 0;
for (let i = 0, len = str.length; i < len; i++) {
const chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}

export default hashCode;
194 changes: 0 additions & 194 deletions src/libs/md5.js

This file was deleted.

0 comments on commit 23ab716

Please sign in to comment.