Sort returned entities

This commit is contained in:
Maurice Parker 2020-04-19 19:27:37 -05:00
parent bb61b15265
commit 45f56f01e3
3 changed files with 24 additions and 9 deletions

View File

@ -14,18 +14,19 @@ protocol TwitterEntity {
} }
extension TwitterEntity { extension TwitterEntity {
var startIndex: Int? {
if indices?.count ?? 0 > 0 { var startIndex: Int {
return indices?[0] if let indices = indices, indices.count > 0 {
return indices[0]
} }
return nil return 0
} }
var endIndex: Int? { var endIndex: Int {
if indices?.count ?? 0 > 1 { if let indices = indices, indices.count > 1 {
return indices?[1] return indices[1]
} }
return nil return 0
} }
} }
@ -58,7 +59,7 @@ struct TwitterEntities: Codable {
if let symbols = symbols { if let symbols = symbols {
entities.append(contentsOf: symbols) entities.append(contentsOf: symbols)
} }
return entities return entities.sorted(by: { $0.startIndex < $1.startIndex })
} }
} }

View File

@ -18,6 +18,13 @@ struct TwitterHashtag: Codable, TwitterEntity {
case indices = "indices" case indices = "indices"
} }
var startIndex: Int {
if let indices = indices, indices.count > 0 {
return indices[0] - 1
}
return 0
}
func renderAsHTML() -> String { func renderAsHTML() -> String {
return "" return ""
} }

View File

@ -18,6 +18,13 @@ struct TwitterSymbol: Codable, TwitterEntity {
case indices = "indices" case indices = "indices"
} }
var startIndex: Int {
if let indices = indices, indices.count > 0 {
return indices[0] - 1
}
return 0
}
func renderAsHTML() -> String { func renderAsHTML() -> String {
return "" return ""
} }