-
Notifications
You must be signed in to change notification settings - Fork 734
Avatar - add name with initials and backgroundColor specific to that name #1239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9f52070
5bdd7e8
2aacc71
ab289fc
4bc280d
b31b894
373abcf
ff110a9
166267b
75a20b6
7baace6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
export declare function hashStringToNumber(str: string): number; | ||
export declare function getAvatarColors(): string[]; | ||
export declare function getColorById(id: string, avatarColors?: string[]): string; | ||
export declare function getInitials(name: string): string; | ||
export declare function getInitials(name?: string, limit?: number): string; | ||
export declare function getBackgroundColor(name?: string, avatarColors?: string[], hashFunction?: (name?: string) => number, defaultColor?: string): string | undefined; | ||
export declare function isGravatarUrl(url: string): any; | ||
export declare function isBlankGravatarUrl(url: string): boolean; | ||
export declare function patchGravatarUrl(gravatarUrl: string): any; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ import _ from 'lodash'; | |
import URL from 'url-parse'; | ||
import Colors from '../style/colors'; | ||
|
||
function hashStringToNumber(str: string) { | ||
export function hashStringToNumber(str: string) { | ||
let hash = 5381; | ||
for (let i = 0; i < str.length; i++) { | ||
const char = str.charCodeAt(i); | ||
|
@@ -27,13 +27,13 @@ export function getColorById(id: string, avatarColors = getAvatarColors()) { | |
return avatarColors[colorIndex]; | ||
} | ||
|
||
export function getInitials(name: string) { | ||
export function getInitials(name?: string, limit = 2) { | ||
ethanshar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let initials = ''; | ||
if (name && _.isString(name)) { | ||
const nameSplitted = _.chain(name) | ||
.split(/\s+/g) | ||
.filter(word => word.length > 0) | ||
.take(2) | ||
.take(limit) | ||
.value(); | ||
_.each(nameSplitted, (str) => { | ||
initials += str[0]; | ||
|
@@ -43,6 +43,19 @@ export function getInitials(name: string) { | |
return _.toUpper(initials); | ||
} | ||
|
||
export function getBackgroundColor(name?: string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider adding few simple tests to cover the new functionalities There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done (found a bug!) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The power of testings (: |
||
avatarColors?: string[], | ||
hashFunction?: (name?: string) => number, | ||
defaultColor?: string) { | ||
if (!name || !avatarColors || !hashFunction) { | ||
return defaultColor; | ||
} | ||
|
||
const hash = hashFunction(name); | ||
const index = Math.abs(hash % avatarColors.length); | ||
return avatarColors[index]; | ||
} | ||
|
||
export function isGravatarUrl(url: string) { | ||
const {hostname, pathname} = new URL(url); | ||
return _.split(hostname, '.').includes('gravatar') && pathname.startsWith('/avatar/'); | ||
|
Uh oh!
There was an error while loading. Please reload this page.