mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-04 19:44:05 +00:00
Replace emoji detection implementation with a simpler one
This commit is contained in:
committed by
Stefan Ceriu
parent
f2e9c32e8d
commit
2019984918
@@ -16,51 +16,46 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
extension UnicodeScalar {
|
// Taken from https://gist.github.com/krummler/879e1ce942893db3104783d1d0e67b34
|
||||||
var isZeroWidthJoiner: Bool {
|
extension Character {
|
||||||
value == 8205
|
/// A simple emoji is one scalar and presented to the user as an Emoji
|
||||||
|
var isSimpleEmoji: Bool {
|
||||||
|
unicodeScalars.count == 1 && unicodeScalars.first?.properties.isEmojiPresentation ?? false
|
||||||
}
|
}
|
||||||
|
|
||||||
var isKeycap: Bool {
|
/// Checks if the scalars will be merged into and emoji
|
||||||
value == 8419
|
var isCombinedIntoEmoji: Bool {
|
||||||
|
unicodeScalars.count > 1 &&
|
||||||
|
unicodeScalars.contains { $0.properties.isJoinControl || $0.properties.isVariationSelector }
|
||||||
}
|
}
|
||||||
|
|
||||||
var isNumber: Bool {
|
var isEmoji: Bool {
|
||||||
switch value {
|
isSimpleEmoji || isCombinedIntoEmoji
|
||||||
case 48...57:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension String {
|
extension String {
|
||||||
|
var isSingleEmoji: Bool {
|
||||||
|
count == 1 && containsEmoji
|
||||||
|
}
|
||||||
|
|
||||||
|
var containsEmoji: Bool {
|
||||||
|
contains { $0.isEmoji }
|
||||||
|
}
|
||||||
|
|
||||||
var containsOnlyEmoji: Bool {
|
var containsOnlyEmoji: Bool {
|
||||||
guard !isEmpty else {
|
!isEmpty && !contains { !$0.isEmoji }
|
||||||
return false
|
}
|
||||||
}
|
|
||||||
|
var emojiString: String {
|
||||||
var emojiMarkerCount = 0
|
emojis.map { String($0) }.reduce("", +)
|
||||||
for scalar in unicodeScalars {
|
}
|
||||||
let isEmojiMarker = scalar.properties.isEmoji ||
|
|
||||||
scalar.properties.isEmojiPresentation ||
|
var emojis: [Character] {
|
||||||
scalar.isZeroWidthJoiner ||
|
filter(\.isEmoji)
|
||||||
scalar.properties.isDefaultIgnorableCodePoint ||
|
}
|
||||||
scalar.isKeycap
|
|
||||||
|
var emojiScalars: [UnicodeScalar] {
|
||||||
guard isEmojiMarker else {
|
filter(\.isEmoji).flatMap(\.unicodeScalars)
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
emojiMarkerCount += 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// Plain numbers like 0 return true for .isEmoji. We don't want that
|
|
||||||
let markersRequiringSiblings = unicodeScalars.filter {
|
|
||||||
$0.properties.isEmoji && $0.isNumber
|
|
||||||
}
|
|
||||||
|
|
||||||
return markersRequiringSiblings.count != emojiMarkerCount && emojiMarkerCount == unicodeScalars.count
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,9 +40,10 @@ class EmojiDetectionTests: XCTestCase {
|
|||||||
|
|
||||||
XCTAssertTrue("👍".containsOnlyEmoji)
|
XCTAssertTrue("👍".containsOnlyEmoji)
|
||||||
XCTAssertTrue("🫱🏼🫲🏾".containsOnlyEmoji)
|
XCTAssertTrue("🫱🏼🫲🏾".containsOnlyEmoji)
|
||||||
XCTAssertTrue("👁❤️🍝".containsOnlyEmoji)
|
|
||||||
XCTAssertFalse("🙂 ".containsOnlyEmoji)
|
XCTAssertFalse("🙂 ".containsOnlyEmoji)
|
||||||
XCTAssertFalse("Hello 👋".containsOnlyEmoji)
|
XCTAssertFalse("Hello 👋".containsOnlyEmoji)
|
||||||
XCTAssertFalse("Thanks".containsOnlyEmoji)
|
XCTAssertFalse("Thanks".containsOnlyEmoji)
|
||||||
|
|
||||||
|
XCTAssertFalse("0*".containsOnlyEmoji)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user