diff --git a/ts/test-both/util/isEmojiOnlyText_test.ts b/ts/test-both/util/isEmojiOnlyText_test.ts new file mode 100644 index 000000000..ca7e12994 --- /dev/null +++ b/ts/test-both/util/isEmojiOnlyText_test.ts @@ -0,0 +1,28 @@ +// Copyright 2022 Signal Messenger, LLC +// SPDX-License-Identifier: AGPL-3.0-only + +import { assert } from 'chai'; + +import { isEmojiOnlyText } from '../../util/isEmojiOnlyText'; + +describe('isEmojiOnlyText', () => { + it('returns false on empty string', () => { + assert.isFalse(isEmojiOnlyText('')); + }); + + it('returns false on non-emoji string', () => { + assert.isFalse(isEmojiOnlyText('123')); + }); + + it('returns false on mixed emoji/text string', () => { + assert.isFalse(isEmojiOnlyText('12😎3')); + }); + + it('returns false on mixed emoji/text string starting with emoji', () => { + assert.isFalse(isEmojiOnlyText('😎12😎3')); + }); + + it('returns true on all emoji string', () => { + assert.isTrue(isEmojiOnlyText('😎👍😀😮‍💨')); + }); +}); diff --git a/ts/util/isEmojiOnlyText.tsx b/ts/util/isEmojiOnlyText.tsx index 958227e5a..94907e9a1 100644 --- a/ts/util/isEmojiOnlyText.tsx +++ b/ts/util/isEmojiOnlyText.tsx @@ -1,9 +1,22 @@ -// Copyright 2021 Signal Messenger, LLC +// Copyright 2021-2022 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only -import * as grapheme from './grapheme'; -import { getEmojiCount } from '../components/emoji/lib'; +import emojiRegex from 'emoji-regex'; export function isEmojiOnlyText(text: string): boolean { - return grapheme.count(text) === getEmojiCount(text); + if (text.length === 0) { + return false; + } + + const regex = emojiRegex(); + let len = 0; + for (const match of text.matchAll(regex)) { + // Skipped some non-emoji text, return early + if (match.index !== len) { + return false; + } + + len += match[0].length; + } + return len === text.length; }