Skip to content

Commit

Permalink
feat: emoji detection and tests TryQuiet#519
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-kiss committed Mar 25, 2024
1 parent 799256f commit 752fbe1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/common/src/emojis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { hasEmoji, isAllEmoji } from './emojis'

describe('Emoji Utilities', () => {
it('Should detect an emoji in a string', () => {
expect(hasEmoji('❤️‍🔥')).toBeTruthy()
expect(hasEmoji('Hello ❤️‍🔥 Emoji')).toBeTruthy()
expect(hasEmoji('No emoji :-(')).toBeFalsy()
})

it('Should detect when a string is all emojis (or spaces)', () => {
expect(isAllEmoji('🙂🙂🙂🙂🙂🙂🙂🙂')).toBeTruthy()
expect(isAllEmoji('🐈‍⬛❤️‍🔥🏴')).toBeTruthy()
expect(isAllEmoji('🐈‍⬛ ❤️‍🔥 🏴')).toBeTruthy()
expect(isAllEmoji('❤️‍🔥')).toBeTruthy()
expect(isAllEmoji('🐈‍⬛')).toBeTruthy()
expect(isAllEmoji('❤️‍🔥 Emoji')).toBeFalsy()
expect(isAllEmoji('Hello ❤️‍🔥')).toBeFalsy()
expect(isAllEmoji('Hello ❤️‍🔥 Emoji')).toBeFalsy()
expect(isAllEmoji('🐈‍⬛ (Not emoji) 🏴')).toBeFalsy()
expect(isAllEmoji('No emoji :-(')).toBeFalsy()
})
})
12 changes: 12 additions & 0 deletions packages/common/src/emojis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const hasEmoji = (testString: string) => {
// All strings with at least one emoji character should match this
const regExp = /\p{Emoji}/gu
return regExp.test(testString)
}

export const isAllEmoji = (testString: string) => {
// Detect whether a string is entirely emojis (and whitespace and zero-width-joins, region indicators, etc)
// This may need to be updated as Unicode's Emoji spec is a moving target
const emojiOrWhitespaceRegExp = /^(\p{Emoji}|\p{Emoji_Modifier}|\uFE0F|\u200D|\p{RI}|\uE007F|\s)+$/gu
return emojiOrWhitespaceRegExp.test(testString)
}

0 comments on commit 752fbe1

Please sign in to comment.