Code Monkey home page Code Monkey logo

Comments (9)

dm8tr avatar dm8tr commented on May 27, 2024 25

You can use parse.unemojify() which is to convert the emoji string info text format, then verify the text.

parser.unemojify('I ❤️ ☕'); // returns: 'I :heart: :coffee:'

Now, if all words are emojis then each should start with a : and end with a :; otherwise, it is not an emoji.

Maybe there is a better way to do this but following @petehouston comment I did something like this

import 'package:flutter_emoji/flutter_emoji.dart';

/*code*/

bool isAllEmoji(String text) {
  for (String s in EmojiParser().unemojify(text).split(" ")) if (!s.startsWith(":") || !s.endsWith(":")) return false;
  return true;
}

from flutter-emoji.

petehouston avatar petehouston commented on May 27, 2024 4

You can use parse.unemojify() which is to convert the emoji string info text format, then verify the text.

parser.unemojify('I ❤️ ☕'); // returns: 'I :heart: :coffee:'

Now, if all words are emojis then each should start with a : and end with a :; otherwise, it is not an emoji.

from flutter-emoji.

migalv avatar migalv commented on May 27, 2024 1

All of this works, unless you use the emojis that have color 👍🏻

UPDATE

This works:

final emojisRegExp =
    RegExp(r"(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])");

bool isOnlyEmojis(String text) {
    // find all emojis
    final emojis = emojisRegExp.allMatches(text);

    // return if none found
    if (emojis.isEmpty) return false;

    // remove all emojis from the this
    for (final emoji in emojis) {
      text = text.replaceAll(emoji.input.substring(emoji.start, emoji.end), "");
    }

    // remove all whitespace (optional)
    text = text.replaceAll(" ", "");

    // return true if nothing else left
    return text.isEmpty;
  }

  /// True if the string is only emojis and the number of emojis is less than [maxEmojis]
  bool isLessEmojisThan(String text, int maxEmojis) {
    final allEmojis = emojisRegExp.allMatches(text);
    final numEmojis = allEmojis.length;

    if (numEmojis < maxEmojis && isOnlyEmojis(text)) {
      return true;
    }

    return false;
  }

from flutter-emoji.

mana-ai avatar mana-ai commented on May 27, 2024

@petehouston What about text in the middle?

such as 😄😄hahah😄

from flutter-emoji.

mana-ai avatar mana-ai commented on May 27, 2024

@petehouston Wanna it contains continuesly pure emoji then mark it as true, otherwise treat as normal.

from flutter-emoji.

mitinKevadiya avatar mitinKevadiya commented on May 27, 2024

You can use parse.unemojify() which is to convert the emoji string info text format, then verify the text.

parser.unemojify('I ❤️ ☕'); // returns: 'I :heart: :coffee:'

Now, if all words are emojis then each should start with a : and end with a :; otherwise, it is not an emoji.

Maybe there is a better way to do this but following @petehouston comment I did something like this

import 'package:flutter_emoji/flutter_emoji.dart';

/*code*/

bool isAllEmoji(String text) {
  for (String s in EmojiParser().unemojify(text).split(" ")) if (!s.startsWith(":") || !s.endsWith(":")) return false;
  return true;
}

I think we have check one more condition for this

import 'package:flutter_emoji/flutter_emoji.dart';

/*code*/

bool isAllEmoji(String text) {
    for (String s in EmojiParser().unemojify(text).split(" ")) {
      if (!s.startsWith(":") || !s.endsWith(":")) return false;
    }
    List<String> parseEmoji = EmojiParser().parseEmojis(text);
    if(parseEmoji.isNotEmpty) {
      String text1 = text;
      for(String s in parseEmoji) {
        text1 = text1.replaceAll(s, "");
      }
      if(text1.isNotEmpty) {
        return false;
      }
    }
    return true;
  }

from flutter-emoji.

salaryazdjerdi avatar salaryazdjerdi commented on May 27, 2024

This is the correct solution.

  bool hasOnlyEmojis(String input) {
    // find all emojis
    final emojis = EmojiParser().parseEmojis(input);

    // return if none found
    if (emojis.isEmpty) return false;

    // remove all emojis from the input
    for (final emoji in emojis) {
      input = input.replaceAll(emoji, "");
    }

    // remove all whitespace (optional)
    input = input.replaceAll(" ", "");

    // return true if nothing else left
    return input.isEmpty;
  }

from flutter-emoji.

yalda-student avatar yalda-student commented on May 27, 2024

All of this works, unless you use the emojis that have color 👍🏻

UPDATE

This works:

final emojisRegExp =
    RegExp(r"(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])");

bool isOnlyEmojis(String text) {
    // find all emojis
    final emojis = emojisRegExp.allMatches(text);

    // return if none found
    if (emojis.isEmpty) return false;

    // remove all emojis from the this
    for (final emoji in emojis) {
      text = text.replaceAll(emoji.input.substring(emoji.start, emoji.end), "");
    }

    // remove all whitespace (optional)
    text = text.replaceAll(" ", "");

    // return true if nothing else left
    return text.isEmpty;
  }

  /// True if the string is only emojis and the number of emojis is less than [maxEmojis]
  bool isLessEmojisThan(String text, int maxEmojis) {
    final allEmojis = emojisRegExp.allMatches(text);
    final numEmojis = allEmojis.length;

    if (numEmojis < maxEmojis && isOnlyEmojis(text)) {
      return true;
    }

    return false;
  }

your code helps me. TNX

from flutter-emoji.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.