Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Graph] improve emoji removal function #2282

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -7,6 +7,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 @@ -45,13 +46,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 : ''))
}
Loading