2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-09 23:00:56 +00:00

fix: support various formats when parsing emoji string

This commit is contained in:
Vjacheslav Trushkin 2022-12-10 13:17:26 +02:00
parent 3fe91ddea3
commit b7a987208f
4 changed files with 29 additions and 1 deletions

View File

@ -5,7 +5,11 @@ import { emojiTones, joinerEmoji, vs16Emoji } from './data';
* Get emoji sequence from string * Get emoji sequence from string
*/ */
export function getEmojiSequenceFromString(value: string): number[] { export function getEmojiSequenceFromString(value: string): number[] {
return value.trim().split(/[\s-]/).map(getEmojiCodePoint); return value
.trim()
.split(/[^0-9A-F]+/i)
.filter((item) => item.length > 0)
.map(getEmojiCodePoint);
} }
/** /**

View File

@ -24,6 +24,17 @@ export function createOptimisedRegexForEmojiSequences(
/** /**
* Create optimised regex for emojis * Create optimised regex for emojis
*
* First parameter is array of emojis, entry can be either list of
* code points or emoji sequence as a string
*
* Examples of acceptable strings (case insensitive):
* '1F636 200D 1F32B FE0F' - space separated UTF32 sequence
* '1f636-200d-1f32b-fe0f' - dash separated UTF32 sequence
* 'd83d-de36-200d-d83c-df2b-fe0f' - dash separated UTF16 sequence
* '\\uD83D\\uDE36\\u200D\\uD83C\\uDF2B\\uFE0F' - UTF16 sequence escaped with '\\u'
*
* All examples above refer to the same emoji and will generate the same regex result
*/ */
export function createOptimisedRegex( export function createOptimisedRegex(
emojis: (string | number[])[], emojis: (string | number[])[],

View File

View File

@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */ /* eslint-disable @typescript-eslint/no-non-null-assertion */
import { convertEmojiSequenceToUTF32 } from '../lib';
import { import {
getEmojiSequenceFromString, getEmojiSequenceFromString,
joinEmojiSequences, joinEmojiSequences,
@ -16,6 +17,18 @@ describe('Testing formatting emoji cleanup', () => {
); );
expect(sequence).toEqual([0x1f441, 0xfe0f, 0x200d, 0x1f5e8, 0xfe0f]); expect(sequence).toEqual([0x1f441, 0xfe0f, 0x200d, 0x1f5e8, 0xfe0f]);
// Various representations of the same sequence
expect(
getEmojiSequenceFromString('1f441-fe0f-200d-1f5e8-fe0f')
).toEqual(sequence);
expect(
convertEmojiSequenceToUTF32(
getEmojiSequenceFromString(
'\\uD83D\\uDC41\\uFE0F\\u200D\\uD83D\\uDDE8\\uFE0F'
)
)
).toEqual(sequence);
// Split // Split
const split = splitEmojiSequences(sequence); const split = splitEmojiSequences(sequence);
expect(split).toEqual([ expect(split).toEqual([