mirror of
https://github.com/iconify/iconify.git
synced 2025-01-06 07:20:40 +00:00
chore(utils): simple function to convert emoji sequence to keyword
This commit is contained in:
parent
37e382d989
commit
6fb8700632
@ -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;
|
||||
|
@ -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('-');
|
||||
}
|
||||
|
@ -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)
|
||||
);
|
||||
|
||||
|
@ -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<string, number[]>;
|
||||
const copiedItems = Object.create(null) as Record<string, number[]>;
|
||||
sequences.forEach((sequence) => {
|
||||
existingItems[getEmojiSequenceString(sequence)] = sequence;
|
||||
existingItems[getEmojiSequenceKeyword(sequence)] = sequence;
|
||||
});
|
||||
|
||||
// Check if item exists
|
||||
|
@ -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)
|
||||
)
|
||||
|
@ -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<number[], number[]> | Record<string, string> {
|
||||
const convert = toString || getEmojiSequenceString;
|
||||
const convert = toString || getEmojiSequenceKeyword;
|
||||
const results = Object.create(null) as Record<string, string>;
|
||||
|
||||
for (let i = 0; i < sequences.length; i++) {
|
||||
|
@ -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
|
||||
);
|
||||
|
@ -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,
|
||||
|
@ -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'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user