Skip to content

Commit

Permalink
Refactor emoji stripping and punctuation stripping functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmybtlr committed May 14, 2024
1 parent d73bbe1 commit bd2aec7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/modifiers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ test('unescapeHtml', () => {

test('stripEmojis', () => {
expect(mod.stripEmojis('Hello 😃')).toBe('Hello ')
expect(mod.stripEmojis('Hello 😃👍')).toBe('Hello ')
expect(mod.stripEmojis('Hello 😃👍🏻')).toBe('Hello ')
expect(mod.stripEmojis('Hello 🎉')).toBe('Hello ')
})

test('stripWhitespace', () => {
Expand All @@ -32,6 +35,7 @@ test('stripSymbols', () => {

test('stripPunctuation', () => {
expect(mod.stripPunctuation('Hello world!')).toBe('Hello world')
expect(mod.stripPunctuation('Hello, world!')).toBe('Hello world')
})

test('slugify', () => {
Expand Down
16 changes: 13 additions & 3 deletions src/modifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export function stripNumbers(text: string): string {
* Strips punctuation from a string.
*/
export function stripPunctuation(text: string): string {
return text.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, '')
return text.replace(/[.,/#!$%^&*;:{}=\-_`~()]/g, '')
}

/**
Expand All @@ -191,7 +191,17 @@ export function stripSymbols(text: string): string {
* Strips emojis from a string (requires ES6 Unicode support) 🦊.
*/
export function stripEmojis(text: string): string {
return text.replace(/[\p{Emoji_Presentation}\p{Emoji}\uFE0F\u200D\u20E3]/gu, '')
return text.replace(/[\u{1F600}-\u{1F64F}]/gu, '') // Emoticons
.replace(/[\u{1F300}-\u{1F5FF}]/gu, '') // Miscellaneous Symbols and Pictographs
.replace(/[\u{1F680}-\u{1F6FF}]/gu, '') // Transport and Map Symbols
.replace(/[\u{1F700}-\u{1F77F}]/gu, '') // Alchemical Symbols
.replace(/[\u{1F780}-\u{1F7FF}]/gu, '') // Geometric Shapes Extended
.replace(/[\u{1F800}-\u{1F8FF}]/gu, '') // Supplemental Arrows-C
.replace(/[\u{1F900}-\u{1F9FF}]/gu, '') // Supplemental Symbols and Pictographs
.replace(/[\u{1FA00}-\u{1FA6F}]/gu, '') // Chess Symbols
.replace(/[\u{1FA70}-\u{1FAFF}]/gu, '') // Symbols and Pictographs Extended-A
.replace(/[\u{2600}-\u{26FF}]/gu, '') // Miscellaneous Symbols
.replace(/[\u{2700}-\u{27BF}]/gu, ''); // Dingbats
}

/**
Expand Down Expand Up @@ -259,7 +269,7 @@ export function kebabCase(text: string): string {
*/
export function titleCase(text: string): string {
return text
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => {
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => {
return word.toUpperCase()
})
.replace(/\s+/g, ' ')
Expand Down

0 comments on commit bd2aec7

Please sign in to comment.