Skip to content

Commit

Permalink
Added emoji String & Character extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangerhards committed Jun 9, 2022
1 parent 04ab66e commit 429f232
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
24 changes: 24 additions & 0 deletions Sources/SwiftPlus/Extensions/Character.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// File.swift
//
//
// Created by Julian Gerhards on 09.06.22.
//

import Foundation

public extension Character {
/// A simple emoji is one scalar and presented to the user as an Emoji
@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(iOS 10.2, *)
var isCombinedIntoEmoji: Bool { unicodeScalars.count > 1 && unicodeScalars.first?.properties.isEmoji ?? false }

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

return mentions
}

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

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

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

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

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

@available(iOS 10.2, *)
var emojiScalars: [UnicodeScalar] { filter { $0.isEmoji }.flatMap { $0.unicodeScalars } }
}
16 changes: 12 additions & 4 deletions Tests/SwiftPlusTests/SwiftPlusTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import XCTest
@testable import SwiftPlus

final class SwiftPlusTests: XCTestCase {

func test_double_extensions() throws {
let double: Double = 1.494

Expand All @@ -28,11 +29,11 @@ final class SwiftPlusTests: XCTestCase {
}

func test_cgfloat_extensions() throws {
let double: CGFloat = 1.494
let cgFloat: CGFloat = 1.494

XCTAssertEqual(double.round(to: 2), 1.49)
XCTAssertEqual(double.maxValue(1.4), 1.4)
XCTAssertEqual(double.minValue(4.0), 4.0)
XCTAssertEqual(cgFloat.round(to: 2), 1.49)
XCTAssertEqual(cgFloat.maxValue(1.4), 1.4)
XCTAssertEqual(cgFloat.minValue(4.0), 4.0)
}

func test_string_extensions() throws {
Expand All @@ -41,5 +42,12 @@ final class SwiftPlusTests: XCTestCase {
XCTAssertEqual(string.trim(), "Hallo, mein #Name ist @String. Ich bin ein Swift-Typ.")
XCTAssertEqual(string.getHashtags(), ["#Name": NSRange(location: 13, length: 5)])
XCTAssertEqual(string.getMentions(), ["@String": NSRange(location: 23, length: 7)])

if #available(iOS 10.2, *) {
XCTAssertEqual("☺️ Test".containsEmoji, true)
XCTAssertEqual("☺️ Test".containsOnlyEmoji, false)
XCTAssertEqual("☺️☺️".containsOnlyEmoji, true)
XCTAssertEqual("☺️".isSingleEmoji, true)
}
}
}

0 comments on commit 429f232

Please sign in to comment.