const isEmoji = /[\ud800-\udbff][\udc00-\udfff]/g; const isEmojiCode = /\[:\d+]/g; export const emojiToString = (str: string) => { const result = str.replace(isEmoji, (char) => { let H; let L; let code; let s; if (char.length === 2) { H = char.charCodeAt(0); L = char.charCodeAt(1); code = (H - 0xd800) * 0x400 + 0x10000 + L - 0xdc00; s = `[:${code}]`; } else { s = char; } return s; }); return result; }; export const stringToEmoji = (strObj: string) => { if (!isEmojiCode.test(strObj)) { return strObj; } const arr = strObj.match(isEmojiCode) || []; let result = strObj; let H; let L; let code; arr.forEach((item) => { code = item; code = Number(code.replace('[:', '').replace(']', '')); H = Math.floor((code - 0x10000) / 0x400) + 0xd800; L = ((code - 0x10000) % 0x400) + 0xdc00; code = `[:${code}]`; const s = String.fromCharCode(H, L); result = result.replace(code, s); }); return result; };