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

Check if emojis are invalid #23

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions Source/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public enum StickersError: Error {
case dataTypeMismatch
case setIsEmpty
case emojiIsEmpty
case emojisInvalid
case telegramNotInstalled
}
8 changes: 8 additions & 0 deletions Source/StickerSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,18 @@ public class StickerSet {
if emojis.isEmpty {
throw StickersError.emojiIsEmpty
}
if !arrayContainsEmoji(array: emojis) {
throw StickersError.emojisInvalid
}
if try self.validateData(data) {
self.stickers.append(Sticker(data: data, emojis: emojis))
}
}

/// Returns true if an array contains at least 1 valid emoji
func arrayContainsEmoji(array: [String]) -> Bool {
return array.contains { $0.containsEmoji }
}

/**
Sets a thumbnail of the sticker set using the provided data.
Expand Down
11 changes: 11 additions & 0 deletions Source/StringExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extension String {
/// Checks if a string is a valid emoji
var containsEmoji: Bool {
for scalar in self.unicodeScalars {
if scalar.properties.isEmoji && (scalar.value > 0x238C) { // Exclude non-graphical symbols
return true
}
}
return false
}
}