emoji.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*jslint node: true*/
  2. require('string.prototype.codepointat');
  3. "use strict";
  4. /**
  5. * regex to parse emoji in a string - finds emoji, e.g. :coffee:
  6. */
  7. var parser = /:([a-zA-Z0-9_\-\+]+):/g;
  8. /**
  9. * Removes colons on either side
  10. * of the string if present
  11. * @param {string} str
  12. * @return {string}
  13. */
  14. var trim = function(str) {
  15. var colonIndex = str.indexOf(':');
  16. if (colonIndex > -1) {
  17. // :emoji: (http://www.emoji-cheat-sheet.com/)
  18. if (colonIndex === str.length - 1) {
  19. str = str.substring(0, colonIndex);
  20. return trim(str);
  21. } else {
  22. str = str.substr(colonIndex + 1);
  23. return trim(str);
  24. }
  25. }
  26. return str;
  27. }
  28. /**
  29. * Emoji namespace
  30. */
  31. var Emoji = module.exports = {
  32. emoji: require('./emoji.json')
  33. };
  34. /**
  35. * get emoji code from name
  36. * @param {string} emoji
  37. * @return {string}
  38. */
  39. Emoji._get = function _get(emoji) {
  40. if (Emoji.emoji.hasOwnProperty(emoji)) {
  41. return Emoji.emoji[emoji];
  42. }
  43. return ':' + emoji + ':';
  44. };
  45. /**
  46. * get emoji code from :emoji: string or name
  47. * @param {string} emoji
  48. * @return {string}
  49. */
  50. Emoji.get = function get(emoji) {
  51. emoji = trim(emoji);
  52. return Emoji._get(emoji);
  53. };
  54. /**
  55. * get emoji name from code
  56. * @param {string} emoji_code
  57. * @return {string}
  58. */
  59. Emoji.which = function which(emoji_code) {
  60. for (var prop in Emoji.emoji) {
  61. if (Emoji.emoji.hasOwnProperty(prop)) {
  62. if (Emoji.emoji[prop].codePointAt() === emoji_code.codePointAt()) {
  63. return prop;
  64. }
  65. }
  66. }
  67. };
  68. /**
  69. * emojify a string (replace :emoji: with an emoji)
  70. * @param {string} str
  71. * @param {function} on_missing (gets emoji name without :: and returns a proper emoji if no emoji was found)
  72. * @return {string}
  73. */
  74. Emoji.emojify = function emojify(str, on_missing) {
  75. if (!str) return '';
  76. return str.split(parser) // parse emoji via regex
  77. .map(function parseEmoji(s, i) {
  78. // every second element is an emoji, e.g. "test :fast_forward:" -> [ "test ", "fast_forward" ]
  79. if (i % 2 === 0) return s;
  80. var emoji = Emoji._get(s);
  81. if (emoji.indexOf(':') > -1 && typeof on_missing === 'function') {
  82. return on_missing(emoji.substr(1, emoji.length-2));
  83. }
  84. return emoji;
  85. })
  86. .join('') // convert back to string
  87. ;
  88. };
  89. /**
  90. * return a random emoji
  91. * @return {string}
  92. */
  93. Emoji.random = function random() {
  94. var emojiKeys = Object.keys(Emoji.emoji);
  95. var randomIndex = Math.floor(Math.random() * emojiKeys.length);
  96. var key = emojiKeys[randomIndex];
  97. var emoji = Emoji._get(key);
  98. return {key: key, emoji: emoji};
  99. }
  100. /**
  101. * return an collection of potential emoji matches
  102. * @param {string} str
  103. * @return {Array.<Object>}
  104. */
  105. Emoji.search = function search(str) {
  106. var emojiKeys = Object.keys(Emoji.emoji);
  107. var matcher = trim(str)
  108. var matchingKeys = emojiKeys.filter(function(key) {
  109. return key.toString().indexOf(matcher) === 0;
  110. });
  111. return matchingKeys.map(function(key) {
  112. return {
  113. key: key,
  114. emoji: Emoji._get(key),
  115. };
  116. });
  117. }