Repository navigation
Expand file tree
/
Copy pathemoji.js
More file actions
71 lines (63 loc) · 1.56 KB
/
Copy pathemoji.js
File metadata and controls
71 lines (63 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*jslint node: true*/
require('string.prototype.codepointat');
"use strict";
/**
* regex to parse emoji in a string - finds emoji, e.g. :coffee:
*/
var parser = /:([a-zA-Z0-9_\-\+]+):/g;
/**
* Emoji namespace
*/
var Emoji = module.exports = {
emoji: require('./emoji.json')
};
/**
* get emoji code from name
* @param {string} emoji
* @return {string}
*/
Emoji._get = function _get(emoji) {
if (Emoji.emoji.hasOwnProperty(emoji)) {
return Emoji.emoji[emoji];
}
return ':' + emoji + ':';
};
/**
* get emoji code from :emoji: string or name
* @param {string} emoji
* @return {string}
*/
Emoji.get = function get(emoji) {
if (emoji.indexOf(':') > -1) {
// :emoji: (http://www.emoji-cheat-sheet.com/)
emoji = emoji.substr(1, emoji.length-2);
}
return Emoji._get(emoji);
};
/**
* get emoji name from code
* @param {string} emoji_code
* @return {string}
*/
Emoji.which = function which(emoji_code) {
for (var prop in Emoji.emoji) {
if (Emoji.emoji.hasOwnProperty(prop)) {
if (Emoji.emoji[prop].codePointAt() === emoji_code.codePointAt()) {
return prop;
}
}
}
};
/**
* emojify a string (replace :emoji: with an emoji)
* @param {string} str
* @return {string}
*/
Emoji.emojify = function emojify(str) {
return str.split(parser) // parse emoji via regex
.map(function parseEmoji(s, i) {
return (i % 2 === 0) ? s : Emoji._get(s); // every second element is an emoji, e.g. "test :fast_forward:" -> [ "test ", "fast_forward" ]
})
.join('') // convert back to string
;
};