Fix emoji
This commit is contained in:
parent
9339260fde
commit
95353248f5
36
src/emoji/index.ts
Normal file
36
src/emoji/index.ts
Normal file
@ -0,0 +1,36 @@
|
||||
// https://github.com/twitter/twemoji-parser/blob/master/src/lib/regex.js
|
||||
|
||||
export function encodeEmoji(emojiText: string) {
|
||||
const codepoints = toCodePoints(removeVS16s(emojiText)).join('-');
|
||||
return codepoints;
|
||||
}
|
||||
|
||||
const vs16RegExp = /\uFE0F/g;
|
||||
// avoid using a string literal like '\u200D' here because minifiers expand it inline
|
||||
const zeroWidthJoiner = String.fromCharCode(0x200d);
|
||||
|
||||
const removeVS16s = (rawEmoji: string) => (rawEmoji.indexOf(zeroWidthJoiner) < 0 ? rawEmoji.replace(vs16RegExp, '') : rawEmoji);
|
||||
|
||||
export function toCodePoints(unicodeSurrogates: string): Array<string> {
|
||||
const points = [];
|
||||
let char = 0;
|
||||
let previous = 0;
|
||||
let i = 0;
|
||||
while(i < unicodeSurrogates.length) {
|
||||
char = unicodeSurrogates.charCodeAt(i++);
|
||||
if(previous) {
|
||||
points.push((0x10000 + ((previous - 0xd800) << 10) + (char - 0xdc00)).toString(16));
|
||||
previous = 0;
|
||||
} else if (char > 0xd800 && char <= 0xdbff) {
|
||||
previous = char;
|
||||
} else {
|
||||
points.push(char.toString(16));
|
||||
}
|
||||
}
|
||||
|
||||
if(points.length && points[0].length == 2) {
|
||||
points[0] = '00' + points[0];
|
||||
}
|
||||
|
||||
return points;
|
||||
}
|
4
src/emoji/regex.ts
Normal file
4
src/emoji/regex.ts
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -482,57 +482,6 @@ export function calcImageInBox (imageW: number, imageH: number, boxW: number, bo
|
||||
return {w: boxedImageW, h: boxedImageH};
|
||||
}
|
||||
|
||||
/**
|
||||
* emojiUnicode
|
||||
* Get the unicode code of an emoji in base 16.
|
||||
*
|
||||
* @name emojiUnicode
|
||||
* @function
|
||||
* @param {String} input The emoji character.
|
||||
* @returns {String} The base 16 unicode code.
|
||||
*/
|
||||
export function emojiUnicode(input: string) {
|
||||
let pairs = emojiUnicode.raw(input).split(' ').map(val => parseInt(val).toString(16))/* .filter(p => p != 'fe0f') */;
|
||||
if(pairs.length && pairs[0].length == 2) pairs[0] = '00' + pairs[0];
|
||||
return pairs.join('-');
|
||||
}
|
||||
|
||||
/**
|
||||
* emojiunicode.raw
|
||||
* Get the unicode code points of an emoji in base 16.
|
||||
*
|
||||
* @name emojiunicode.raw
|
||||
* @function
|
||||
* @param {String} input The emoji character.
|
||||
* @returns {String} The unicode code points.
|
||||
*/
|
||||
emojiUnicode.raw = function(input: string) {
|
||||
if(input.length === 1) {
|
||||
return input.charCodeAt(0).toString();
|
||||
} else if(input.length > 1) {
|
||||
const pairs = [];
|
||||
for(var i = 0; i < input.length; i++) {
|
||||
// high surrogate
|
||||
if(input.charCodeAt(i) >= 0xd800 && input.charCodeAt(i) <= 0xdbff) {
|
||||
if(input.charCodeAt(i + 1) >= 0xdc00 && input.charCodeAt(i + 1) <= 0xdfff) {
|
||||
// low surrogate
|
||||
pairs.push(
|
||||
(input.charCodeAt(i) - 0xd800) * 0x400
|
||||
+ (input.charCodeAt(i + 1) - 0xdc00) + 0x10000
|
||||
);
|
||||
}
|
||||
} else if(input.charCodeAt(i) < 0xd800 || input.charCodeAt(i) > 0xdfff) {
|
||||
// modifiers and joiners
|
||||
pairs.push(input.charCodeAt(i))
|
||||
}
|
||||
}
|
||||
|
||||
return pairs.join(' ');
|
||||
}
|
||||
|
||||
return '';
|
||||
};
|
||||
|
||||
export function getEmojiToneIndex(input: string) {
|
||||
let match = input.match(/[\uDFFB-\uDFFF]/);
|
||||
return match ? 5 - (57343 - match[0].charCodeAt(0)) : 0;
|
||||
|
@ -92,6 +92,8 @@ if(false) {
|
||||
|
||||
let emoji = unified/* .replace(/-FE0F/gi, '') */.split('-')
|
||||
.reduce((prev, curr) => prev + String.fromCodePoint(parseInt(curr, 16)), '');
|
||||
|
||||
//emoji = emoji.replace(/\ufe0f/g, '');
|
||||
|
||||
let c = categories[category] === undefined ? 9 : categories[category];
|
||||
//obj[emoji] = '' + c + sort_order;
|
||||
|
Loading…
x
Reference in New Issue
Block a user