├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── index.test-d.ts ├── index.d.ts ├── package.json ├── license ├── readme.md ├── test.js └── index.js /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType, expectError} from 'tsd'; 2 | import skinTone, {type SkinToneType} from './index.js'; 3 | 4 | const tone: SkinToneType = 'none'; 5 | 6 | expectType(skinTone('👍', 'none')); 7 | expectType(skinTone('👍', 'white')); 8 | expectType(skinTone('👍', 'creamWhite')); 9 | expectType(skinTone('👍', 'lightBrown')); 10 | expectType(skinTone('👍', 'brown')); 11 | expectType(skinTone('👍', 'darkBrown')); 12 | expectError(skinTone('👍', 'foo')); 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type SkinToneType = 2 | | 'none' 3 | | 'white' 4 | | 'creamWhite' 5 | | 'lightBrown' 6 | | 'brown' 7 | | 'darkBrown'; 8 | 9 | /** 10 | Change the skin tone of an emoji 👌👌🏻👌🏼👌🏽👌🏾👌🏿. 11 | 12 | @param emoji - Emoji to modify. 13 | @param tone - Skin tone to use for `emoji`. 14 | 15 | - `'none'` : *(Removes skin tone)* 16 | - `'white'` : 🏻 *(Fitzpatrick Type-1–2)* 17 | - `'creamWhite'` : 🏼 *(Fitzpatrick Type-3)* 18 | - `'lightBrown'` : 🏽 *(Fitzpatrick Type-4)* 19 | - `'brown'` : 🏾 *(Fitzpatrick Type-5)* 20 | - `'darkBrown'` : 🏿 *(Fitzpatrick Type-6)* 21 | 22 | @example 23 | ``` 24 | import skinTone from 'skin-tone'; 25 | 26 | skinTone('👍', 'brown'); 27 | //=> '👍🏾' 28 | 29 | skinTone('👍', 'white'); 30 | //=> '👍🏻' 31 | 32 | // can also remove skin tone 33 | skinTone('👍🏾', 'none'); 34 | //=> '👍' 35 | 36 | // just passes it through when not supported 37 | skinTone('🦄', 'darkBrown'); 38 | //=> '🦄' 39 | ``` 40 | */ 41 | export default function skinTone(emoji: string, type: SkinToneType): string; 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "skin-tone", 3 | "version": "4.0.0", 4 | "description": "Change the skin tone of an emoji 👌👌🏻👌🏼👌🏽👌🏾👌🏿", 5 | "license": "MIT", 6 | "repository": "sindresorhus/skin-tone", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsd" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "emoji", 31 | "emojis", 32 | "skin", 33 | "tone", 34 | "type", 35 | "unicode", 36 | "emoticon", 37 | "fitzpatrick", 38 | "scale", 39 | "modify", 40 | "change", 41 | "strip", 42 | "remove" 43 | ], 44 | "devDependencies": { 45 | "ava": "^6.0.1", 46 | "tsd": "^0.30.3", 47 | "xo": "^0.56.0" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # skin-tone 2 | 3 | > Change the skin tone of an emoji 👌👌🏻👌🏼👌🏽👌🏾👌🏿 4 | 5 | The [Fitzpatrick scale](https://en.wikipedia.org/wiki/Fitzpatrick_scale#Unicode) is used to specify skin tones for emoji characters which represent humans. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install skin-tone 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import skinTone from 'skin-tone'; 17 | 18 | skinTone('👍', 'brown'); 19 | //=> '👍🏾' 20 | 21 | skinTone('👍', 'white'); 22 | //=> '👍🏻' 23 | 24 | // Can also remove skin tone 25 | skinTone('👍🏾', 'none'); 26 | //=> '👍' 27 | 28 | // Just passes it through when not supported 29 | skinTone('🦄', 'darkBrown'); 30 | //=> '🦄' 31 | ``` 32 | 33 | ## API 34 | 35 | ### skinTone(emoji, type) 36 | 37 | #### emoji 38 | 39 | Type: `string` 40 | 41 | Emoji to modify. 42 | 43 | #### type 44 | 45 | Type: `'none' | 'white' | 'creamWhite' | 'lightBrown' | 'brown' | 'darkBrown'` 46 | 47 | Skin tone to use for `emoji`. 48 | 49 | - `'none'` : *(Removes skin tone)* 50 | - `'white'` : 🏻 *(Fitzpatrick Type-1–2)* 51 | - `'creamWhite'` : 🏼 *(Fitzpatrick Type-3)* 52 | - `'lightBrown'` : 🏽 *(Fitzpatrick Type-4)* 53 | - `'brown'` : 🏾 *(Fitzpatrick Type-5)* 54 | - `'darkBrown'` : 🏿 *(Fitzpatrick Type-6)* 55 | 56 | > [!NOTE] 57 | > `'none'` removes only skin tone modifiers. Presentation selectors (like `\uFE0F`) are not restored, because the original selector state is lost once a skin tone is applied. Both forms (`✌` and `✌️`) are valid. 58 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import skinTone from './index.js'; 3 | 4 | test('main', t => { 5 | t.is(skinTone('👍', 'none'), '👍'); 6 | t.is(skinTone('👍', 'white'), '👍🏻'); 7 | t.is(skinTone('👍', 'creamWhite'), '👍🏼'); 8 | t.is(skinTone('👍', 'lightBrown'), '👍🏽'); 9 | t.is(skinTone('👍', 'brown'), '👍🏾'); 10 | t.is(skinTone('👍', 'darkBrown'), '👍🏿'); 11 | t.is(skinTone('👍🏿', 'none'), '👍'); 12 | t.is(skinTone('👸', 'lightBrown'), '👸🏽'); 13 | t.is(skinTone('🐶', 'darkBrown'), '🐶'); 14 | t.is(skinTone('👍🏿', 'white'), '👍🏻'); 15 | 16 | // Trickier emojis, where the presence of variation selector 17 | // or more than one person usually causes issues. 18 | t.is(skinTone('🕵️‍♀️', 'brown'), '🕵🏾‍♀'); 19 | t.is(skinTone('⛹️‍♀️', 'darkBrown'), '⛹🏿‍♀'); 20 | t.is(skinTone('👩‍❤️‍👨', 'brown'), '👩🏾‍❤‍👨🏾'); 21 | t.is(skinTone('👬', 'white'), '👬🏻'); 22 | 23 | // Family emojis don't support skin tone, so these shouldn't change. 24 | t.is(skinTone('👩‍👦', 'brown'), '👩‍👦'); 25 | t.is(skinTone('👩‍👩‍👧‍👧', 'white'), '👩‍👩‍👧‍👧'); 26 | }); 27 | 28 | test('removing skin tone', t => { 29 | // Should only remove skin tone modifiers, not presentation selectors 30 | t.is(skinTone('✌🏻', 'none'), '✌'); 31 | t.is(skinTone('🕵🏻', 'none'), '🕵'); 32 | t.is(skinTone('⛹🏿', 'none'), '⛹'); 33 | t.is(skinTone('👌🏻', 'none'), '👌'); 34 | t.is(skinTone('👍🏼', 'none'), '👍'); 35 | 36 | // Should preserve presentation selector if present 37 | t.is(skinTone('✌️', 'none'), '✌️'); 38 | t.is(skinTone('🕵️', 'none'), '🕵️'); 39 | }); 40 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const skinTones = new Map([ 2 | ['none', ''], 3 | ['white', '🏻'], 4 | ['creamWhite', '🏼'], 5 | ['lightBrown', '🏽'], 6 | ['brown', '🏾'], 7 | ['darkBrown', '🏿'], 8 | ]); 9 | 10 | // Emoji presentation selector takes the same place as skin tone modifier https://unicode.org/reports/tr51/#composing_zwj_seq 11 | // So it should be removed if present, otherwise, it causes issues with emojis with several traits. 12 | // For example `female-detective` turns into a detective with gender symbol next to it, instead of showing a female detective with set skin tone `🕵🏼️‍♀️`. 13 | const emojiPresentationSelector = '\u{FE0F}'; 14 | 15 | // Skin tones are not supported for family emojis. 16 | // Family emojis with 3+ person emojis can easily checked by the number of modifiable component emojis. 17 | // For two person family emojis it's needed to check directly 18 | // to distinguish them from other two person emojis: couple, handshake, fencing, etc. 19 | const twoFamilyEmojis = new Set(['👩‍👦', '👩‍👧', '👨‍👧', '👨‍👦']); 20 | 21 | export default function skinTone(emoji, tone) { 22 | if (!skinTones.has(tone)) { 23 | throw new TypeError(`Unexpected \`skinTone\` name: ${tone}`); 24 | } 25 | 26 | emoji = emoji.replaceAll(/[\u{1F3FB}-\u{1F3FF}]/ug, ''); 27 | 28 | if (tone === 'none') { 29 | return emoji; 30 | } 31 | 32 | // This emoji modifier base is present in emojis that the skin tone can apply to. 33 | const emojiBaseModifierRegex = /\p{Emoji_Modifier_Base}/ug; 34 | 35 | // If the emoji has more than two modifiable components, or it is a two-person family emoji, then skin tone should not be applied. 36 | if ((emoji).match(emojiBaseModifierRegex)?.length > 2 || twoFamilyEmojis.has(emoji)) { 37 | return emoji; 38 | } 39 | 40 | let processedEmoji = ''; 41 | 42 | for (const codePoint of emoji) { 43 | // If this code point is a emoji presentation selector, it should not be added to toned emoji. 44 | if (codePoint === emojiPresentationSelector) { 45 | continue; 46 | } 47 | 48 | processedEmoji += codePoint; 49 | // Tone should be applied to all modifiable components. 50 | // For example, both persons in couple emojis, etc. 51 | if (emojiBaseModifierRegex.test(codePoint)) { 52 | processedEmoji += skinTones.get(tone); 53 | } 54 | } 55 | 56 | return processedEmoji; 57 | } 58 | --------------------------------------------------------------------------------