Skip to content

Commit

Permalink
Update index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
dejurin committed May 13, 2024
1 parent 58da5e0 commit da15dd2
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SkinTone, string> = {
'': '',
if (!tone) {
return emoji;
}
const skinTonMap = {
none: '',
light: '\u{1F3FB}',
mediumLight: '\u{1F3FC}',
Expand All @@ -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);
}
}

0 comments on commit da15dd2

Please sign in to comment.