mirror of
https://github.com/iconify/iconify.git
synced 2025-01-22 22:58:27 +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 { getEmojiCodePoint } from './convert';
|
||||||
import { emojiComponents, joinerEmoji, vs16Emoji } from './data';
|
import { emojiComponents, joinerEmoji, vs16Emoji } from './data';
|
||||||
import { getEmojiSequenceString } from './format';
|
import { getEmojiSequenceKeyword } from './format';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get emoji sequence from string
|
* Get emoji sequence from string
|
||||||
@ -133,7 +133,7 @@ export function mapEmojiSequences(
|
|||||||
|
|
||||||
// Check for duplicate
|
// Check for duplicate
|
||||||
if (fullOptions.removeDuplicates) {
|
if (fullOptions.removeDuplicates) {
|
||||||
const value = getEmojiSequenceString(result);
|
const value = getEmojiSequenceKeyword(result);
|
||||||
if (values.has(value)) {
|
if (values.has(value)) {
|
||||||
// duplicate
|
// duplicate
|
||||||
return;
|
return;
|
||||||
|
@ -94,3 +94,12 @@ export function getEmojiSequenceString(
|
|||||||
...options,
|
...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 { convertEmojiSequenceToUTF32 } from '../convert';
|
||||||
import { vs16Emoji } from '../data';
|
import { vs16Emoji } from '../data';
|
||||||
import { getEmojiSequenceString } from '../format';
|
import { getEmojiSequenceKeyword } from '../format';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create regular expression instance
|
* Create regular expression instance
|
||||||
@ -81,7 +81,7 @@ export function getEmojiMatchesInText(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get keyword
|
// Get keyword
|
||||||
const keyword = getEmojiSequenceString(
|
const keyword = getEmojiSequenceKeyword(
|
||||||
convertEmojiSequenceToUTF32(sequence)
|
convertEmojiSequenceToUTF32(sequence)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { getUnqualifiedEmojiSequence } from '../cleanup';
|
import { getUnqualifiedEmojiSequence } from '../cleanup';
|
||||||
import { emojiComponents, EmojiComponentType } from '../data';
|
import { emojiComponents, EmojiComponentType } from '../data';
|
||||||
import { getEmojiSequenceString } from '../format';
|
import { getEmojiSequenceKeyword } from '../format';
|
||||||
import { mapEmojiTestDataComponents } from './components';
|
import { mapEmojiTestDataComponents } from './components';
|
||||||
import { EmojiComponentsMapItem, getEmojiComponentsMap } from './name';
|
import { EmojiComponentsMapItem, getEmojiComponentsMap } from './name';
|
||||||
import { EmojiTestDataItem, mapEmojiTestDataBySequence } from './parse';
|
import { EmojiTestDataItem, mapEmojiTestDataBySequence } from './parse';
|
||||||
@ -58,7 +58,7 @@ function getSequence(sequence: number[]): Sequences {
|
|||||||
const qualified: SequenceData = {
|
const qualified: SequenceData = {
|
||||||
type: 'qualified',
|
type: 'qualified',
|
||||||
sequence,
|
sequence,
|
||||||
key: getEmojiSequenceString(sequence),
|
key: getEmojiSequenceKeyword(sequence),
|
||||||
};
|
};
|
||||||
|
|
||||||
const unqualifiedSequence = getUnqualifiedEmojiSequence(sequence);
|
const unqualifiedSequence = getUnqualifiedEmojiSequence(sequence);
|
||||||
@ -71,7 +71,7 @@ function getSequence(sequence: number[]): Sequences {
|
|||||||
: {
|
: {
|
||||||
type: 'unqualified',
|
type: 'unqualified',
|
||||||
sequence: unqualifiedSequence,
|
sequence: unqualifiedSequence,
|
||||||
key: getEmojiSequenceString(unqualifiedSequence),
|
key: getEmojiSequenceKeyword(unqualifiedSequence),
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -106,8 +106,8 @@ export function getEmojisSequencesToCopy(
|
|||||||
|
|
||||||
// Prepare stuff
|
// Prepare stuff
|
||||||
const componentsMap = mapEmojiTestDataComponents(
|
const componentsMap = mapEmojiTestDataComponents(
|
||||||
mapEmojiTestDataBySequence(testData, getEmojiSequenceString),
|
mapEmojiTestDataBySequence(testData, getEmojiSequenceKeyword),
|
||||||
getEmojiSequenceString
|
getEmojiSequenceKeyword
|
||||||
);
|
);
|
||||||
const componentsMapItems = getEmojiComponentsMap(testData, componentsMap);
|
const componentsMapItems = getEmojiComponentsMap(testData, componentsMap);
|
||||||
|
|
||||||
@ -115,7 +115,7 @@ export function getEmojisSequencesToCopy(
|
|||||||
const existingItems = Object.create(null) as Record<string, number[]>;
|
const existingItems = Object.create(null) as Record<string, number[]>;
|
||||||
const copiedItems = Object.create(null) as Record<string, number[]>;
|
const copiedItems = Object.create(null) as Record<string, number[]>;
|
||||||
sequences.forEach((sequence) => {
|
sequences.forEach((sequence) => {
|
||||||
existingItems[getEmojiSequenceString(sequence)] = sequence;
|
existingItems[getEmojiSequenceKeyword(sequence)] = sequence;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check if item exists
|
// Check if item exists
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { emojiComponents, EmojiComponentType, vs16Emoji } from '../data';
|
import { emojiComponents, EmojiComponentType, vs16Emoji } from '../data';
|
||||||
import { getEmojiSequenceString } from '../format';
|
import { getEmojiSequenceKeyword } from '../format';
|
||||||
import {
|
import {
|
||||||
EmojiTestDataComponentsMap,
|
EmojiTestDataComponentsMap,
|
||||||
mapEmojiTestDataComponents,
|
mapEmojiTestDataComponents,
|
||||||
@ -155,13 +155,13 @@ export function getEmojiComponentsMap(
|
|||||||
const components =
|
const components =
|
||||||
componentsMap ||
|
componentsMap ||
|
||||||
mapEmojiTestDataComponents(
|
mapEmojiTestDataComponents(
|
||||||
mapEmojiTestDataBySequence(testData, getEmojiSequenceString),
|
mapEmojiTestDataBySequence(testData, getEmojiSequenceKeyword),
|
||||||
getEmojiSequenceString
|
getEmojiSequenceKeyword
|
||||||
);
|
);
|
||||||
|
|
||||||
// Function to clean sequence
|
// Function to clean sequence
|
||||||
const cleanSequence = (sequence: number[]): string => {
|
const cleanSequence = (sequence: number[]): string => {
|
||||||
return getEmojiSequenceString(
|
return getEmojiSequenceKeyword(
|
||||||
sequence.filter(
|
sequence.filter(
|
||||||
(num) => num !== vs16Emoji && !components.converted.has(num)
|
(num) => num !== vs16Emoji && !components.converted.has(num)
|
||||||
)
|
)
|
||||||
|
@ -2,7 +2,7 @@ import {
|
|||||||
getEmojiSequenceFromString,
|
getEmojiSequenceFromString,
|
||||||
getUnqualifiedEmojiSequence,
|
getUnqualifiedEmojiSequence,
|
||||||
} from '../cleanup';
|
} from '../cleanup';
|
||||||
import { getEmojiSequenceString } from '../format';
|
import { getEmojiSequenceKeyword } from '../format';
|
||||||
|
|
||||||
// Emoji types
|
// Emoji types
|
||||||
export type EmojiStatus =
|
export type EmojiStatus =
|
||||||
@ -165,7 +165,7 @@ export function getQualifiedEmojiSequencesMap(
|
|||||||
sequences: number[][],
|
sequences: number[][],
|
||||||
toString?: (value: number[]) => string
|
toString?: (value: number[]) => string
|
||||||
): Map<number[], number[]> | Record<string, string> {
|
): Map<number[], number[]> | Record<string, string> {
|
||||||
const convert = toString || getEmojiSequenceString;
|
const convert = toString || getEmojiSequenceKeyword;
|
||||||
const results = Object.create(null) as Record<string, string>;
|
const results = Object.create(null) as Record<string, string>;
|
||||||
|
|
||||||
for (let i = 0; i < sequences.length; i++) {
|
for (let i = 0; i < sequences.length; i++) {
|
||||||
|
@ -6,7 +6,7 @@ import {
|
|||||||
} from '../cleanup';
|
} from '../cleanup';
|
||||||
import { convertEmojiSequenceToUTF32 } from '../convert';
|
import { convertEmojiSequenceToUTF32 } from '../convert';
|
||||||
import { keycapEmoji, vs16Emoji } from '../data';
|
import { keycapEmoji, vs16Emoji } from '../data';
|
||||||
import { getEmojiSequenceString } from '../format';
|
import { getEmojiSequenceKeyword } from '../format';
|
||||||
import { EmojiTestDataItem, getQualifiedEmojiSequencesMap } from './parse';
|
import { EmojiTestDataItem, getQualifiedEmojiSequencesMap } from './parse';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,7 +59,7 @@ export function getQualifiedEmojiVariations(
|
|||||||
testData: (number[] | EmojiTestDataItem)[] = [],
|
testData: (number[] | EmojiTestDataItem)[] = [],
|
||||||
toString?: (value: number[]) => string
|
toString?: (value: number[]) => string
|
||||||
): number[][] | string[] {
|
): number[][] | string[] {
|
||||||
const convert = toString || getEmojiSequenceString;
|
const convert = toString || getEmojiSequenceKeyword;
|
||||||
const testSequences = testData.map((item) =>
|
const testSequences = testData.map((item) =>
|
||||||
item instanceof Array ? item : item.sequence
|
item instanceof Array ? item : item.sequence
|
||||||
);
|
);
|
||||||
|
@ -100,7 +100,11 @@ export {
|
|||||||
convertEmojiSequenceToUTF16,
|
convertEmojiSequenceToUTF16,
|
||||||
convertEmojiSequenceToUTF32,
|
convertEmojiSequenceToUTF32,
|
||||||
} from './emoji/convert';
|
} from './emoji/convert';
|
||||||
export { getEmojiUnicodeString, getEmojiSequenceString } from './emoji/format';
|
export {
|
||||||
|
getEmojiUnicodeString,
|
||||||
|
getEmojiSequenceString,
|
||||||
|
getEmojiSequenceKeyword,
|
||||||
|
} from './emoji/format';
|
||||||
export {
|
export {
|
||||||
parseEmojiTestFile,
|
parseEmojiTestFile,
|
||||||
getQualifiedEmojiSequencesMap,
|
getQualifiedEmojiSequencesMap,
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
import { getEmojiSequenceString } from '../lib/emoji/format';
|
import {
|
||||||
|
getEmojiSequenceString,
|
||||||
|
getEmojiSequenceKeyword,
|
||||||
|
} from '../lib/emoji/format';
|
||||||
|
|
||||||
describe('Testing formatting emoji code points', () => {
|
describe('Testing formatting emoji code points', () => {
|
||||||
it('Empty sequence', () => {
|
it('Empty sequence', () => {
|
||||||
const sequence: number[] = [];
|
const sequence: number[] = [];
|
||||||
expect(getEmojiSequenceString(sequence)).toBe('');
|
expect(getEmojiSequenceString(sequence)).toBe('');
|
||||||
|
expect(getEmojiSequenceKeyword(sequence)).toBe('');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Keycap sequence', () => {
|
it('Keycap sequence', () => {
|
||||||
@ -15,6 +19,8 @@ describe('Testing formatting emoji code points', () => {
|
|||||||
add0: true,
|
add0: true,
|
||||||
})
|
})
|
||||||
).toBe('0023-fe0f-20e3');
|
).toBe('0023-fe0f-20e3');
|
||||||
|
|
||||||
|
expect(getEmojiSequenceKeyword(sequence)).toBe('23-fe0f-20e3');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('UTF-16 sequence', () => {
|
it('UTF-16 sequence', () => {
|
||||||
@ -26,6 +32,10 @@ describe('Testing formatting emoji code points', () => {
|
|||||||
expect(getEmojiSequenceString(sequence)).toBe(
|
expect(getEmojiSequenceString(sequence)).toBe(
|
||||||
'1f441-fe0f-200d-1f5e8-fe0f'
|
'1f441-fe0f-200d-1f5e8-fe0f'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
expect(getEmojiSequenceKeyword(sequence)).toBe(
|
||||||
|
'd83d-dc41-fe0f-200d-d83d-dde8-fe0f'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('UTF-32 sequence', () => {
|
it('UTF-32 sequence', () => {
|
||||||
@ -35,5 +45,9 @@ describe('Testing formatting emoji code points', () => {
|
|||||||
expect(getEmojiSequenceString(sequence)).toBe(
|
expect(getEmojiSequenceString(sequence)).toBe(
|
||||||
'1f441-fe0f-200d-1f5e8-fe0f'
|
'1f441-fe0f-200d-1f5e8-fe0f'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
expect(getEmojiSequenceKeyword(sequence)).toBe(
|
||||||
|
'1f441-fe0f-200d-1f5e8-fe0f'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user