From da15dd27a24e038b4043e55b3ceff0ba866dab0a Mon Sep 17 00:00:00 2001 From: "YURII D." Date: Mon, 13 May 2024 23:17:16 +0300 Subject: [PATCH] Update index.ts --- src/index.ts | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3548ec3..ce8586d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,12 +16,14 @@ export type SkinTone = '' | 'none' | 'light' | 'mediumLight' | 'medium' | 'mediu * RGI Emoji Modifier Sequence. * * @param {string} emoji - The original emoji string. - * @param {SkinTone} [tone] - The skin tone to apply. If undefined, returns the original emoji. + * @param {SkinTone} tone - The skin tone to apply. If empty, returns the original emoji. * @returns {string} The emoji string with skin tones applied where applicable. */ export default function skinTone(emoji: string, tone?: SkinTone): string { - const skinToneMap: Record = { - '': '', + if (!tone) { + return emoji; + } + const skinTonMap = { none: '', light: '\u{1F3FB}', mediumLight: '\u{1F3FC}', @@ -30,29 +32,22 @@ export default function skinTone(emoji: string, tone?: SkinTone): string { dark: '\u{1F3FF}', }; - // If no tone or invalid tone is provided, return the original emoji - if (!tone || !(tone in skinToneMap)) { - return emoji; - } + let zwj = '\u200D'; - const zwj = emoji.includes('\u200D\ud83E\udd1D\u200D') ? '\u200D\ud83E\udd1D\u200D' : '\u200D'; + // Hand Shake 🧑‍🤝‍🧑 + if (emoji.includes('\u200d\ud83e\udd1d\u200d')) { + zwj = '\u200d\ud83e\udd1d\u200d'; + } const parts = emoji.split(zwj); const modifiedParts = parts.map((part) => { - // Remove existing skin tone modifiers - const basePart = part.replace(/[\u{1F3FB}-\u{1F3FF}]/gu, ''); - - // If tone is 'none', return the base part without modifiers - if (tone === 'none') { - return basePart; - } + const basePart = part.replace(/\p{Emoji_Modifier}/gu, ''); - // Check if the base part is an Emoji Modifier Base if (/\p{Emoji_Modifier_Base}/u.test(basePart)) { - return basePart + skinToneMap[tone]; + return basePart.replace(/(\p{Extended_Pictographic}+)(\uFE0F?)/u, `$1${skinTonMap[tone]}`); } return part; }); return modifiedParts.join(zwj); -} +} \ No newline at end of file