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

chore(utils): remove unused functions

This commit is contained in:
Vjacheslav Trushkin 2022-12-22 11:15:03 +02:00
parent 6fb8700632
commit a8ce57b7a9
3 changed files with 10 additions and 109 deletions

View File

@ -26,12 +26,15 @@ export function getEmojiSequenceFromString(value: string): number[] {
* Example:
* [0x1FAF1, 0x1F3FB, 0x200D, 0x1FAF2, 0x1F3FC] => [[0x1FAF1, 0x1F3FB], [0x1FAF2, 0x1F3FC]]
*/
export function splitEmojiSequences(sequence: number[]): number[][] {
export function splitEmojiSequences(
sequence: number[],
separator = joinerEmoji
): number[][] {
const results: number[][] = [];
let queue: number[] = [];
for (let i = 0; i < sequence.length; i++) {
const code = sequence[i];
if (code === joinerEmoji) {
if (code === separator) {
results.push(queue);
queue = [];
} else {
@ -50,11 +53,14 @@ export function splitEmojiSequences(sequence: number[]): number[][] {
* Example:
* [[0x1FAF1, 0x1F3FB], [0x1FAF2, 0x1F3FC]] => [0x1FAF1, 0x1F3FB, 0x200D, 0x1FAF2, 0x1F3FC]
*/
export function joinEmojiSequences(sequences: number[][]): number[] {
export function joinEmojiSequences(
sequences: number[][],
separator = joinerEmoji
): number[] {
let results: number[] = [];
for (let i = 0; i < sequences.length; i++) {
if (i > 0) {
results.push(joinerEmoji);
results.push(separator);
}
results = results.concat(sequences[i]);
}
@ -68,81 +74,9 @@ export function removeEmojiVariations(sequence: number[]): number[] {
return sequence.filter((code) => code !== vs16Emoji);
}
/**
* Remove variations
*
* This function should be used with UTF-32 sequence, not UTF-16
*/
export function removeEmojiTones(sequence: number[]): number[] {
return sequence.filter((code) => {
for (const key in emojiComponents) {
const range = emojiComponents[key as keyof typeof emojiComponents];
if (code >= range[0] && code < range[1]) {
return false;
}
}
return true;
});
}
/**
* Get unqualified sequence
*/
export function getUnqualifiedEmojiSequence(sequence: number[]): number[] {
return sequence.filter((num) => num !== vs16Emoji);
}
/**
* Types for mapEmojiSequence()
*/
type MapCallback = (sequence: number[]) => number[];
interface MapOptions {
removeEmpty?: boolean;
removeDuplicates?: boolean;
}
const mapOptions: Required<MapOptions> = {
removeEmpty: true,
removeDuplicates: false,
};
/**
* Run function on sequences
*
* Intended to be used with functions such as `removeEmojiVariations` or `removeEmojiTones`
*/
export function mapEmojiSequences(
sequences: number[][],
callback: MapCallback,
options: MapOptions = {}
): number[][] {
const fullOptions = {
...mapOptions,
...options,
};
const values: Set<string> = new Set();
const results: number[][] = [];
sequences.forEach((sequence) => {
const result = callback(sequence);
// Check for empty sequences
if (fullOptions.removeEmpty && !result.length) {
return;
}
// Check for duplicate
if (fullOptions.removeDuplicates) {
const value = getEmojiSequenceKeyword(result);
if (values.has(value)) {
// duplicate
return;
}
values.add(value);
}
results.push(result);
});
return results;
}

View File

@ -88,8 +88,6 @@ export {
getEmojiSequenceFromString,
getUnqualifiedEmojiSequence,
removeEmojiVariations,
removeEmojiTones,
mapEmojiSequences,
} from './emoji/cleanup';
export {
getEmojiCodePoint,

View File

@ -2,8 +2,6 @@ import { convertEmojiSequenceToUTF32 } from '../lib/emoji/convert';
import {
getEmojiSequenceFromString,
joinEmojiSequences,
mapEmojiSequences,
removeEmojiTones,
removeEmojiVariations,
splitEmojiSequences,
} from '../lib/emoji/cleanup';
@ -42,14 +40,6 @@ describe('Testing formatting emoji cleanup', () => {
expect(removeEmojiVariations(sequence)).toEqual([
0x1f441, 0x200d, 0x1f5e8,
]);
expect(mapEmojiSequences(split, removeEmojiVariations)).toEqual([
[0x1f441],
[0x1f5e8],
]);
// Remove tones (does nothing for this sequence)
expect(removeEmojiTones(sequence)).toEqual(sequence);
expect(mapEmojiSequences(split, removeEmojiTones)).toEqual(split);
});
it('UTF-32 sequence with tones', () => {
@ -74,26 +64,5 @@ describe('Testing formatting emoji cleanup', () => {
// Remove variations (does nothing for this sequence)
expect(removeEmojiVariations(sequence)).toEqual(sequence);
expect(mapEmojiSequences(split, removeEmojiVariations)).toEqual(split);
// Remove tones
expect(removeEmojiTones(sequence)).toEqual([
0x1f9d1, 0x200d, 0x1f91d, 0x200d, 0x1f9d1,
]);
expect(mapEmojiSequences(split, removeEmojiTones)).toEqual([
[0x1f9d1],
[0x1f91d],
[0x1f9d1],
]);
// Hair tones (bad emoji, second chunk only has tone without emoji)
const sequence2 = getEmojiSequenceFromString('1F471 1F3FC-200D 1F3FF');
expect(sequence2).toEqual([0x1f471, 0x1f3fc, 0x200d, 0x1f3ff]);
const split2 = splitEmojiSequences(sequence2);
expect(removeEmojiTones(sequence2)).toEqual([0x1f471, 0x200d]);
expect(mapEmojiSequences(split2, removeEmojiTones)).toEqual([
[0x1f471],
]);
});
});