signal-desktop/ts/reactions/isValidReactionEmoji.ts
2022-04-07 14:50:42 -04:00

22 lines
666 B
TypeScript

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import emojiRegex from 'emoji-regex';
import { getGraphemes } from '../util/grapheme';
import { take, size } from '../util/iterables';
export function isValidReactionEmoji(value: unknown): value is string {
if (typeof value !== 'string') {
return false;
}
// This is effectively `countGraphemes(value) === 1`, but doesn't require iterating
// through an extremely long string.
const graphemes = getGraphemes(value);
const truncatedGraphemes = take(graphemes, 2);
if (size(truncatedGraphemes) !== 1) {
return false;
}
return emojiRegex().test(value);
}