You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			39 lines
		
	
	
		
			865 B
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			39 lines
		
	
	
		
			865 B
		
	
	
	
		
			TypeScript
		
	
| export type SizeClassType = 'default' | 'small' | 'medium' | 'large' | 'jumbo';
 | |
| 
 | |
| function getRegexUnicodeEmojis() {
 | |
|   return /\p{Emoji_Presentation}/gu;
 | |
| }
 | |
| 
 | |
| function getCountOfAllMatches(str: string) {
 | |
|   const regex = getRegexUnicodeEmojis();
 | |
| 
 | |
|   const matches = str.match(regex);
 | |
| 
 | |
|   return matches?.length || 0;
 | |
| }
 | |
| 
 | |
| function hasNormalCharacters(str: string) {
 | |
|   const noEmoji = str.replace(getRegexUnicodeEmojis(), '').trim();
 | |
|   return noEmoji.length > 0;
 | |
| }
 | |
| 
 | |
| export function getEmojiSizeClass(str: string): SizeClassType {
 | |
|   if (!str || !str.length) {
 | |
|     return 'small';
 | |
|   }
 | |
|   if (hasNormalCharacters(str)) {
 | |
|     return 'small';
 | |
|   }
 | |
| 
 | |
|   const emojiCount = getCountOfAllMatches(str);
 | |
|   if (emojiCount > 6) {
 | |
|     return 'small';
 | |
|   } else if (emojiCount > 4) {
 | |
|     return 'medium';
 | |
|   } else if (emojiCount > 2) {
 | |
|     return 'large';
 | |
|   } else {
 | |
|     return 'jumbo';
 | |
|   }
 | |
| }
 |