Helpers.swift 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import Foundation
  2. import CryptoKit
  3. func topicUrl(baseUrl: String, topic: String) -> String {
  4. return "\(baseUrl)/\(topic)"
  5. }
  6. func topicShortUrl(baseUrl: String, topic: String) -> String {
  7. return topicUrl(baseUrl: baseUrl, topic: topic)
  8. .replacingOccurrences(of: "http://", with: "")
  9. .replacingOccurrences(of: "https://", with: "")
  10. }
  11. func topicHash(baseUrl: String, topic: String) -> String {
  12. let data = Data(topicUrl(baseUrl: baseUrl, topic: topic).utf8)
  13. let digest = SHA256.hash(data: data)
  14. return digest.compactMap { String(format: "%02x", $0)}.joined()
  15. }
  16. func parseAllTags(_ tags: String?) -> [String] {
  17. return (tags?.components(separatedBy: ",") ?? [])
  18. .filter { !$0.trimmingCharacters(in: .whitespaces).isEmpty }
  19. }
  20. func parseEmojiTags(_ tags: String?) -> [String] {
  21. return parseEmojiTags(parseAllTags(tags))
  22. }
  23. func parseEmojiTags(_ tags: [String]?) -> [String] {
  24. guard let tags = tags else { return [] }
  25. var emojiTags: [String] = []
  26. for tag in tags {
  27. if let emoji = EmojiManager.shared.getEmojiByAlias(alias: tag) {
  28. emojiTags.append(emoji.getUnicode())
  29. }
  30. }
  31. return emojiTags
  32. }
  33. func parseNonEmojiTags(_ tags: String?) -> [String] {
  34. return parseAllTags(tags)
  35. .filter { EmojiManager.shared.getEmojiByAlias(alias: $0) == nil }
  36. }