forked from binary-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ameerul /P2PS-1563 Migrate string.js to typescript (binary-com#10053)
* 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
1 parent
9e97302
commit 3649620
Showing
3 changed files
with
72 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |