Skip to content

Commit

Permalink
Ameerul /P2PS-1563 Migrate string.js to typescript (binary-com#10053)
Browse files Browse the repository at this point in the history
* chore: migrated string.js to typescript, added test case

* chore: annoted types and replaced deprecated substr with substring

* fix: removed extra nickname check
  • Loading branch information
ameerul-deriv committed Nov 27, 2023
1 parent 9e97302 commit 3649620
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 51 deletions.
18 changes: 18 additions & 0 deletions packages/p2p/src/utils/__tests__/string.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
51 changes: 0 additions & 51 deletions packages/p2p/src/utils/string.js

This file was deleted.

54 changes: 54 additions & 0 deletions packages/p2p/src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 3649620

Please sign in to comment.