UNPKG

@chayns-components/emoji-input

Version:
108 lines (106 loc) 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.unescapeHTML = exports.escapeHTML = exports.convertEmojisToUnicode = exports.addSkinToneToEmoji = void 0; var _emoji = require("../constants/emoji"); const convert = unicode => { if (unicode.indexOf('-') > -1) { const parts = []; const s = unicode.split('-'); for (let i = 0; i < s.length; i++) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion let part = parseInt(s[i], 16); if (part >= 0x10000 && part <= 0x10ffff) { const hi = Math.floor((part - 0x10000) / 0x400) + 0xd800; const lo = (part - 0x10000) % 0x400 + 0xdc00; part = String.fromCharCode(hi) + String.fromCharCode(lo); } else { part = String.fromCharCode(part); } parts.push(part); } return parts.join(''); } const s = parseInt(unicode, 16); if (s >= 0x10000 && s <= 0x10ffff) { const hi = Math.floor((s - 0x10000) / 0x400) + 0xd800; const lo = (s - 0x10000) % 0x400 + 0xdc00; return String.fromCharCode(hi) + String.fromCharCode(lo); } return String.fromCharCode(s); }; const unescapeHTML = text => { const unescaped = { '&amp;': '&', '&#38;': '&', '&#x26;': '&', '&lt;': '<', '&#60;': '<', '&#x3C;': '<', '&gt;': '>', '&#62;': '>', '&#x3E;': '>', '&quot;': '"', '&#34;': '"', '&#x22;': '"', '&apos;': "'", '&#39;': "'", '&#x27;': "'" }; return text.replace(/&(?:amp|#38|#x26|lt|#60|#x3C|gt|#62|#x3E|apos|#39|#x27|quot|#34|#x22);/gi, match => unescaped[match] ?? match); }; exports.unescapeHTML = unescapeHTML; const escapeHTML = text => { const escaped = { '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&apos;' }; return text.replace(/[&<>"']/g, match => escaped[match] ?? match); }; exports.escapeHTML = escapeHTML; const convertEmojisToUnicode = (text, regShortnames, shortNameList, options) => { let result = text; result = result.replace(/https?:\/\/.*?(?=$|\s)/gi, fullMatch => fullMatch.replace(/:/g, '%3A')); result = result.replace(regShortnames, shortname => { if (shortname) { const unicode = shortNameList[shortname]; if (unicode) { return convert(unicode.toUpperCase()); } } return shortname; }); result = result.replace(/https?%3A\/\/.*?(?=$|\s)/gi, fullMatch => fullMatch.replace(/%3A/g, ':')); result = result.replace(_emoji.regAscii, (fullMatch, m1, m2, m3) => { if (typeof m3 === 'string' && m3 !== '') { const unicode = _emoji.asciiList[unescapeHTML(m3)]; if (unicode) { const emoji = convert(unicode.toUpperCase()); if (typeof (options === null || options === void 0 ? void 0 : options.onAsciiConversion) === 'function') { options.onAsciiConversion({ original: m3, emoji }); } return m2 + emoji; } } return fullMatch; }); return result; }; exports.convertEmojisToUnicode = convertEmojisToUnicode; const addSkinToneToEmoji = (emojiList, emoji, skinTone) => emoji.split('\u{200D}').map(rawEmoji => { var _emojiList$rawEmoji; const parts = [rawEmoji.replace(/\ufe0f/, '')]; // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access if ((_emojiList$rawEmoji = emojiList[rawEmoji]) !== null && _emojiList$rawEmoji !== void 0 && _emojiList$rawEmoji.skin_tone_support) { parts.push(skinTone); } return parts.join(''); }).join('\u{200D}'); exports.addSkinToneToEmoji = addSkinToneToEmoji; //# sourceMappingURL=emoji.js.map