Last active
August 9, 2022 17:39
-
-
Save fabiospampinato/f0f8b74bf4ba0222ad2729d233373d25 to your computer and use it in GitHub Desktop.
Little ugly function for setting the skin tone on an emoji
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* HELPERS */ | |
const SKIN_TONE = { | |
DARK: '\uD83C\uDFFF', | |
MEDIUM_DARK: '\uD83C\uDFFE', | |
MEDIUM: '\uD83C\uDFFD', | |
MEDIUM_LIGHT: '\uD83C\uDFFC', | |
LIGHT: '\uD83C\uDFFB', | |
NEUTRAL: '' | |
}; | |
const VARIATION = '\uFE0F'; | |
const ZWJ = '\u200D'; | |
/* MAIN */ | |
// This assumes a neutral emoji as input | |
const getEmojiWithSkinTone = ( emoji: string, skinTone: string, isEmojiSkinToneable: ( emoji: string ) => boolean ): string => { | |
if ( skinTone === SKIN_TONE.NEUTRAL ) return emoji; | |
const emojiWithSkinTone = emoji.split ( ZWJ ).map ( chunk => { | |
if ( !isEmojiSkinToneable ( chunk ) ) return chunk; | |
return `${chunk}${skinTone}`; | |
}).join ( ZWJ ); | |
const emojiWithoutVariation = emojiWithSkinTone.replaceAll ( `${VARIATION}${skinTone}`, `${skinTone}` ); | |
return emojiWithoutVariation; | |
}; | |
/* EXPORT */ | |
export default getEmojiWithSkinTone; | |
export {SKIN_TONE}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment