Skip to content

Commit 2ce4f0e

Browse files
authored
Merge pull request #4 from Drusy/master
Add dictionary addition operators
2 parents 89e89c5 + a3c8e1a commit 2ce4f0e

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

SwiftiumKit.xcodeproj/project.pbxproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
BAF01A811DB7F610006F81D3 /* UIImageExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAF01A801DB7F610006F81D3 /* UIImageExtensionsTests.swift */; };
2424
BAFA99E11DB7817600582104 /* UIFontExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAFA99E01DB7817600582104 /* UIFontExtensions.swift */; };
2525
BAFA99E31DB781D600582104 /* UIFontExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAFA99E21DB781D600582104 /* UIFontExtensionsTests.swift */; };
26+
F9E838D31DCCE15700F3C5FB /* DictionaryOperators.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9E838D21DCCE15700F3C5FB /* DictionaryOperators.swift */; };
27+
F9E838D61DCCE2D000F3C5FB /* DictionaryOperatorsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9E838D51DCCE2D000F3C5FB /* DictionaryOperatorsTests.swift */; };
2628
/* End PBXBuildFile section */
2729

2830
/* Begin PBXContainerItemProxy section */
@@ -59,6 +61,8 @@
5961
BAFA99E01DB7817600582104 /* UIFontExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIFontExtensions.swift; sourceTree = "<group>"; };
6062
BAFA99E21DB781D600582104 /* UIFontExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIFontExtensionsTests.swift; sourceTree = "<group>"; };
6163
BAFA99E41DB786EF00582104 /* CONTRIBUTING.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = CONTRIBUTING.md; sourceTree = "<group>"; };
64+
F9E838D21DCCE15700F3C5FB /* DictionaryOperators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DictionaryOperators.swift; sourceTree = "<group>"; };
65+
F9E838D51DCCE2D000F3C5FB /* DictionaryOperatorsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DictionaryOperatorsTests.swift; sourceTree = "<group>"; };
6266
/* End PBXFileReference section */
6367

6468
/* Begin PBXFrameworksBuildPhase section */
@@ -172,6 +176,7 @@
172176
children = (
173177
BA4D06731C91E023000DD654 /* C */,
174178
BA4D066D1C91C2F2000DD654 /* Additions */,
179+
F9E838D11DCCE14300F3C5FB /* Operators */,
175180
BA8961DC1C8EDBB100305CB0 /* Info.plist */,
176181
BA8961DA1C8EDBB100305CB0 /* SwiftiumKit.h */,
177182
);
@@ -182,6 +187,7 @@
182187
isa = PBXGroup;
183188
children = (
184189
BA4D066E1C91C2FE000DD654 /* Additions */,
190+
F9E838D41DCCE2A400F3C5FB /* Operators */,
185191
BA8961E81C8EDBB100305CB0 /* Info.plist */,
186192
);
187193
path = SwiftiumKitTests;
@@ -197,6 +203,22 @@
197203
name = Documentation;
198204
sourceTree = "<group>";
199205
};
206+
F9E838D11DCCE14300F3C5FB /* Operators */ = {
207+
isa = PBXGroup;
208+
children = (
209+
F9E838D21DCCE15700F3C5FB /* DictionaryOperators.swift */,
210+
);
211+
name = Operators;
212+
sourceTree = "<group>";
213+
};
214+
F9E838D41DCCE2A400F3C5FB /* Operators */ = {
215+
isa = PBXGroup;
216+
children = (
217+
F9E838D51DCCE2D000F3C5FB /* DictionaryOperatorsTests.swift */,
218+
);
219+
name = Operators;
220+
sourceTree = "<group>";
221+
};
200222
/* End PBXGroup section */
201223

202224
/* Begin PBXHeadersBuildPhase section */
@@ -310,6 +332,7 @@
310332
buildActionMask = 2147483647;
311333
files = (
312334
BA4D06691C91C2B5000DD654 /* StringExtensions.swift in Sources */,
335+
F9E838D31DCCE15700F3C5FB /* DictionaryOperators.swift in Sources */,
313336
BA8A2D4A1C96FAAD00125DC3 /* NSDataExtensions.swift in Sources */,
314337
BAF01A7F1DB7F59A006F81D3 /* UIImageExtensions.swift in Sources */,
315338
BAFA99E11DB7817600582104 /* UIFontExtensions.swift in Sources */,
@@ -327,6 +350,7 @@
327350
BA8A2D4C1C96FAC800125DC3 /* NSDataExtensionsTests.swift in Sources */,
328351
BA4D066B1C91C2DD000DD654 /* StringExtensionsTests.swift in Sources */,
329352
BAF01A811DB7F610006F81D3 /* UIImageExtensionsTests.swift in Sources */,
353+
F9E838D61DCCE2D000F3C5FB /* DictionaryOperatorsTests.swift in Sources */,
330354
BA8961F21C8EDBFC00305CB0 /* UIColorExtensionsTests.swift in Sources */,
331355
BAFA99E31DB781D600582104 /* UIFontExtensionsTests.swift in Sources */,
332356
);

SwiftiumKit/DictionaryOperators.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// Dictionary.swift
3+
// SwiftiumKit
4+
//
5+
// Created by Drusy on 04/11/2016.
6+
// Copyright © 2016 Openium. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/**
12+
Function to concat two dictionaries of the same types using assignment by sum operator.
13+
If the dictionaries contain common keys, the right values will me taken.
14+
15+
Usage example :
16+
````
17+
var left: [Int: String] = [ 1: "String 1" ]
18+
let right: [Int: String] = [ 2: "String 2" ]
19+
20+
left += right
21+
````
22+
23+
- Parameter left: a dictionary<T:V>
24+
- Parameter right: a dictionary<T:V>
25+
- Returns: a dictionary containing keys and values from left and right
26+
*/
27+
public func +=<K, V>(left: inout [K:V], right: [K:V]) {
28+
for (k, v) in right {
29+
left.updateValue(v, forKey: k)
30+
}
31+
}
32+
33+
/**
34+
Function to concat two dictionaries of the same types using addition operator.
35+
If the dictionaries contain common keys, the right values will me taken.
36+
37+
Usage example :
38+
````
39+
let left: [Int: String] = [ 1: "String 1" ]
40+
let right: [Int: String] = [ 2: "String 2" ]
41+
42+
let result = left + right
43+
````
44+
45+
- Parameter left: a dictionary<T:V>
46+
- Parameter right: a dictionary<T:V>
47+
- Returns: a dictionary containing keys and values from left and right
48+
*/
49+
public func +<K, V>(left: [K: V], right: [K: V]) -> Dictionary<K, V> {
50+
var map = Dictionary<K, V>()
51+
52+
for (k, v) in left {
53+
map[k] = v
54+
}
55+
for (k, v) in right {
56+
map[k] = v
57+
}
58+
59+
return map
60+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// DictionaryOperatorsTests.swift
3+
// SwiftiumKit
4+
//
5+
// Created by Drusy on 04/11/2016.
6+
// Copyright © 2016 Openium. All rights reserved.
7+
//
8+
9+
import XCTest
10+
11+
import SwiftiumKit
12+
13+
class DictionaryOperatorsTests: XCTestCase {
14+
15+
override func setUp() {
16+
super.setUp()
17+
}
18+
19+
override func tearDown() {
20+
super.tearDown()
21+
}
22+
23+
func testConcatDictionary_usingAssignmentBySum_shouldFillDictionary() {
24+
// Given
25+
var given: [Int: String] = [ 1: "String 1" ]
26+
let toAdd: [Int: String] = [ 2: "String 2" ]
27+
28+
// When
29+
given += toAdd
30+
31+
// Expect
32+
XCTAssertEqual(given, [
33+
1: "String 1",
34+
2: "String 2"
35+
]
36+
)
37+
}
38+
39+
func testConcatDictionary_usingAssignmentBySum_shouldReplaceDictionaryValues() {
40+
// Given
41+
var given: [Int: String] = [
42+
1: "String 1",
43+
2: "String 2"
44+
]
45+
let expected: [Int: String] = [
46+
1: "Remplacement 1",
47+
2: "Remplacement 2"
48+
]
49+
50+
// When
51+
given += expected
52+
53+
// Expect
54+
XCTAssertEqual(given, expected)
55+
}
56+
57+
func testConcatDictionary_usingAddition_shouldFillDictionary() {
58+
// Given
59+
let given: [Int: String] = [ 1: "String 1" ]
60+
let toAdd: [Int: String] = [ 2: "String 2" ]
61+
62+
// When
63+
let result = given + toAdd
64+
65+
// Expect
66+
XCTAssertEqual(result, [
67+
1: "String 1",
68+
2: "String 2"
69+
]
70+
)
71+
}
72+
73+
func testConcatDictionary_usingAddition_shouldReplaceDictionaryValues() {
74+
// Given
75+
let given: [Int: String] = [
76+
1: "String 1",
77+
2: "String 2"
78+
]
79+
let expected: [Int: String] = [
80+
1: "Remplacement 1",
81+
2: "Remplacement 2"
82+
]
83+
84+
// When
85+
let result = given + expected
86+
87+
// Expect
88+
XCTAssertEqual(result, expected)
89+
}
90+
}

0 commit comments

Comments
 (0)