Skip to content

Commit

Permalink
Merge pull request #2282 from Ekep-Obasi/fix/remove-all-emoji-graph
Browse files Browse the repository at this point in the history
[Graph] improve emoji removal function
  • Loading branch information
Rassl authored Oct 7, 2024
2 parents 6a23865 + dbc304b commit 7e5e1b5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/components/Universe/Graph/Cubes/Text/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useGraphStore, useHoveredNode, useSelectedNode, useSelectedNodeRelative
import { useSchemaStore } from '~/stores/useSchemaStore'
import { NodeExtended } from '~/types'
import { colors } from '~/utils/colors'
import { removeEmojis } from '~/utils/removeEmojisFromText'
import { truncateText } from '~/utils/truncateText'
import { fontProps } from './constants'

Expand Down Expand Up @@ -47,13 +48,6 @@ type Props = {
hide?: boolean
}

function removeEmojis(text: string): string {
return text.replace(
/[\u{1F600}-\u{1F64F}\u{1F300}-\u{1F5FF}\u{1F680}-\u{1F6FF}\u{1F700}-\u{1F77F}\u{1F780}-\u{1F7FF}\u{1F800}-\u{1F8FF}\u{1F900}-\u{1F9FF}\u{1FA00}-\u{1FA6F}\u{1FA70}-\u{1FAFF}\u{1FB00}-\u{1FBFF}\u{1FC00}-\u{1FCFF}\u{1FD00}-\u{1FDFF}\u{1FE00}-\u{1FEFF}\u{1FF00}-\u{1FFFF}\u{20000}-\u{2A6DF}\u{2A700}-\u{2B73F}\u{2B740}-\u{2B81F}\u{2B820}-\u{2CEAF}\u{2F800}-\u{2FA1F}]/gu,
'',
)
}

function splitStringIntoThreeParts(text: string): string {
// Split the string into an array of words

Expand Down
32 changes: 32 additions & 0 deletions src/utils/removeEmojisFromText/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { removeEmojis } from '../index'

describe('removeEmojis', () => {
test('handles empty string', () => {
expect(removeEmojis('')).toBe('')
})

test('returns the same string if no emojis are present', () => {
expect(removeEmojis('Hello, World!')).toBe('Hello, World!')
})

test('handles string with only emojis', () => {
expect(removeEmojis('πŸ˜€πŸ˜ƒπŸ˜„πŸ˜πŸ˜†')).toBe('')
})

test('removes spaced out emojis', () => {
expect(removeEmojis('Hello 😊 World 🌍')).toBe('Hello World ')
})

test('removes multiple emojis in a row', () => {
expect(removeEmojis('Wow! πŸŽ‰πŸŽŠπŸŽˆ')).toBe('Wow! ')
})

test.each([
['πŸš€Start', 'Start'],
['EndπŸš€', 'End'],
['πŸš€MiddleπŸš€', 'Middle'],
['πŸš€', ''],
])('removes emojis at different positions: %s', (input, expected) => {
expect(removeEmojis(input)).toBe(expected)
})
})
6 changes: 6 additions & 0 deletions src/utils/removeEmojisFromText/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const removeEmojis = (text: string): string => {
const emojiRegex =
/(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])/g

return text.replace(emojiRegex, (match) => (/^[\d*#]$/.test(match) ? match : ''))
}

0 comments on commit 7e5e1b5

Please sign in to comment.