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.
Files changed (2) hide show
  1. package/index.js +24 -39
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,15 +1,14 @@
1
1
  /**
2
- * Telegram Bot API 9.4 – Custom Emoji Helper
3
- * Author: ikjava (fixed version)
4
- * ✔ UTF-16 correct
5
- * ✔ Handles spaces, multiple emojis, and all text correctly
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 (what Telegram API uses)
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
- * Main function: Replace emojis with custom emoji entities
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
- const { stripOriginal = true, skipUnmapped = true } = options;
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; // UTF-16 offset in the OUTPUT string
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
- // Mapped emoji found
36
- if (stripOriginal) {
37
- // Replace with placeholder
38
- const placeholder = '\u200B'; // Zero-width space (1 UTF-16 unit)
39
- out += placeholder;
40
- entities.push({
41
- type: 'custom_emoji',
42
- offset: outOffset,
43
- length: 1,
44
- custom_emoji_id: id
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
- // Not a mapped emoji
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 without modifying text
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 contains emoji
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 multiple entity arrays
134
+ * Merge and sort entity arrays
150
135
  */
151
136
  function mergeEntities(baseEntities, newEntities) {
152
137
  const all = [...baseEntities, ...newEntities];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ikemoji",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Telegram Bot API 9.4 custom emoji helper - UTF-16 correct, ZWJ/skin tone safe, Telegraf compatible",
5
5
  "main": "index.js",
6
6
  "scripts": {