EmojiDecoder.js 726 B

123456789101112131415161718192021222324
  1. class EmojiDecoder {
  2. emojiMap = null;
  3. url = "";
  4. patterns = [];
  5. metaChars = /[[\]{}()*+?.\\|^$\-,&#\s]/g;
  6. constructor(url,emojiMap) {
  7. this.url = url || '';
  8. this.emojiMap = emojiMap || {};
  9. for (let i in this.emojiMap) {
  10. if (this.emojiMap.hasOwnProperty(i)){
  11. this.patterns.push('('+i.replace(this.metaChars, "\\$&")+')');
  12. }
  13. }
  14. }
  15. decode (text) {
  16. return text.replace(new RegExp(this.patterns.join('|'),'g'), (match) => {
  17. return typeof this.emojiMap[match] != 'undefined' ? '<img height="20rpx" width="20rpx" src="'+this.url+this.emojiMap[match]+'" />' : match;
  18. });
  19. }
  20. }
  21. export default EmojiDecoder