From 6fb8700632d3054a0735b66674d7ff34a4ffc639 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 18 Dec 2022 10:00:40 +0200 Subject: [PATCH] chore(utils): simple function to convert emoji sequence to keyword --- packages/utils/src/emoji/cleanup.ts | 4 ++-- packages/utils/src/emoji/format.ts | 9 +++++++++ packages/utils/src/emoji/replace/find.ts | 4 ++-- packages/utils/src/emoji/test/copy.ts | 12 ++++++------ packages/utils/src/emoji/test/name.ts | 8 ++++---- packages/utils/src/emoji/test/parse.ts | 4 ++-- packages/utils/src/emoji/test/variations.ts | 4 ++-- packages/utils/src/index.ts | 6 +++++- packages/utils/tests/emoji-format-test.ts | 16 +++++++++++++++- 9 files changed, 47 insertions(+), 20 deletions(-) diff --git a/packages/utils/src/emoji/cleanup.ts b/packages/utils/src/emoji/cleanup.ts index 406ac3e..cce14fa 100644 --- a/packages/utils/src/emoji/cleanup.ts +++ b/packages/utils/src/emoji/cleanup.ts @@ -1,6 +1,6 @@ import { getEmojiCodePoint } from './convert'; import { emojiComponents, joinerEmoji, vs16Emoji } from './data'; -import { getEmojiSequenceString } from './format'; +import { getEmojiSequenceKeyword } from './format'; /** * Get emoji sequence from string @@ -133,7 +133,7 @@ export function mapEmojiSequences( // Check for duplicate if (fullOptions.removeDuplicates) { - const value = getEmojiSequenceString(result); + const value = getEmojiSequenceKeyword(result); if (values.has(value)) { // duplicate return; diff --git a/packages/utils/src/emoji/format.ts b/packages/utils/src/emoji/format.ts index 85c4002..d62c920 100644 --- a/packages/utils/src/emoji/format.ts +++ b/packages/utils/src/emoji/format.ts @@ -94,3 +94,12 @@ export function getEmojiSequenceString( ...options, }); } + +/** + * Convert unicode numbers sequence to string + * + * Simple version of `getEmojiSequenceString()` without options that otherwise add to bundle + */ +export function getEmojiSequenceKeyword(sequence: number[]): string { + return sequence.map((code) => code.toString(16)).join('-'); +} diff --git a/packages/utils/src/emoji/replace/find.ts b/packages/utils/src/emoji/replace/find.ts index 4d81f8b..9c72515 100644 --- a/packages/utils/src/emoji/replace/find.ts +++ b/packages/utils/src/emoji/replace/find.ts @@ -1,6 +1,6 @@ import { convertEmojiSequenceToUTF32 } from '../convert'; import { vs16Emoji } from '../data'; -import { getEmojiSequenceString } from '../format'; +import { getEmojiSequenceKeyword } from '../format'; /** * Create regular expression instance @@ -81,7 +81,7 @@ export function getEmojiMatchesInText( } // Get keyword - const keyword = getEmojiSequenceString( + const keyword = getEmojiSequenceKeyword( convertEmojiSequenceToUTF32(sequence) ); diff --git a/packages/utils/src/emoji/test/copy.ts b/packages/utils/src/emoji/test/copy.ts index f07c71a..e7cdef5 100644 --- a/packages/utils/src/emoji/test/copy.ts +++ b/packages/utils/src/emoji/test/copy.ts @@ -1,6 +1,6 @@ import { getUnqualifiedEmojiSequence } from '../cleanup'; import { emojiComponents, EmojiComponentType } from '../data'; -import { getEmojiSequenceString } from '../format'; +import { getEmojiSequenceKeyword } from '../format'; import { mapEmojiTestDataComponents } from './components'; import { EmojiComponentsMapItem, getEmojiComponentsMap } from './name'; import { EmojiTestDataItem, mapEmojiTestDataBySequence } from './parse'; @@ -58,7 +58,7 @@ function getSequence(sequence: number[]): Sequences { const qualified: SequenceData = { type: 'qualified', sequence, - key: getEmojiSequenceString(sequence), + key: getEmojiSequenceKeyword(sequence), }; const unqualifiedSequence = getUnqualifiedEmojiSequence(sequence); @@ -71,7 +71,7 @@ function getSequence(sequence: number[]): Sequences { : { type: 'unqualified', sequence: unqualifiedSequence, - key: getEmojiSequenceString(unqualifiedSequence), + key: getEmojiSequenceKeyword(unqualifiedSequence), }; return { @@ -106,8 +106,8 @@ export function getEmojisSequencesToCopy( // Prepare stuff const componentsMap = mapEmojiTestDataComponents( - mapEmojiTestDataBySequence(testData, getEmojiSequenceString), - getEmojiSequenceString + mapEmojiTestDataBySequence(testData, getEmojiSequenceKeyword), + getEmojiSequenceKeyword ); const componentsMapItems = getEmojiComponentsMap(testData, componentsMap); @@ -115,7 +115,7 @@ export function getEmojisSequencesToCopy( const existingItems = Object.create(null) as Record; const copiedItems = Object.create(null) as Record; sequences.forEach((sequence) => { - existingItems[getEmojiSequenceString(sequence)] = sequence; + existingItems[getEmojiSequenceKeyword(sequence)] = sequence; }); // Check if item exists diff --git a/packages/utils/src/emoji/test/name.ts b/packages/utils/src/emoji/test/name.ts index 23ab52a..707ddef 100644 --- a/packages/utils/src/emoji/test/name.ts +++ b/packages/utils/src/emoji/test/name.ts @@ -1,5 +1,5 @@ import { emojiComponents, EmojiComponentType, vs16Emoji } from '../data'; -import { getEmojiSequenceString } from '../format'; +import { getEmojiSequenceKeyword } from '../format'; import { EmojiTestDataComponentsMap, mapEmojiTestDataComponents, @@ -155,13 +155,13 @@ export function getEmojiComponentsMap( const components = componentsMap || mapEmojiTestDataComponents( - mapEmojiTestDataBySequence(testData, getEmojiSequenceString), - getEmojiSequenceString + mapEmojiTestDataBySequence(testData, getEmojiSequenceKeyword), + getEmojiSequenceKeyword ); // Function to clean sequence const cleanSequence = (sequence: number[]): string => { - return getEmojiSequenceString( + return getEmojiSequenceKeyword( sequence.filter( (num) => num !== vs16Emoji && !components.converted.has(num) ) diff --git a/packages/utils/src/emoji/test/parse.ts b/packages/utils/src/emoji/test/parse.ts index 669cc33..0d8f2ca 100644 --- a/packages/utils/src/emoji/test/parse.ts +++ b/packages/utils/src/emoji/test/parse.ts @@ -2,7 +2,7 @@ import { getEmojiSequenceFromString, getUnqualifiedEmojiSequence, } from '../cleanup'; -import { getEmojiSequenceString } from '../format'; +import { getEmojiSequenceKeyword } from '../format'; // Emoji types export type EmojiStatus = @@ -165,7 +165,7 @@ export function getQualifiedEmojiSequencesMap( sequences: number[][], toString?: (value: number[]) => string ): Map | Record { - const convert = toString || getEmojiSequenceString; + const convert = toString || getEmojiSequenceKeyword; const results = Object.create(null) as Record; for (let i = 0; i < sequences.length; i++) { diff --git a/packages/utils/src/emoji/test/variations.ts b/packages/utils/src/emoji/test/variations.ts index 1f0f1fe..ac38111 100644 --- a/packages/utils/src/emoji/test/variations.ts +++ b/packages/utils/src/emoji/test/variations.ts @@ -6,7 +6,7 @@ import { } from '../cleanup'; import { convertEmojiSequenceToUTF32 } from '../convert'; import { keycapEmoji, vs16Emoji } from '../data'; -import { getEmojiSequenceString } from '../format'; +import { getEmojiSequenceKeyword } from '../format'; import { EmojiTestDataItem, getQualifiedEmojiSequencesMap } from './parse'; /** @@ -59,7 +59,7 @@ export function getQualifiedEmojiVariations( testData: (number[] | EmojiTestDataItem)[] = [], toString?: (value: number[]) => string ): number[][] | string[] { - const convert = toString || getEmojiSequenceString; + const convert = toString || getEmojiSequenceKeyword; const testSequences = testData.map((item) => item instanceof Array ? item : item.sequence ); diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 6232a4b..d0cf130 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -100,7 +100,11 @@ export { convertEmojiSequenceToUTF16, convertEmojiSequenceToUTF32, } from './emoji/convert'; -export { getEmojiUnicodeString, getEmojiSequenceString } from './emoji/format'; +export { + getEmojiUnicodeString, + getEmojiSequenceString, + getEmojiSequenceKeyword, +} from './emoji/format'; export { parseEmojiTestFile, getQualifiedEmojiSequencesMap, diff --git a/packages/utils/tests/emoji-format-test.ts b/packages/utils/tests/emoji-format-test.ts index ad3b582..7bb7611 100644 --- a/packages/utils/tests/emoji-format-test.ts +++ b/packages/utils/tests/emoji-format-test.ts @@ -1,9 +1,13 @@ -import { getEmojiSequenceString } from '../lib/emoji/format'; +import { + getEmojiSequenceString, + getEmojiSequenceKeyword, +} from '../lib/emoji/format'; describe('Testing formatting emoji code points', () => { it('Empty sequence', () => { const sequence: number[] = []; expect(getEmojiSequenceString(sequence)).toBe(''); + expect(getEmojiSequenceKeyword(sequence)).toBe(''); }); it('Keycap sequence', () => { @@ -15,6 +19,8 @@ describe('Testing formatting emoji code points', () => { add0: true, }) ).toBe('0023-fe0f-20e3'); + + expect(getEmojiSequenceKeyword(sequence)).toBe('23-fe0f-20e3'); }); it('UTF-16 sequence', () => { @@ -26,6 +32,10 @@ describe('Testing formatting emoji code points', () => { expect(getEmojiSequenceString(sequence)).toBe( '1f441-fe0f-200d-1f5e8-fe0f' ); + + expect(getEmojiSequenceKeyword(sequence)).toBe( + 'd83d-dc41-fe0f-200d-d83d-dde8-fe0f' + ); }); it('UTF-32 sequence', () => { @@ -35,5 +45,9 @@ describe('Testing formatting emoji code points', () => { expect(getEmojiSequenceString(sequence)).toBe( '1f441-fe0f-200d-1f5e8-fe0f' ); + + expect(getEmojiSequenceKeyword(sequence)).toBe( + '1f441-fe0f-200d-1f5e8-fe0f' + ); }); });