Skip to content

Commit 793b67d

Browse files
committed
add operators: + and +=
1 parent 7db709c commit 793b67d

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

Sources/Lookup/Lookup.swift

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
11
import Foundation
22

3-
// MARK: - extension Array Helper merging multi lookup into one lookup
4-
public extension Array where Element == Lookup {
5-
6-
/// Merging multi `rawDict` into one
7-
/// ⚠️ Only support `Dictionary`
8-
///
9-
/// - Parameter uniquingKeysWith: uniquing keys with conflict
10-
/// - Returns: Merged `Lookup`
11-
func merging(uniquingKeysWith: (Any, Any) -> Any) -> Lookup {
12-
let dictLookups = self.compactMap({ $0.dict })
13-
var temp: [String: Any] = [:]
14-
for value in dictLookups {
15-
temp.merge(value, uniquingKeysWith: uniquingKeysWith)
16-
}
17-
return Lookup(temp)
18-
}
19-
}
20-
213
extension Array {
224
var countIndex: Int {
235
count - 1
@@ -34,7 +16,7 @@ fileprivate extension String {
3416
}
3517

3618
// MARK: Unwrap
37-
fileprivate func unwrap(_ object: Any) -> Any {
19+
fileprivate func unwrap(_ object: Any?) -> Any {
3820
switch object {
3921
case let lookup as Lookup:
4022
return unwrap(lookup.object)
@@ -58,7 +40,7 @@ fileprivate func unwrap(_ object: Any) -> Any {
5840
return array.map(unwrap)
5941

6042
default:
61-
return object
43+
return object ?? NSNull()
6244
}
6345
}
6446

@@ -286,6 +268,17 @@ public struct Lookup: Swift.CustomStringConvertible, Swift.CustomDebugStringConv
286268
public var debugDescription: String { description }
287269

288270
fileprivate static var null: Lookup { Lookup(NSNull()) }
271+
272+
fileprivate mutating func merge(other: Lookup) {
273+
switch (self.rawType, other.rawType) {
274+
case (.dict, .dict):
275+
self.rawDict.merge(other.rawDict, uniquingKeysWith: { $1 })
276+
case (.array, .array):
277+
self.rawArray += other.rawArray
278+
default:
279+
break
280+
}
281+
}
289282
}
290283

291284
extension Lookup: ExpressibleByArrayLiteral {
@@ -529,6 +522,24 @@ public extension Lookup {
529522
}
530523
}
531524

525+
526+
// MARK: - Operator
527+
public func + (lhs: Lookup, rhs: Lookup) -> Lookup {
528+
switch (lhs.rawType, rhs.rawType) {
529+
case (.dict, .dict):
530+
let lhsRawDict = lhs.rawDict
531+
return Lookup(lhsRawDict.merging(rhs.rawDict, uniquingKeysWith: { $1 }))
532+
case (.array, .array):
533+
return Lookup(lhs.rawArray + rhs.rawArray)
534+
default:
535+
return .null
536+
}
537+
}
538+
539+
public func += (lhs: inout Lookup, rhs: Lookup) {
540+
lhs.merge(other: rhs)
541+
}
542+
532543
// MARK: - Codable
533544
extension Lookup: Codable {
534545

Tests/LookupTests/LookupTests.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,16 @@ final class LookupTests: QuickSpec {
188188
it("merge dictionary lookups") {
189189
let lookup1 = Lookup(["name": "Lookup", "age": 3])
190190
let lookup2 = Lookup(["age": 1])
191-
let merged = [lookup1, lookup2].merging(uniquingKeysWith: { $1 })
191+
let merged = lookup1 + lookup2
192192
expect(merged.name.string) == "Lookup"
193193
expect(merged.age.int) == 1
194+
195+
var lookup3 = Lookup(["name": "Lookup", "brief": "A data handle tools."])
196+
let lookup4 = Lookup(["age": 1])
197+
lookup3 += lookup4
198+
expect(lookup3.age.int) == 1
199+
expect(lookup3.name.string) == "Lookup"
200+
expect(lookup3.brief.string) == "A data handle tools."
194201
}
195202

196203
// MARK: NOT SUPOORT NOW

0 commit comments

Comments
 (0)