Skip to content
Snippets Groups Projects
Commit f538f821 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Add convenient getter & setter to Fact array

parent 6a4c64ee
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!71Fact improvements & helpers
...@@ -43,3 +43,17 @@ extension Array where Element == Fact { ...@@ -43,3 +43,17 @@ extension Array where Element == Fact {
return try JSONEncoder().encode(self) return try JSONEncoder().encode(self)
} }
} }
extension Array where Element == Fact {
public func get(_ type: FactType) -> Fact? {
first(where: { $0.type == type.rawValue })
}
public mutating func set(_ type: FactType, _ value: String?) {
removeAll(where: { $0.type == type.rawValue })
if let value = value {
append(Fact(fact: value, type: type.rawValue))
sort(by: { $0.type < $1.type })
}
}
}
...@@ -51,4 +51,49 @@ final class FactTests: XCTestCase { ...@@ -51,4 +51,49 @@ final class FactTests: XCTestCase {
XCTAssertNoDifference(encodedModels, jsonData) XCTAssertNoDifference(encodedModels, jsonData)
} }
func testArrayGetter() {
let facts = [
Fact(fact: "username", type: 0),
Fact(fact: "email", type: 1),
Fact(fact: "phone", type: 2),
Fact(fact: "other", type: 3),
]
XCTAssertNoDifference(
[
facts.get(.username),
facts.get(.email),
facts.get(.phone),
facts.get(.other(3)),
facts.get(.other(4)),
],
[
Fact(fact: "username", type: 0),
Fact(fact: "email", type: 1),
Fact(fact: "phone", type: 2),
Fact(fact: "other", type: 3),
nil
]
)
}
func testArraySetter() {
var facts: [Fact] = []
facts.set(.email, "email")
facts.set(.phone, "phone")
facts.set(.other(3), "other")
facts.set(.username, "username")
XCTAssertNoDifference(
facts,
[
Fact(fact: "username", type: 0),
Fact(fact: "email", type: 1),
Fact(fact: "phone", type: 2),
Fact(fact: "other", type: 3),
]
)
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment