Skip to content

Commit

Permalink
update 0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dejurin committed May 13, 2024
1 parent 8c6ffce commit 58da5e0
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ console.log(skinTone("πŸ§‘πŸΏβ€πŸ€β€πŸ§‘πŸΏ", "light")); // πŸ§‘πŸ»β€πŸ€
### Browser

```js
// https://unpkg.com/@qit.tools/skin-tone@0.5.3/dist/browser/latest.min.js
// https://unpkg.com/@qit.tools/skin-tone@0.6.0/dist/browser/latest.min.js

document.addEventListener("DOMContentLoaded", () => {
console.log(skinTone("πŸ§‘πŸ»β€πŸ€β€πŸ§‘πŸ»", "dark"));
Expand Down
6 changes: 3 additions & 3 deletions dist/browser/latest.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/browser/latest.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/esnext/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type SkinTone = '' | 'none' | 'light' | 'mediumLight' | 'medium' | 'mediu
*
* πŸͺ„ Qit.tools
* @name @qit.tools/skin-tone
* @version 0.5.3
* @version 0.6.0
* @license MIT
* @copyright Copyright (c) 2024 Qit.tools.
* @see https://github.com/Qit-tools/skin-tone
Expand All @@ -15,7 +15,7 @@ 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 empty, returns the original emoji.
* @param {SkinTone} [tone] - The skin tone to apply. If undefined, returns the original emoji.
* @returns {string} The emoji string with skin tones applied where applicable.
*/
export default function skinTone(emoji: string, tone?: SkinTone): string;
28 changes: 16 additions & 12 deletions dist/esnext/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/esnext/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@qit.tools/skin-tone",
"version": "0.5.3",
"version": "0.6.0",
"description": "Change emoji skin tones effortlessly. πŸ§›πŸ§›πŸ»πŸ§›πŸΌπŸ§›πŸ½πŸ§›πŸΎπŸ§›πŸΏ",
"types": "./dist/esnext/index.d.ts",
"exports": {
Expand Down
29 changes: 17 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ 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 empty, returns the original emoji.
* @param {SkinTone} [tone] - The skin tone to apply. If undefined, returns the original emoji.
* @returns {string} The emoji string with skin tones applied where applicable.
*/
export default function skinTone(emoji: string, tone?: SkinTone): string {
if (!tone) {
return emoji;
}
const skinTonMap = {
const skinToneMap: Record<SkinTone, string> = {
'': '',
none: '',
light: '\u{1F3FB}',
mediumLight: '\u{1F3FC}',
Expand All @@ -32,19 +30,26 @@ export default function skinTone(emoji: string, tone?: SkinTone): string {
dark: '\u{1F3FF}',
};

let zwj = '\u200D';

// Hand Shake πŸ§‘β€πŸ€β€πŸ§‘
if (emoji.includes('\u200d\ud83e\udd1d\u200d')) {
zwj = '\u200d\ud83e\udd1d\u200d';
// If no tone or invalid tone is provided, return the original emoji
if (!tone || !(tone in skinToneMap)) {
return emoji;
}

const zwj = emoji.includes('\u200D\ud83E\udd1D\u200D') ? '\u200D\ud83E\udd1D\u200D' : '\u200D';

const parts = emoji.split(zwj);
const modifiedParts = parts.map((part) => {
const basePart = part.replace(/\p{Emoji_Modifier}/gu, '');
// 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;
}

// Check if the base part is an Emoji Modifier Base
if (/\p{Emoji_Modifier_Base}/u.test(basePart)) {
return basePart.replace(/(\p{Extended_Pictographic}+)(\uFE0F?)/u, `$1${skinTonMap[tone]}`);
return basePart + skinToneMap[tone];
}
return part;
});
Expand Down
10 changes: 5 additions & 5 deletions tests/skin-tone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,31 @@ describe('skinTone tests for various emojis', () => {
});

emojis.forEach((item) => {
it(`Should apply light skin tone to ${item.light}`, () => {
it(`Should apply light skin tone: ${item.emoji} => ${item.light}`, () => {
expect(skinTone(item.emoji, 'light')).toBe(item.light);
});
});

emojis.forEach((item) => {
it(`Should apply medium light skin tone to ${item.mediumLight}`, () => {
it(`Should apply medium light skin tone: ${item.emoji} => ${item.mediumLight}`, () => {
expect(skinTone(item.emoji, 'mediumLight')).toBe(item.mediumLight);
});
});

emojis.forEach((item) => {
it(`Should apply medium light skin tone to ${item.medium}`, () => {
it(`Should apply medium light skin tone: ${item.emoji} => ${item.medium}`, () => {
expect(skinTone(item.emoji, 'medium')).toBe(item.medium);
});
});

emojis.forEach((item) => {
it(`Should apply medium dark skin tone to ${item.mediumDark}`, () => {
it(`Should apply medium dark skin tone: ${item.emoji} => ${item.mediumDark}`, () => {
expect(skinTone(item.emoji, 'mediumDark')).toBe(item.mediumDark);
});
});

emojis.forEach((item) => {
it(`Should apply dark skin tone to ${item.dark}`, () => {
it(`Should apply dark skin tone: ${item.emoji} => ${item.dark}`, () => {
expect(skinTone(item.emoji, 'dark')).toBe(item.dark);
});
});
Expand Down

0 comments on commit 58da5e0

Please sign in to comment.