Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Size of the single emoji in chat along with updated Emoji Regex #5039

Merged
merged 18 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 11 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.

38 changes: 38 additions & 0 deletions src/libs/Emoji/getEmojiUnicode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint radix: ["error", "as-needed"] */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are we silencing here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseInt also accepts a second param radix, which I've marked as-needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just add the radix?


/**
* getEmojiUnicode
* Get the unicode code of an emoji in base 16.
*
* @name emojiUnicode
* @function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can make these docs more consistent with the others in our app. Notably, @name and @function + the repeated name before the description. We won't find these anywhere so I think better to not use them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay noted.

* @param {String} input The emoji character.
* @returns {String} The base 16 unicode code.
*/
export default function getEmojiUnicode(input) {
if (input.length === 0) {
return '';
}

if (input.length === 1) {
return input.charCodeAt(0).toString().split(' ').map(val => parseInt(val).toString(16))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use _.map()

.join(' ');
}

const pairs = [];
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is a surrogate?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the emojis are stored as two Unicodes. Hence we call each of the Unicode as high surrogate and low surrogate

https://thekevinscott.com/emojis-in-javascript/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it thanks. I'm not really familiar with this language - could we maybe add a comment that briefly explains what we mean when we refer to a "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 pairs.map(val => parseInt(val).toString(16)).join(' ');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_.map()

}
8 changes: 8 additions & 0 deletions src/libs/Emoji/trimEmojiUnicode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Function to remove Skin Tone and utf16 surrogates from Emoji
* @param {String} emojiCode
* @returns {String}
*/
export default function trimEmojiUnicode(emojiCode) {
return emojiCode.replace(/(fe0f|1f3fb|1f3fc|1f3fd|1f3fe|1f3ff)$/, '').trim();
}
6 changes: 5 additions & 1 deletion src/libs/ValidationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import moment from 'moment';
import CONST from '../CONST';
import {showBankAccountFormValidationError, showBankAccountErrorModal} from './actions/BankAccounts';
import {translateLocal} from './translate';
import getEmojiUnicode from './Emoji/getEmojiUnicode';
import trimEmojiUnicode from './Emoji/trimEmojiUnicode';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably these could be combined into a single file called EmojiUtils.js or something? We don't enforce one file per method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay noted


/**
* Validating that this is a valid address (PO boxes are not allowed)
Expand Down Expand Up @@ -31,7 +33,9 @@ function isSingleEmoji(message) {
}

const matchedEmoji = match[0];
return message.length === matchedEmoji.length;
const matchedUnicode = getEmojiUnicode(matchedEmoji);
const currentMessageUnicode = trimEmojiUnicode(getEmojiUnicode(message));
return matchedUnicode === currentMessageUnicode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure when isSingleEmoji() was added - but it doesn't belongs in this file. ValidationUtils is for validating user input into a form field not to check if something is an emoji. Let's please move it somewhere else.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay should I move it to EmojiUtils, that I'll create for the above functions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works for me! Thanks!

}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/EmojiRegexTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import _ from 'underscore';
import Emoji from '../../assets/emojis';
import CONST from '../../src/CONST';
import {isSingleEmoji} from '../../src/libs/ValidationUtils';

describe('EmojiRegexTest', () => {
it('matches all the emojis in the list', () => {
const emojiMatched = _.every(Emoji, (emoji) => {
if (emoji.header === true || emoji.code === CONST.EMOJI_SPACER) {
return true;
}
const isEmojiMatched = isSingleEmoji(emoji.code);
let skinToneMatched = true;
if (emoji.types) {
skinToneMatched = _.every(emoji.types, emojiWithSkinTone => isSingleEmoji(emojiWithSkinTone));
}
return skinToneMatched && isEmojiMatched;
});

expect(emojiMatched).toBe(true);
});

it('matches single emojis variants for size', () => {
expect(isSingleEmoji('👨‍👩‍👦️')).toBe(true);
expect(isSingleEmoji('👉')).toBe(true);
expect(isSingleEmoji('👊🏾')).toBe(true);
expect(isSingleEmoji('😪️')).toBe(true);
expect(isSingleEmoji('❤️')).toBe(true);
expect(isSingleEmoji('👨‍👩‍👦️')).toBe(true);
expect(isSingleEmoji('😎️')).toBe(true);
expect(isSingleEmoji('🔫️')).toBe(true);
expect(isSingleEmoji('🛍')).toBe(true);
expect(isSingleEmoji('⁉️')).toBe(true);
expect(isSingleEmoji('*️⃣')).toBe(true);
expect(isSingleEmoji('✳️')).toBe(true);
expect(isSingleEmoji('👶🏽')).toBe(true);
expect(isSingleEmoji('🕍')).toBe(true);
expect(isSingleEmoji('🇺🇲')).toBe(true);
expect(isSingleEmoji('🇮🇳')).toBe(true);
expect(isSingleEmoji('🇺🇦️')).toBe(true);
marcaaron marked this conversation as resolved.
Show resolved Hide resolved
});
});