From b7a0044a70020bf97d9f1d604f126ab23db3dc93 Mon Sep 17 00:00:00 2001 From: Mike Greiling Date: Sat, 24 Jun 2017 01:41:49 -0500 Subject: [PATCH] move normalizeEmojiName method to emoji helper module --- app/assets/javascripts/awards_handler.js | 11 +++-------- app/assets/javascripts/emoji/index.js | 12 +++++++++++- app/assets/javascripts/emoji/is_emoji_name_valid.js | 11 ----------- 3 files changed, 14 insertions(+), 20 deletions(-) delete mode 100644 app/assets/javascripts/emoji/is_emoji_name_valid.js diff --git a/app/assets/javascripts/awards_handler.js b/app/assets/javascripts/awards_handler.js index 2bae4f4d955..27b61a0c61f 100644 --- a/app/assets/javascripts/awards_handler.js +++ b/app/assets/javascripts/awards_handler.js @@ -3,7 +3,7 @@ import Cookies from 'js-cookie'; import { glEmojiTag } from './behaviors/gl_emoji'; -import { emojiMap, emojiAliases, isEmojiNameValid } from './emoji'; +import { emojiMap, emojiAliases, isEmojiNameValid, normalizeEmojiName } from './emoji'; const animationEndEventString = 'animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd'; const transitionEndEventString = 'transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd'; @@ -65,7 +65,6 @@ function renderCategory(name, emojiList, opts = {}) { export default class AwardsHandler { constructor() { this.eventListeners = []; - this.aliases = emojiAliases; // If the user shows intent let's pre-build the menu this.registerEventListener('one', $(document), 'mouseenter focus', '.js-add-award', 'mouseenter focus', () => { const $menu = $('.emoji-menu'); @@ -260,7 +259,7 @@ export default class AwardsHandler { checkMutuality, callback, ) { - const normalizedEmoji = this.normalizeEmojiName(emoji); + const normalizedEmoji = normalizeEmojiName(emoji); const $emojiButton = this.findEmojiIcon(votesBlock, normalizedEmoji).parent(); this.postEmoji($emojiButton, awardUrl, normalizedEmoji, () => { this.addAwardToEmojiBar(votesBlock, normalizedEmoji, checkMutuality); @@ -279,7 +278,7 @@ export default class AwardsHandler { this.checkMutuality(votesBlock, emoji); } this.addEmojiToFrequentlyUsedList(emoji); - const normalizedEmoji = this.normalizeEmojiName(emoji); + const normalizedEmoji = normalizeEmojiName(emoji); const $emojiButton = this.findEmojiIcon(votesBlock, normalizedEmoji).parent(); if ($emojiButton.length > 0) { if (this.isActive($emojiButton)) { @@ -469,10 +468,6 @@ export default class AwardsHandler { return $('body, html').animate(options, 200); } - normalizeEmojiName(emoji) { - return Object.prototype.hasOwnProperty.call(this.aliases, emoji) ? this.aliases[emoji] : emoji; - } - addEmojiToFrequentlyUsedList(emoji) { if (isEmojiNameValid(emoji)) { this.frequentlyUsedEmojis = _.uniq(this.getFrequentlyUsedEmojis().concat(emoji)); diff --git a/app/assets/javascripts/emoji/index.js b/app/assets/javascripts/emoji/index.js index 1e86ce822d2..ad4cdfb4355 100644 --- a/app/assets/javascripts/emoji/index.js +++ b/app/assets/javascripts/emoji/index.js @@ -1,12 +1,22 @@ import emojiMap from 'emojis/digests.json'; import emojiAliases from 'emojis/aliases.json'; import getUnicodeSupportMap from './unicode_support_map'; -import isEmojiNameValid from './is_emoji_name_valid'; import isEmojiUnicodeSupported from './is_emoji_unicode_supported'; +const validEmojiNames = [...Object.keys(emojiMap), ...Object.keys(emojiAliases)]; + +function normalizeEmojiName(name) { + return Object.prototype.hasOwnProperty.call(emojiAliases, name) ? emojiAliases[name] : name; +} + +function isEmojiNameValid(name) { + return validEmojiNames.indexOf(name) >= 0; +} + export { emojiMap, emojiAliases, + normalizeEmojiName, getUnicodeSupportMap, isEmojiNameValid, isEmojiUnicodeSupported, diff --git a/app/assets/javascripts/emoji/is_emoji_name_valid.js b/app/assets/javascripts/emoji/is_emoji_name_valid.js deleted file mode 100644 index be4aeb32c46..00000000000 --- a/app/assets/javascripts/emoji/is_emoji_name_valid.js +++ /dev/null @@ -1,11 +0,0 @@ -import emojiMap from 'emojis/digests.json'; -import emojiAliases from 'emojis/aliases.json'; - -function isEmojiNameValid(inputName) { - const name = Object.prototype.hasOwnProperty.call(emojiAliases, inputName) ? - emojiAliases[inputName] : inputName; - - return name && emojiMap[name]; -} - -export default isEmojiNameValid;