将表情符号代码点翻译成 JavaScript 中的字符串
Translate emoji code points to string in JavaScript
我正在尝试从 the GitHub API and convert the code point(s) to a string in JavaScript. This works fine for emoji comprised of a singe code point but fails for those comprised of multiple points, eg family_woman_woman_girl_girl
. I'm using zero width joiner (zwj) 加载表情符号以连接字符。
const list = document.getElementById('emojis');
const zwj = '\u200D';
async function renderList() {
// load the github emojis: https://developer.github.com/v3/emojis/
const response = await fetch('https://api.github.com/emojis');
const data = await response.json();
// render a list item for each emoji
for (const [key, value] of Object.entries(data)) {
// skip GitHub's custom emoji
if (!/\/unicode\//.test(value)) {
continue;
}
// parse the url into an array of code points
const codePoints = value
.substr(57)
.replace(/\.png\?.*$/, '')
.split('-')
.map(hex => parseInt(hex, 16));
// translate the code points to a string. SOMETHING WRONG HERE
const emoji = codePoints
.map(p => String.fromCodePoint(p))
.join(zwj);
// render the list item
const li = document.createElement('li');
li.textContent = `${key}: ${codePoints} ${emoji}`;
list.appendChild(li);
}
}
renderList();
<ul id="emojis"></ul>
并非所有表情符号序列都与 ZWJ 粘合在一起。最值得注意的是,人和他们的肤色只是简单地结合在一起,没有任何填充物。
Unicode 将 a list of all code points/combinations 视为表情符号。 emoji-data.txt
文件都是 single-cp 表情符号。 emoji-zwj-sequences.txt
是包含至少一个 ZWJ 和 emoji-sequences.txt
其余序列的所有序列。
请注意,并非 emoji-zwj-sequences.txt
中的所有 cps 都与 ZWJ 粘合在一起,例如,这一行:
1F469 1F3FD 200D 1F4BB ; Emoji_ZWJ_Sequence ; woman technologist: medium skin tone # 8.0 [1] ()
artist
部分添加了ZWJ。女色和肤色是连在一起的,没有其他cp。
有一些启发式方法可以找出您是否需要 ZWJ。如果您查看 emoji-data.txt
的末尾,您会看到肤色修饰符具有 属性 Emoji_Modifier
。这些被定义为简单地改变以前表情符号的外观,如果它有 Emoji_Modifier_Base
属性.
下一个块 Emoji_Components
也可以在没有 ZWJ 的情况下以一种或另一种方式组合。
我正在尝试从 the GitHub API and convert the code point(s) to a string in JavaScript. This works fine for emoji comprised of a singe code point but fails for those comprised of multiple points, eg family_woman_woman_girl_girl
. I'm using zero width joiner (zwj) 加载表情符号以连接字符。
const list = document.getElementById('emojis');
const zwj = '\u200D';
async function renderList() {
// load the github emojis: https://developer.github.com/v3/emojis/
const response = await fetch('https://api.github.com/emojis');
const data = await response.json();
// render a list item for each emoji
for (const [key, value] of Object.entries(data)) {
// skip GitHub's custom emoji
if (!/\/unicode\//.test(value)) {
continue;
}
// parse the url into an array of code points
const codePoints = value
.substr(57)
.replace(/\.png\?.*$/, '')
.split('-')
.map(hex => parseInt(hex, 16));
// translate the code points to a string. SOMETHING WRONG HERE
const emoji = codePoints
.map(p => String.fromCodePoint(p))
.join(zwj);
// render the list item
const li = document.createElement('li');
li.textContent = `${key}: ${codePoints} ${emoji}`;
list.appendChild(li);
}
}
renderList();
<ul id="emojis"></ul>
并非所有表情符号序列都与 ZWJ 粘合在一起。最值得注意的是,人和他们的肤色只是简单地结合在一起,没有任何填充物。
Unicode 将 a list of all code points/combinations 视为表情符号。 emoji-data.txt
文件都是 single-cp 表情符号。 emoji-zwj-sequences.txt
是包含至少一个 ZWJ 和 emoji-sequences.txt
其余序列的所有序列。
请注意,并非 emoji-zwj-sequences.txt
中的所有 cps 都与 ZWJ 粘合在一起,例如,这一行:
1F469 1F3FD 200D 1F4BB ; Emoji_ZWJ_Sequence ; woman technologist: medium skin tone # 8.0 [1] ()
artist
部分添加了ZWJ。女色和肤色是连在一起的,没有其他cp。
有一些启发式方法可以找出您是否需要 ZWJ。如果您查看 emoji-data.txt
的末尾,您会看到肤色修饰符具有 属性 Emoji_Modifier
。这些被定义为简单地改变以前表情符号的外观,如果它有 Emoji_Modifier_Base
属性.
下一个块 Emoji_Components
也可以在没有 ZWJ 的情况下以一种或另一种方式组合。