mail/back/utils/string.ts
2023-04-01 22:36:51 +02:00

19 lines
758 B
TypeScript

export function transformEmojis(str :string): string {
if (!str) return str;
// Use a regular expression to match emojis in the string
const regex =
/[\u{1F600}-\u{1F64F}\u{1F300}-\u{1F5FF}\u{1F680}-\u{1F6FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}\u{1F900}-\u{1F9FF}\u{1F1E0}-\u{1F1FF}]/gu;
// Replace each matched emoji with its Unicode code point
const transformedStr = str.replace(regex, (match) => {
return "\\u{" + match.codePointAt(0)?.toString(16).toUpperCase() + "}";
});
return transformedStr;
}
export function decodeEmojis(text: string): string {
const regex = /\\u{([^}]+)}/g;
const decodedText = text.replace(regex, (_, hex) => String.fromCodePoint(parseInt(hex, 16)));
return decodedText;
}