Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
f0273ce
fix(emoji-size-in-chat): Moved regex to a different function
mananjadhav Sep 2, 2021
41d5ecf
fix(emoji-size-in-chat): Convert emoji to Unicode and compare
mananjadhav Sep 2, 2021
800e002
fix(emoji-size-in-chat): Replaced Emoji Regex
mananjadhav Sep 2, 2021
8fe203d
fix(emoji-size-in-chat): Handled skin-tone support
mananjadhav Sep 7, 2021
1da613a
fix(emoji-size-in-chat): wip: Added test for emoji regex
mananjadhav Sep 8, 2021
87a29b2
fix(emoji-size-in-chat): Update emoji regex test and pending emoji range
mananjadhav Sep 8, 2021
1f6354b
fix(emoji-size-in-chat): Added skintone emoji in test
mananjadhav Sep 8, 2021
c07557b
fix(emoji-size-in-chat): Hardcoded emoji variants to test against the…
mananjadhav Sep 8, 2021
cb3a584
fix(emoji-size-in-chat): Removed unused import
mananjadhav Sep 8, 2021
c6e1d9a
fix(emoji-size-in-chat): Replace function with inline regex
mananjadhav Sep 9, 2021
09a56b5
fix(emoji-size-in-chat): Merged function into one
mananjadhav Sep 9, 2021
73655c7
Merge branch 'main' of https://github.com/mananjadhav/App into fix/si…
mananjadhav Sep 10, 2021
031191e
fix(emoji-size-in-chat): Merged all Emoji functions into EmojiUtils
mananjadhav Sep 10, 2021
b3322b6
fix(emoji-size-in-chat): Added comments on surrogate and changed to _…
mananjadhav Sep 10, 2021
09babe0
fix(emoji-size-in-chat): Updated test documentation
mananjadhav Sep 10, 2021
5d75570
Merge branch 'main' of https://github.com/mananjadhav/App into fix/si…
mananjadhav Sep 10, 2021
22ca1d7
fix(emoji-size-in-chat): Added additional skull emoji from previous r…
mananjadhav Sep 10, 2021
52d9827
fix(emoji-size-in-chat): Added radix to parseInt
mananjadhav Sep 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/CONST.js

Large diffs are not rendered by default.

75 changes: 75 additions & 0 deletions src/libs/EmojiUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import _ from 'underscore';
import CONST from '../CONST';

/**
* Get the unicode code of an emoji in base 16.
* @param {String} input
* @returns {String}
*/
function getEmojiUnicode(input) {
if (input.length === 0) {
return '';
}

if (input.length === 1) {
return _.map(input.charCodeAt(0).toString().split(' '), val => parseInt(val, 10).toString(16)).join(' ');
}

const pairs = [];

// Some Emojis in UTF-16 are stored as pair of 2 Unicode characters (eg Flags)
// The first char is generally between the range U+D800 to U+DBFF called High surrogate
// & the second char between the range U+DC00 to U+DFFF called low surrogate
// More info in the following links:
// 1. https://docs.microsoft.com/en-us/windows/win32/intl/surrogates-and-supplementary-characters
// 2. https://thekevinscott.com/emojis-in-javascript/
for (let i = 0; i < input.length; i++) {
if (input.charCodeAt(i) >= 0xd800 && input.charCodeAt(i) <= 0xdbff) { // high surrogate
if (input.charCodeAt(i + 1) >= 0xdc00 && input.charCodeAt(i + 1) <= 0xdfff) { // low surrogate
pairs.push(
((input.charCodeAt(i) - 0xd800) * 0x400)
+ (input.charCodeAt(i + 1) - 0xdc00) + 0x10000,
);
}
} else if (input.charCodeAt(i) < 0xd800 || input.charCodeAt(i) > 0xdfff) {
// modifiers and joiners
pairs.push(input.charCodeAt(i));
}
}
return _.map(pairs, val => parseInt(val, 10).toString(16)).join(' ');
}

/**
* Function to remove Skin Tone and utf16 surrogates from Emoji
* @param {String} emojiCode
* @returns {String}
*/
function trimEmojiUnicode(emojiCode) {
return emojiCode.replace(/(fe0f|1f3fb|1f3fc|1f3fd|1f3fe|1f3ff)$/, '').trim();
}

/**
* Validates that this string is composed of a single emoji
*
* @param {String} message
* @returns {Boolean}
*/
function isSingleEmoji(message) {
const match = message.match(CONST.REGEX.EMOJIS);

if (!match) {
return false;
}

const matchedEmoji = match[0];
const matchedUnicode = getEmojiUnicode(matchedEmoji);
const currentMessageUnicode = trimEmojiUnicode(getEmojiUnicode(message));
return matchedUnicode === currentMessageUnicode;
}


export {
getEmojiUnicode,
trimEmojiUnicode,
isSingleEmoji,
};
17 changes: 0 additions & 17 deletions src/libs/ValidationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@ function isValidAddress(value) {
return !CONST.REGEX.PO_BOX.test(value);
}

/**
* Validates that this string is composed of a single emoji
*
* @param {String} message
* @returns {Boolean}
*/
function isSingleEmoji(message) {
const match = message.match(CONST.REGEX.EMOJIS);

if (!match) {
return false;
}

const matchedEmoji = match[0];
return message.length === matchedEmoji.length;
}

/**
* Validate date fields
Expand Down Expand Up @@ -127,5 +111,4 @@ export {
isValidIndustryCode,
isValidIdentity,
isValidZipCode,
isSingleEmoji,
};
2 changes: 1 addition & 1 deletion src/pages/home/report/ReportActionItemFragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import themeColors from '../../../styles/themes/default';
import RenderHTML from '../../../components/RenderHTML';
import Text from '../../../components/Text';
import Tooltip from '../../../components/Tooltip';
import {isSingleEmoji} from '../../../libs/ValidationUtils';
import {isSingleEmoji} from '../../../libs/EmojiUtils';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import canUseTouchScreen from '../../../libs/canUseTouchscreen';
Expand Down
64 changes: 64 additions & 0 deletions tests/unit/EmojiRegexTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import _ from 'underscore';
import Emoji from '../../assets/emojis';
import CONST from '../../src/CONST';
import {isSingleEmoji} from '../../src/libs/EmojiUtils';

describe('EmojiRegexTest', () => {
it('matches all the emojis in the list', () => {
// Given the set of Emojis available in the application
const emojiMatched = _.every(Emoji, (emoji) => {
if (emoji.header === true || emoji.code === CONST.EMOJI_SPACER) {
return true;
}

// When we match every Emoji Code
const isEmojiMatched = isSingleEmoji(emoji.code);
let skinToneMatched = true;
if (emoji.types) {
// and every skin tone variant of the Emoji code
skinToneMatched = _.every(emoji.types, emojiWithSkinTone => isSingleEmoji(emojiWithSkinTone));
}
return skinToneMatched && isEmojiMatched;
});

// Then it should return true for every Emoji Code
expect(emojiMatched).toBe(true);
});

it('matches single emojis variants for size', () => {
// GIVEN an emoji that has the default Unicode representation WHEN we check if it's a single emoji THEN it should return true
expect(isSingleEmoji('👉')).toBe(true);
expect(isSingleEmoji('😪️')).toBe(true);
expect(isSingleEmoji('😎️')).toBe(true);

// GIVEN an emoji that different cross-platform variations WHEN we check if it's a single emoji THEN it should return true
expect(isSingleEmoji('🔫️')).toBe(true);
expect(isSingleEmoji('🛍')).toBe(true);
expect(isSingleEmoji('🕍')).toBe(true);

// GIVEN an emoji that is symbol/numerical WHEN we check if it's a single emoji THEN it should return true
expect(isSingleEmoji('*️⃣')).toBe(true);
expect(isSingleEmoji('1️⃣️')).toBe(true);

// GIVEN an emoji that has text-variant WHEN we check if it's a single emoji THEN it should return true
expect(isSingleEmoji('❤️')).toBe(true);
expect(isSingleEmoji('⁉️')).toBe(true);
expect(isSingleEmoji('✳️')).toBe(true);
expect(isSingleEmoji('☠️')).toBe(true);


// GIVEN an emoji that has skin tone attached WHEN we check if it's a single emoji THEN it should return true
expect(isSingleEmoji('👶🏽')).toBe(true);
expect(isSingleEmoji('👩🏾')).toBe(true);
expect(isSingleEmoji('👊🏾')).toBe(true);

// GIVEN an emoji that is composite(family) with 4+ unicode pairs WHEN we check if it's a single emoji THEN it should return true
expect(isSingleEmoji('👨‍👩‍👦️')).toBe(true);
expect(isSingleEmoji('👩‍👩‍👧‍👦️')).toBe(true);

// GIVEN an emoji that has a length of 2 (flags) WHEN we check if it's a single emoji THEN it should return true
expect(isSingleEmoji('🇺🇲')).toBe(true);
expect(isSingleEmoji('🇮🇳')).toBe(true);
expect(isSingleEmoji('🇺🇦️')).toBe(true);
Comment thread
marcaaron marked this conversation as resolved.
});
});