Skip to content

Commit

Permalink
added @available for macOS -> emoji extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangerhards committed Jun 18, 2022
1 parent 429f232 commit 6462a65
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Sources/SwiftPlus/Extensions/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import Foundation

public extension Character {
/// A simple emoji is one scalar and presented to the user as an Emoji
@available(macOS 10.12.2, *)
@available(iOS 10.2, *)
var isSimpleEmoji: Bool {
guard let firstScalar = unicodeScalars.first else { return false }
return firstScalar.properties.isEmoji && firstScalar.value > 0x238C
}

/// Checks if the scalars will be merged into an emoji
@available(macOS 10.12.2, *)
@available(iOS 10.2, *)
var isCombinedIntoEmoji: Bool { unicodeScalars.count > 1 && unicodeScalars.first?.properties.isEmoji ?? false }

@available(macOS 10.12.2, *)
@available(iOS 10.2, *)
var isEmoji: Bool { isSimpleEmoji || isCombinedIntoEmoji }
}
6 changes: 6 additions & 0 deletions Sources/SwiftPlus/Extensions/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,29 @@ public extension String {
}

/// Returns true if `String` is only a single emoji
@available(macOS 10.12.2, *)
@available(iOS 10.2, *)
var isSingleEmoji: Bool { count == 1 && containsEmoji }

/// Returns true if `String` contains at least one emoji
@available(macOS 10.12.2, *)
@available(iOS 10.2, *)
var containsEmoji: Bool { contains { $0.isEmoji } }

/// Returns true if `String` contains only emoji´s
@available(macOS 10.12.2, *)
@available(iOS 10.2, *)
var containsOnlyEmoji: Bool { !isEmpty && !contains { !$0.isEmoji } }

@available(macOS 10.12.2, *)
@available(iOS 10.2, *)
var emojiString: String { emojis.map { String($0) }.reduce("", +) }

@available(macOS 10.12.2, *)
@available(iOS 10.2, *)
var emojis: [Character] { filter { $0.isEmoji } }

@available(macOS 10.12.2, *)
@available(iOS 10.2, *)
var emojiScalars: [UnicodeScalar] { filter { $0.isEmoji }.flatMap { $0.unicodeScalars } }
}

0 comments on commit 6462a65

Please sign in to comment.