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

Add .remove() and .replaceAll() functions #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
67 changes: 46 additions & 21 deletions lib/flutter_emoji.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,12 @@ class EmojiParser {
var _e = EmojiUtil.stripColons(m.group(0));
if (_e == null || m.group(0) == null) continue;
if (hasName(_e)) {
var pattern = RegExp.escape(m.group(0)!);
final pattern = RegExp.escape(m.group(0)!);
var formattedCode = get(_e).code;
if (fnFormat != null) {
formattedCode = fnFormat(formattedCode);
}
result =
result.replaceAll(RegExp(pattern, unicode: true), formattedCode);
result = result.replaceAll(RegExp(pattern, unicode: true), formattedCode);
}
}
return result;
Expand All @@ -234,24 +233,7 @@ class EmojiParser {
/// For example: 'I ❤️ Flutter' => 'I :heart: Flutter'
///
String unemojify(String text) {
if (text.isEmpty) return text;

final characters = Characters(text);
final buffer = StringBuffer();
for (final character in characters) {
if (hasEmoji(character)) {
var result = character;
result = result.replaceAll(
character,
getEmoji(character).full,
);

buffer.write(result);
} else {
buffer.write(character);
}
}
return buffer.toString();
return _mapEmojis(text, (final String emoji) => emoji.replaceAll(emoji, getEmoji(emoji).full));
}

///
Expand Down Expand Up @@ -306,6 +288,30 @@ class EmojiParser {
return buffer.toString();
}

///
/// Replace an emoji by a symbol.
///
/// For example: replace('I ❤️ coffee', '??') => 'I ?? coffee'
///
String replaceAll(final String text, final String toSymbol) {
if (toSymbol.isEmpty){
return removeEmojis(text);
}
return _mapEmojis(text, (String _) => toSymbol);
}

///
/// Remove all emojis from the input text, removing double spaces
///
/// For example: removeEmojis('I ❤️ Flutter just like ☕') => 'I Flutter just like '
String removeEmojis(final String text, {removeDoubleSpaces = true}) {
final result = _mapEmojis(text, (String _) => '');
if (removeDoubleSpaces) {
return result.replaceAll(RegExp(r'\s\s+'), ' ');
}
return result;
}

///
/// Return a list of emojis found in the input text
///
Expand All @@ -322,4 +328,23 @@ class EmojiParser {
}
return result;
}

///
/// When an emoji is found, call the transform function
///
String _mapEmojis(final String text, Function(String) transform) {
if (text.isEmpty) return "";

final characters = Characters(text);
final buffer = StringBuffer();
for (final character in characters) {
if (hasEmoji(character)) {
final result = character;
buffer.write(result.replaceAll(character, transform(character)));
} else {
buffer.write(character);
}
}
return buffer.toString();
}
}
12 changes: 12 additions & 0 deletions test/flutter_emoji_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ void main() {
expect(emojiParser.replace('I ❤️ coffee', '❤️', '❤️‍🔥'), 'I ❤️‍🔥 coffee');
});

test('replaceAll emojis', () {
expect(emojiParser.replaceAll('I ❤️ coffee', '??'), 'I ?? coffee');
expect(emojiParser.replaceAll('I ❤️ coffee', ''), 'I coffee');
expect(emojiParser.replaceAll('I ❤️ coffee', '❤️'), 'I ❤️ coffee');
});

test('remove emojis', () {
expect(emojiParser.removeEmojis('I ❤️🔥 coffee'), 'I coffee');
expect(emojiParser.removeEmojis('I ❤️ coffee'), 'I coffee');
expect(emojiParser.removeEmojis('I love coffee 🔥'), 'I love coffee ');
});

test('parse Emojis', () {
// var equalList = (var list1, var list2) {
// if (!(list1 is List && list2 is List) || list1.length != list2.length) {
Expand Down