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

fix feature : update isOnlyEmoji #128

Merged
merged 3 commits into from
May 24, 2023
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
48 changes: 32 additions & 16 deletions src/utils/isOnlyEmoji.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
import { describe, expect, it } from 'vitest'

import isOnlyEmoji from './isOnlyEmoji.js'
import isOnlyEmoji from './isOnlyEmoji'

describe('isOnlyEmoji', () => {
it.each([
{ msg: '🫠' },
{ msg: '🅰️' },
{ msg: '🅾' },
{ msg: ' 🅾 🅾 🅾 🅾 🅾' },
{ msg: '<:test:000>' },
{ msg: '1️⃣' },
{ msg: '<a:test:111>' },
{ msg: '<:ShareX_0RB3:1108771953776537701>' },
{ msg: '<:Test:1108771953776537701>' },
{ msg: '🅾🫠' },
{ msg: '🏻 🏼' },
{ msg: '👩🏾‍❤‍💋‍👩🏼' },
{ msg: '👩🏾‍❤‍💋‍👩🏼\n🅾\n<:test:000>' },
//case of variation selector (0xFE0F)
{ msg: '0️⃣' },
{ msg: ` 1️⃣ ` },
{ msg: '#️⃣ *️⃣ 0️⃣ 1️⃣ 2️⃣ 3️⃣ 4️⃣ 5️⃣ 6️⃣ 7️⃣ 8️⃣ 9️⃣ 🔟' },
{ msg: '1⃣' },
])('should match emoji ($msg)', async ({ msg }) => {
expect(isOnlyEmoji(msg)).toBeTruthy()
})

it.each([{ msg: '' }, { msg: 'hello' }, { msg: 'a' }, { msg: '<html>' }])(
'should not match emoji ($msg)',
async ({ msg }) => {
expect(isOnlyEmoji(msg)).toBeFalsy()
},
)

it('should not match emoji with text', async () => {
expect(isOnlyEmoji('hello 🫠')).toBeFalsy()
})

it('should not match messages with only numbers', async () => {
expect(isOnlyEmoji('1 2 3')).toBeFalsy()
it.each([
{ msg: '' },
{ msg: 'hello' },
{ msg: 'a' },
{ msg: '<html>' },
{ msg: '1 2 3' },
{ msg: '1️⃣0' },
{ msg: '-1' },
{ msg: '0x 000' },
{ msg: '<:ShareX_0000 :>' },
{ msg: 'Test : ' },
{ msg: ':Imao' },
{ msg: 'hello 🫠🫠🫠' },
{ msg: `hi <:ShareX_00000:>` },
{ msg: `#20` },
{ msg: `0000\n00 2131⃣33` },
])('should not match emoji ($msg)', async ({ msg }) => {
expect(isOnlyEmoji(msg)).toBeFalsy()
})
})
35 changes: 22 additions & 13 deletions src/utils/isOnlyEmoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ const emojiRegex =
export default (message: string): boolean => {
const emoji = message.match(emojiRegex)
if (emoji !== null) {
const unicoded = emoji.map((emo) => {
return emo.codePointAt(0)
})
for (const [index, value] of unicoded.entries()) {
// the condition after number is to detect the unicode 0xFE0F next to number which mean to convert normal number to it emoji alternative.
if (
value !== undefined &&
isNumber(value) &&
index + 1 <= unicoded.length &&
unicoded[index + 1] !== 0xfe_0f
) {
// Convert each emoji to its Unicode code and filter out undefined
const unicoded = emoji
.map((emo) => emo.codePointAt(0))
.filter((codePoint): codePoint is number => codePoint !== undefined)
// Check if have any number -> not an emoji
for (let index = 0; index < unicoded.length; index++) {
if (isEmojiNumber(unicoded[index], unicoded[index + 1])) {
// Skip the next unicode as we already checked it in isEmojiNumber function.
index++
} else if (unicoded[index] >= 0x30 && unicoded[index] <= 0x39) {
// If the current unicode is a number but not an emoji number, return false.
return false
}
}
Expand All @@ -23,6 +23,15 @@ export default (message: string): boolean => {
)
}

function isNumber(input: number): boolean {
return input >= 0x30 && input <= 0x39 //0x30 to 0x39 is range of number unicode from 0 to 9.
function isEmojiNumber(input: number, nextInput: number): boolean {
// Unicode 0x30 to 0x39 is range of number from 0 to 9.
// The following unicode 0xFE0F denotes to Variation Selector-16
// which is used to convert normal number to it emoji alternative.
// The following unicode 0x20E3 denotes to Combining Enclosing Keycap
// which is used to convert number to keycap emoji.
return (
input >= 0x30 &&
input <= 0x39 &&
(nextInput === 0xfe_0f || nextInput === 0x20_e3)
)
}