missive / emoji-mart

๐Ÿช One component to pick them all
https://missiveapp.com/open/emoji-mart
MIT License
8.67k stars 835 forks source link

Get Emoji from its unicode #16

Closed iamsaksham closed 5 years ago

iamsaksham commented 8 years ago

is there any way by which we can get Emoji from its unicode?

EtienneLem commented 8 years ago

Not at the moment

goodmind commented 7 years ago

Is there any progress?

Fauntleroy commented 7 years ago

This is the temporary solution I added for my application:

import _ from 'lodash';
import { emojiIndex } from 'emoji-mart';

const { emojis } = emojiIndex;

export function getEmojiDataByNativeString (nativeString) {
  return _.find(emojis, { 'native': nativeString });
}

Not sure if this works in all cases yet. lodash isn't essential for this, I just use it elsewhere in my application.

stnwk commented 6 years ago

Example without lodash:

import { emojiIndex } from 'emoji-mart';

export function getEmojiDataByNativeString (nativeString) {
  return Object.values(emojiIndex.emojis).find(item => item.native === nativeString)
}
johnsenpeder commented 6 years ago

Won't work when the unicode has skin tones in it.

This should though:

import { emojiIndex } from 'emoji-mart';

export function getEmojiDataByNativeString(nativeString) {
  const skinTones = ['', '๐Ÿป', '๐Ÿผ', '๐Ÿฝ', '๐Ÿพ', '๐Ÿฟ'];

  let skin;
  let baseNativeString = nativeString;

  skinTones.forEach(skinTone => {
    baseNativeString = baseNativeString.replace(skinTone, '');
    if (nativeString.indexOf(skinTone) > 0) {
      skin = skinTones.indexOf(skinTone) + 1;
    }
  });

  const emojiData = Object.values(emojiIndex.emojis).find(item => item.native === baseNativeString);
  emojiData.skin = skin;

  return emojiData;
}
jmcrthrs commented 6 years ago

@johnsenpeder Very nice thanks.

However I have found a few instances where this doesn't work. They appear to be gender related:

person_with_ball man-bouncing-ball woman-bouncing-ball

You can have a play here to see the problem: https://codesandbox.io/s/oq8rkky5yq

johnsenpeder commented 6 years ago

@jmcrthrs yeah, seems to be gender related... :thinking: interesting..

ifiokjr commented 5 years ago

A quick fix would be to add the following since no other gender dependent emoji which I checked had the same problem. That being said I haven't investigated this fully.

import { emojiIndex } from 'emoji-mart';
import { BaseEmoji, CustomEmoji } from 'emoji-mart/dist-es/utils/emoji-index/nimble-emoji-index';

const isBaseEmoji = (emoji: BaseEmoji | CustomEmoji | undefined): emoji is BaseEmoji => {
  return Boolean(emoji && (emoji as BaseEmoji).native);
};

export function getEmojiDataByNativeString(nativeString: string) {
  const skinTones = ['', '๐Ÿป', '๐Ÿผ', '๐Ÿฝ', '๐Ÿพ', '๐Ÿฟ'];

  let skin = null;
  let baseNativeString = nativeString;

  skinTones.forEach(skinTone => {
    baseNativeString = baseNativeString.replace(skinTone, '');
    if (nativeString.indexOf(skinTone) > 0) {
      skin = skinTones.indexOf(skinTone) + 1;
    }
  });

  // Added to checks - no other gender dependent emoji which I checked had the same problem.
  if (baseNativeString === 'โ›นโ€โ™€๏ธ') {
    baseNativeString = 'โ›น๏ธโ€โ™€๏ธ';
  } else if (baseNativeString === 'โ›นโ€๏ธโ™‚๏ธ') {
    baseNativeString = 'โ›น๏ธโ€โ™‚๏ธ';
  }

  const emojiData = Object.values(emojiIndex.emojis)
    .filter(isBaseEmoji)
    .find(item => item.native === baseNativeString);

  if (emojiData) {
    emojiData.skin = skin;
  }

  return emojiData;
}
johnsenpeder commented 5 years ago

@ifiokjr sadly it does happen with other ones too, :man-lifting-weights: is one

TranquilMarmot commented 3 years ago

For anybody else landing here from a Google search and wondering why this issue is closed, emoji-mart now has a getEmojiDataFromNative function ๐Ÿ˜„

https://github.com/missive/emoji-mart/blob/master/README.md#get-emoji-data-from-native