ikemoji 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +24 -39
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Telegram Bot API
|
|
3
|
-
*
|
|
4
|
-
* ✔ UTF-16 correct
|
|
5
|
-
* ✔ Handles
|
|
6
|
-
* ✔ Telegraf compatible
|
|
2
|
+
* Telegram Bot API Custom Emoji Helper by (ikjava)
|
|
3
|
+
* ✔ Keeps original emoji in text (REQUIRED by Telegram)
|
|
4
|
+
* ✔ UTF-16 correct offsets
|
|
5
|
+
* ✔ Handles multiple emojis
|
|
7
6
|
*/
|
|
8
7
|
|
|
9
8
|
const segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' });
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
|
-
* Get UTF-16 code unit length
|
|
11
|
+
* Get UTF-16 code unit length
|
|
13
12
|
*/
|
|
14
13
|
function getUtf16Length(str) {
|
|
15
14
|
let length = 0;
|
|
@@ -20,52 +19,38 @@ function getUtf16Length(str) {
|
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
/**
|
|
23
|
-
*
|
|
22
|
+
* Replace emojis with custom emoji entities
|
|
23
|
+
* IMPORTANT: stripOriginal MUST be false for Telegram Bot API
|
|
24
24
|
*/
|
|
25
25
|
function emo(text, emojiMap, options = {}) {
|
|
26
|
-
|
|
26
|
+
// Force stripOriginal to false - Telegram REQUIRES the original emoji
|
|
27
|
+
const { skipUnmapped = true } = options;
|
|
27
28
|
const entities = [];
|
|
28
29
|
let out = '';
|
|
29
|
-
let outOffset = 0;
|
|
30
|
+
let outOffset = 0;
|
|
30
31
|
|
|
31
32
|
for (const { segment } of segmenter.segment(text)) {
|
|
32
33
|
const id = emojiMap[segment];
|
|
33
34
|
|
|
34
35
|
if (id) {
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
});
|
|
46
|
-
outOffset += 1;
|
|
47
|
-
} else {
|
|
48
|
-
// Keep original emoji
|
|
49
|
-
out += segment;
|
|
50
|
-
const segmentLength = getUtf16Length(segment);
|
|
51
|
-
entities.push({
|
|
52
|
-
type: 'custom_emoji',
|
|
53
|
-
offset: outOffset,
|
|
54
|
-
length: segmentLength,
|
|
55
|
-
custom_emoji_id: id
|
|
56
|
-
});
|
|
57
|
-
outOffset += segmentLength;
|
|
58
|
-
}
|
|
36
|
+
// Keep the original emoji (REQUIRED by Telegram)
|
|
37
|
+
out += segment;
|
|
38
|
+
const segmentLength = getUtf16Length(segment);
|
|
39
|
+
entities.push({
|
|
40
|
+
type: 'custom_emoji',
|
|
41
|
+
offset: outOffset,
|
|
42
|
+
length: segmentLength,
|
|
43
|
+
custom_emoji_id: id
|
|
44
|
+
});
|
|
45
|
+
outOffset += segmentLength;
|
|
59
46
|
} else {
|
|
60
|
-
//
|
|
47
|
+
// Regular text or unmapped emoji
|
|
61
48
|
const isEmoji = isEmo(segment);
|
|
62
49
|
|
|
63
50
|
if (!skipUnmapped || !isEmoji) {
|
|
64
|
-
// Keep this segment (regular text, spaces, or unmapped emojis)
|
|
65
51
|
out += segment;
|
|
66
52
|
outOffset += getUtf16Length(segment);
|
|
67
53
|
}
|
|
68
|
-
// If skipUnmapped=true and it's an unmapped emoji, skip it entirely
|
|
69
54
|
}
|
|
70
55
|
}
|
|
71
56
|
|
|
@@ -73,7 +58,7 @@ function emo(text, emojiMap, options = {}) {
|
|
|
73
58
|
}
|
|
74
59
|
|
|
75
60
|
/**
|
|
76
|
-
* Generate entities
|
|
61
|
+
* Generate entities for existing text with emojis
|
|
77
62
|
*/
|
|
78
63
|
function emoEnt(text, emojiMap) {
|
|
79
64
|
const entities = [];
|
|
@@ -139,14 +124,14 @@ function checkEnt(entities, text) {
|
|
|
139
124
|
}
|
|
140
125
|
|
|
141
126
|
/**
|
|
142
|
-
* Check if string
|
|
127
|
+
* Check if string is emoji
|
|
143
128
|
*/
|
|
144
129
|
function isEmo(str) {
|
|
145
130
|
return /\p{Extended_Pictographic}/u.test(str);
|
|
146
131
|
}
|
|
147
132
|
|
|
148
133
|
/**
|
|
149
|
-
* Merge and sort
|
|
134
|
+
* Merge and sort entity arrays
|
|
150
135
|
*/
|
|
151
136
|
function mergeEntities(baseEntities, newEntities) {
|
|
152
137
|
const all = [...baseEntities, ...newEntities];
|