我正在尝试从GitHub API加载表情符号,并在JavaScript中将代码点转换为字符串。对于由一个代码点组成的表情符号,这很好用,但对于由多个点组成的表情符号(例如
family_woman_woman_girl_girl
),则失败了。我正在使用零宽连接器(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>