emoji.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*jslint node: true*/
  2. /*jslint expr: true*/
  3. /*global describe, it*/
  4. "use strict";
  5. var should = require('should');
  6. var emoji = require('../index');
  7. describe("emoji.js", function () {
  8. describe("get(emoji)", function () {
  9. it("should return an emoji code when passed a string", function () {
  10. var coffee = emoji.get('coffee');
  11. should.exist(coffee);
  12. coffee.should.be.exactly('☕');
  13. });
  14. it("should return emoji code back when passed an emoji code", function () {
  15. var dancers = emoji.get('👯‍♀️');
  16. should.exist(dancers);
  17. dancers.should.be.exactly('👯‍♀️');
  18. })
  19. it("should support github flavored markdown emoji", function () {
  20. var coffee = emoji.get(':coffee:');
  21. should.exist(coffee);
  22. coffee.should.be.exactly('☕');
  23. });
  24. });
  25. describe("random()", function () {
  26. it("should return a random emoji and the corresponding key", function () {
  27. var result = emoji.random();
  28. should.exist(result);
  29. should.exist(result.key);
  30. should.exist(result.emoji);
  31. result.emoji.should.be.exactly(emoji.get(result.key));
  32. });
  33. });
  34. describe("which(emoji_code)", function () {
  35. it("should return name of the emoji", function () {
  36. var coffee = emoji.which('☕');
  37. should.exist(coffee);
  38. coffee.should.be.exactly('coffee');
  39. });
  40. it("should work for differently formed characters", function () {
  41. var umbrella = emoji.which('☔');
  42. should.exist(umbrella);
  43. umbrella.should.be.exactly('umbrella_with_rain_drops');
  44. });
  45. it("should return the same name for differently formed characters", function () {
  46. var umbrella1 = emoji.which('☔');
  47. should.exist(umbrella1);
  48. var umbrella2 = emoji.which('☔️');
  49. should.exist(umbrella2);
  50. umbrella1.should.equal(umbrella2);
  51. });
  52. it("should work for flags", function() {
  53. var mexico = emoji.which('🇲🇽');
  54. should.exists(mexico);
  55. mexico.should.be.exactly('flag-mx');
  56. var marocco = emoji.which('🇲🇦');
  57. should.exists(marocco);
  58. marocco.should.be.exactly('flag-ma');
  59. // see issue #21
  60. mexico.should.not.equal(marocco);
  61. });
  62. });
  63. describe("emojify(str)", function () {
  64. it("should parse :emoji: in a string and replace them with the right emoji", function () {
  65. var coffee = emoji.emojify('I :heart: :coffee:! - :hushed::star::heart_eyes: ::: test : : :+1:+');
  66. should.exist(coffee);
  67. coffee.should.be.exactly('I ❤️ ☕! - 😯⭐😍 ::: test : : 👍+');
  68. });
  69. it("should handle flags correctly", function() {
  70. var flags = emoji.emojify('Mexico :flag-mx: and Marocco :flag-ma: are not the same');
  71. should.exists(flags);
  72. flags.should.be.exactly('Mexico 🇲🇽 and Marocco 🇲🇦 are not the same');
  73. });
  74. it("should leave unknown emoji", function () {
  75. var coffee = emoji.emojify('I :unknown_emoji: :star: :another_one:');
  76. should.exist(coffee);
  77. coffee.should.be.exactly('I :unknown_emoji: ⭐ :another_one:');
  78. });
  79. it("should replace unknown emoji using provided cb function", function () {
  80. var coffee = emoji.emojify('I :unknown_emoji: :star: :another_one:', function(name) {
  81. return name;
  82. });
  83. should.exist(coffee);
  84. coffee.should.be.exactly('I unknown_emoji ⭐ another_one');
  85. });
  86. it("should wrap emoji using provided format function", function () {
  87. var coffee = emoji.emojify('I :heart: :coffee:', null, function(code, name) {
  88. return '<img alt="' + code + '" src="' + name + '.png" />';
  89. });
  90. should.exist(coffee);
  91. coffee.should.be.exactly('I <img alt="❤️" src="heart.png" /> <img alt="☕" src="coffee.png" />');
  92. });
  93. it("should not wrap unknown using provided format function", function () {
  94. var coffee = emoji.emojify('I :unknown_emoji: :coffee:', null, function(code, name) {
  95. return '<img alt="' + code + '" src="' + name + '.png" />';
  96. });
  97. should.exist(coffee);
  98. coffee.should.be.exactly('I :unknown_emoji: <img alt="☕" src="coffee.png" />');
  99. });
  100. it("should replace unknown emojis and wrap known emojis using cb functions", function () {
  101. var coffee = emoji.emojify('I :unknown_emoji: :coffee:',
  102. function(name) {
  103. return name;
  104. },
  105. function(code, name) {
  106. return '<img alt="' + code + '" src="' + name + '.png" />';
  107. }
  108. );
  109. should.exist(coffee);
  110. coffee.should.be.exactly('I unknown_emoji <img alt="☕" src="coffee.png" />');
  111. });
  112. });
  113. it("should return an emoji code", function () {
  114. var coffee = emoji.emoji.coffee;
  115. should.exist(coffee);
  116. coffee.should.be.exactly('☕');
  117. });
  118. describe("search(str)", function () {
  119. it("should return partially matched emojis", function () {
  120. var matchingEmojis = emoji.search("cof");
  121. matchingEmojis.length.should.not.eql(0);
  122. matchingEmojis.forEach(function(emoji) {
  123. emoji.key.should.match(/^cof/);
  124. });
  125. });
  126. it("should only include emojies that begin with the search", function () {
  127. var matchingEmojis = emoji.search("ca");
  128. matchingEmojis.length.should.not.eql(0);
  129. matchingEmojis.forEach(function(emoji) {
  130. var index = emoji.key.indexOf("ca");
  131. index.should.be.exactly(0);
  132. });
  133. });
  134. it("should match when you include the colon", function () {
  135. var matchingEmojis = emoji.search(":c");
  136. matchingEmojis.length.should.not.eql(0);
  137. matchingEmojis.forEach(function(emoji) {
  138. var index = emoji.key.indexOf("c");
  139. index.should.be.exactly(0);
  140. });
  141. });
  142. it("should return an empty array when no matching emojis are found", function () {
  143. var matchingEmojis = emoji.search("notAnEmoji");
  144. matchingEmojis.length.should.be.exactly(0);
  145. });
  146. });
  147. describe("unemojify(str)", function () {
  148. it("should parse emoji and replace them with :emoji:", function() {
  149. var coffee = emoji.unemojify('I ❤️ ☕️! - 😯⭐️😍 ::: test : : 👍+');
  150. should.exist(coffee);
  151. coffee.should.be.exactly('I :heart: :coffee:! - :hushed::star::heart_eyes: ::: test : : :thumbsup:+');
  152. })
  153. it("should leave unknown emoji", function () {
  154. // Emoji :melting_face: (U+1FAE0) Unicode 14.0 draft: https://emojipedia.org/unicode-14.0
  155. var coffee = emoji.unemojify('I ⭐️ :another_one: 🫠');
  156. should.exist(coffee);
  157. coffee.should.be.exactly('I :star: :another_one: 🫠');
  158. });
  159. it("should parse a complex emoji like woman-kiss-woman and replace it with :woman-kiss-woman:", function() {
  160. var coffee = emoji.unemojify('I love 👩‍❤️‍💋‍👩');
  161. should.exist(coffee);
  162. coffee.should.be.exactly('I love :woman-kiss-woman:');
  163. });
  164. it("should parse flags correctly", function () {
  165. var flags = emoji.unemojify('The flags of 🇲🇽 and 🇲🇦 are not the same');
  166. should.exists(flags);
  167. flags.should.be.exactly('The flags of :flag-mx: and :flag-ma: are not the same');
  168. });
  169. });
  170. describe('find emoji', function() {
  171. it('Should be able to find a emoji by :name:', function() {
  172. var result = emoji.find(':heart:')
  173. should.exists(result);
  174. result.should.eql({ emoji: '❤️', key: 'heart' });
  175. });
  176. it('Should be able to find an emoji by name', function() {
  177. var result = emoji.find('heart');
  178. should.exists(result);
  179. result.should.eql({ emoji: '❤️', key: 'heart' });
  180. });
  181. it('Should be able to find an emoji by code', function() {
  182. var result = emoji.find('❤');
  183. should.exists(result);
  184. result.should.eql({ emoji: '❤️', key: 'heart' });
  185. });
  186. it('Should return `undefined` for unknown emojis', function() {
  187. var result = emoji.find('unknown_emoji');
  188. should.not.exists(result);
  189. })
  190. });
  191. describe('hasEmoji', function() {
  192. it('Should be able to check a emoji by :name:', function() {
  193. var result = emoji.hasEmoji(':heart:');
  194. result.should.equal(true)
  195. });
  196. it('Should be able to check a emoji by name', function() {
  197. var result = emoji.hasEmoji('heart');
  198. result.should.equal(true);
  199. });
  200. it('Should be able to check a emoji by code text form)', function() {
  201. var result = emoji.hasEmoji('❤');
  202. result.should.equal(true);
  203. });
  204. it('Should be able to check a emoji by code in variant form', function() {
  205. var result = emoji.hasEmoji('❤️');
  206. result.should.equal(true);
  207. });
  208. it('Should return false for unknown emoji names', function() {
  209. var result = emoji.hasEmoji(':pizza-kiss-coffee:');
  210. result.should.equal(false);
  211. });
  212. it('Should return false for unknown emoji codes', function() {
  213. var result = emoji.hasEmoji('🍕❤️‍💋‍☕');
  214. result.should.equal(false);
  215. });
  216. });
  217. describe('replace', function() {
  218. it('Should be able to strip emojis', function() {
  219. var result = emoji.replace('Host: eseaps001 Addr: 10.XX.XX.XX: - ⚠️ 〰️ 〰️ low disk space', '', true);
  220. result.should.equal('Host: eseaps001 Addr: 10.XX.XX.XX: - low disk space');
  221. });
  222. it('Should keep the trailing spaces when not explicitly told to clean', function() {
  223. var result = emoji.replace('Host: eseaps001 Addr: 10.XX.XX.XX: - ⚠️ 〰️ 〰️ low disk space', '');
  224. result.should.equal('Host: eseaps001 Addr: 10.XX.XX.XX: - low disk space');
  225. });
  226. it('Should be able to strip a emoji by code text form', function() {
  227. var result = emoji.replace('I ❤ coffee', '', true);
  228. result.should.equal('I coffee');
  229. });
  230. it('Should be able to strip a emoji by code in variant form', function() {
  231. var result = emoji.replace('I ❤️ cleaning', '', true);
  232. result.should.equal('I cleaning');
  233. });
  234. it('Should be able to strip complex emojis', function() {
  235. var result = emoji.replace('Where did this 👩‍❤️‍💋‍👩 happen?', '', true);
  236. result.should.equal('Where did this happen?');
  237. });
  238. it('Should be able to strip flag emojis', function() {
  239. var result = emoji.replace('There is no flag 🇲🇽', '', true);
  240. result.should.equal('There is no flag');
  241. });
  242. it('Should be able to replace by callback function', function() {
  243. var result = emoji.replace('There is no ⚠ on my hard drive', function (emoji) {
  244. return emoji.key;
  245. });
  246. result.should.equal('There is no warning on my hard drive');
  247. });
  248. it('Non existing complex emojis are known to be ignored', function() {
  249. var result = emoji.replace('Some 🍕❤️‍💋‍☕ emoji', '');
  250. result.should.not.equal('Some emoji');
  251. });
  252. });
  253. describe('strip', function() {
  254. it('Should be able to strip emojis', function() {
  255. var result = emoji.strip('Host: eseaps001 Addr: 10.XX.XX.XX: - ⚠️ 〰️ 〰️ low disk space');
  256. result.should.equal('Host: eseaps001 Addr: 10.XX.XX.XX: - low disk space');
  257. });
  258. });
  259. });