EmojiDecoder.js 845 B

12345678910111213141516171819202122232425262728293031
  1. /*
  2. * @Author: jack.lu
  3. * @Date: 2020/9/11
  4. * @Last Modified by: jack.lu
  5. * @Last Modified time: 2020/9/11 4:35 下午
  6. */
  7. class EmojiDecoder {
  8. emojiMap = null;
  9. url = "";
  10. patterns = [];
  11. metaChars = /[[\]{}()*+?.\\|^$\-,&#\s]/g;
  12. constructor(url,emojiMap) {
  13. this.url = url || '';
  14. this.emojiMap = emojiMap || {};
  15. for (let i in this.emojiMap) {
  16. if (this.emojiMap.hasOwnProperty(i)){
  17. this.patterns.push('('+i.replace(this.metaChars, "\\$&")+')');
  18. }
  19. }
  20. }
  21. decode (text) {
  22. return text.replace(new RegExp(this.patterns.join('|'),'g'), (match) => {
  23. return typeof this.emojiMap[match] != 'undefined' ? '<img height="20rpx" width="20rpx" src="'+this.url+this.emojiMap[match]+'" />' : match;
  24. });
  25. }
  26. }
  27. export default EmojiDecoder