Improve performance of isEmojiOnlyText
This commit is contained in:
parent
8fa4cd68d5
commit
6578679166
2 changed files with 45 additions and 4 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue