diff --git a/packages/p2p/src/utils/__tests__/string.spec.ts b/packages/p2p/src/utils/__tests__/string.spec.ts new file mode 100644 index 000000000000..679a5482be68 --- /dev/null +++ b/packages/p2p/src/utils/__tests__/string.spec.ts @@ -0,0 +1,18 @@ +import { countDecimalPlaces, getShortNickname } from '../string'; + +describe('countDecimalPlaces', () => { + it('should return the correct number of decimal places', () => { + expect(countDecimalPlaces(1.234)).toEqual(3); + expect(countDecimalPlaces(1.2)).toEqual(1); + expect(countDecimalPlaces(1)).toEqual(0); + }); +}); + +describe('getShortNickname', () => { + it('should return the correct short nickname', () => { + expect(getShortNickname('test')).toEqual('TE'); + expect(getShortNickname('test123')).toEqual('TE'); + expect(getShortNickname('test 123')).toEqual('TE'); + expect(getShortNickname('test 123 .,:;()@#+/-')).toEqual('TE'); + }); +}); diff --git a/packages/p2p/src/utils/string.js b/packages/p2p/src/utils/string.js deleted file mode 100644 index 9b506fc51047..000000000000 --- a/packages/p2p/src/utils/string.js +++ /dev/null @@ -1,51 +0,0 @@ -export const toSentenceCase = string => { - if (!string) { - return ''; - } - return string[0].toUpperCase() + string.slice(1); -}; - -export const countDecimalPlaces = value => { - return ((value.toString().split('.') || [])[1] || []).length; -}; - -export const generateHexColourFromNickname = nickname => { - if (!nickname) { - return '??'; - } - - const colours = [ - '#fc4400', - '#ff8c00', - '#092694', - '#527bb5', - '#3f6fe5', - '#6b4bb6', - '#db69e1', - '#ca0051', - '#3f6fe5', - '#f43f83', - '#6aba8d', - '#3fdce5', - '#1fb8bf', - '#9ed178', - '#71bd0e', - '#ff6444', - ]; - const colour_hash = nickname.split().reduce((hash, char, idx) => { - // https://gist.github.com/0x263b/2bdd90886c2036a1ad5bcf06d6e6fb37 - // string.charCodeAt(i) returns the UTF-16 code for the character at index i - // Bit operators work on 32 bits numbers. Any numeric operand in the operation - // is converted into a 32 bit number. - // hash << 5 is equivalent to hash * Math.pow(2, 5) (hash * 32), - // except the bit operator << makes sure our result is a 32 bit number. - // hash & hash again, makes sure we only return a 32 bit number. - const char_hash = nickname.charCodeAt(idx) + ((hash << 5) - hash); // eslint-disable-line no-bitwise - return char_hash & char_hash; // eslint-disable-line no-bitwise - }, 0); - - // Returns a colour derived from nickname that is in our colours array. - return colours[((colour_hash % colours.length) + colours.length) % colours.length]; -}; - -export const getShortNickname = nickname => nickname && nickname.substr(0, 2).toUpperCase(); diff --git a/packages/p2p/src/utils/string.ts b/packages/p2p/src/utils/string.ts new file mode 100644 index 000000000000..0c48e972e299 --- /dev/null +++ b/packages/p2p/src/utils/string.ts @@ -0,0 +1,54 @@ +/** + * A function that counts the number of decimal places in a number. + * @param {Number | String} value - The number to count the decimal places of. + * @returns {Number} The number of decimal places in the number. + */ +export const countDecimalPlaces = (value: number | string): number => (value.toString().split('.')?.[1] ?? '').length; + +/** + * A function that generates a colour from a nickname. + * + * @deprecated This will be removed once design has provided the correct colour for the user avatar. + * @param {String} nickname - The nickname to generate a colour from. + * @returns {String} A colour derived from nickname that is in our colours array. + */ +export const generateHexColourFromNickname = (nickname: string): string => { + if (!nickname) { + return '??'; + } + + const colours = [ + '#fc4400', + '#ff8c00', + '#092694', + '#527bb5', + '#3f6fe5', + '#6b4bb6', + '#db69e1', + '#ca0051', + '#3f6fe5', + '#f43f83', + '#6aba8d', + '#3fdce5', + '#1fb8bf', + '#9ed178', + '#71bd0e', + '#ff6444', + ]; + + const colour_hash = nickname.split().reduce((hash, char, idx) => { + // Below we get a random colour from the string which is explained here: https://gist.github.com/0x263b/2bdd90886c2036a1ad5bcf06d6e6fb37 + const char_hash = nickname.charCodeAt(idx) + ((hash << 5) - hash); // eslint-disable-line no-bitwise + return char_hash & char_hash; // eslint-disable-line no-bitwise + }, 0); + + return colours[((colour_hash % colours.length) + colours.length) % colours.length]; +}; + +/** + * A function that gets the short nickname from a nickname. + * + * @param {String} nickname - The nickname to get the short nickname from. + * @returns {String} The short nickname. + */ +export const getShortNickname = (nickname: string): string => nickname?.substring(0, 2).toUpperCase();