Skip to content

Commit

Permalink
Merge pull request #4 from Drusy/master
Browse files Browse the repository at this point in the history
Add dictionary addition operators
  • Loading branch information
kenji21 authored Nov 7, 2016
2 parents 89e89c5 + a3c8e1a commit 2ce4f0e
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
24 changes: 24 additions & 0 deletions SwiftiumKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
BAF01A811DB7F610006F81D3 /* UIImageExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAF01A801DB7F610006F81D3 /* UIImageExtensionsTests.swift */; };
BAFA99E11DB7817600582104 /* UIFontExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAFA99E01DB7817600582104 /* UIFontExtensions.swift */; };
BAFA99E31DB781D600582104 /* UIFontExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAFA99E21DB781D600582104 /* UIFontExtensionsTests.swift */; };
F9E838D31DCCE15700F3C5FB /* DictionaryOperators.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9E838D21DCCE15700F3C5FB /* DictionaryOperators.swift */; };
F9E838D61DCCE2D000F3C5FB /* DictionaryOperatorsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9E838D51DCCE2D000F3C5FB /* DictionaryOperatorsTests.swift */; };
/* End PBXBuildFile section */

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

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -172,6 +176,7 @@
children = (
BA4D06731C91E023000DD654 /* C */,
BA4D066D1C91C2F2000DD654 /* Additions */,
F9E838D11DCCE14300F3C5FB /* Operators */,
BA8961DC1C8EDBB100305CB0 /* Info.plist */,
BA8961DA1C8EDBB100305CB0 /* SwiftiumKit.h */,
);
Expand All @@ -182,6 +187,7 @@
isa = PBXGroup;
children = (
BA4D066E1C91C2FE000DD654 /* Additions */,
F9E838D41DCCE2A400F3C5FB /* Operators */,
BA8961E81C8EDBB100305CB0 /* Info.plist */,
);
path = SwiftiumKitTests;
Expand All @@ -197,6 +203,22 @@
name = Documentation;
sourceTree = "<group>";
};
F9E838D11DCCE14300F3C5FB /* Operators */ = {
isa = PBXGroup;
children = (
F9E838D21DCCE15700F3C5FB /* DictionaryOperators.swift */,
);
name = Operators;
sourceTree = "<group>";
};
F9E838D41DCCE2A400F3C5FB /* Operators */ = {
isa = PBXGroup;
children = (
F9E838D51DCCE2D000F3C5FB /* DictionaryOperatorsTests.swift */,
);
name = Operators;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -310,6 +332,7 @@
buildActionMask = 2147483647;
files = (
BA4D06691C91C2B5000DD654 /* StringExtensions.swift in Sources */,
F9E838D31DCCE15700F3C5FB /* DictionaryOperators.swift in Sources */,
BA8A2D4A1C96FAAD00125DC3 /* NSDataExtensions.swift in Sources */,
BAF01A7F1DB7F59A006F81D3 /* UIImageExtensions.swift in Sources */,
BAFA99E11DB7817600582104 /* UIFontExtensions.swift in Sources */,
Expand All @@ -327,6 +350,7 @@
BA8A2D4C1C96FAC800125DC3 /* NSDataExtensionsTests.swift in Sources */,
BA4D066B1C91C2DD000DD654 /* StringExtensionsTests.swift in Sources */,
BAF01A811DB7F610006F81D3 /* UIImageExtensionsTests.swift in Sources */,
F9E838D61DCCE2D000F3C5FB /* DictionaryOperatorsTests.swift in Sources */,
BA8961F21C8EDBFC00305CB0 /* UIColorExtensionsTests.swift in Sources */,
BAFA99E31DB781D600582104 /* UIFontExtensionsTests.swift in Sources */,
);
Expand Down
60 changes: 60 additions & 0 deletions SwiftiumKit/DictionaryOperators.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Dictionary.swift
// SwiftiumKit
//
// Created by Drusy on 04/11/2016.
// Copyright © 2016 Openium. All rights reserved.
//

import Foundation

/**
Function to concat two dictionaries of the same types using assignment by sum operator.
If the dictionaries contain common keys, the right values will me taken.

Usage example :
````
var left: [Int: String] = [ 1: "String 1" ]
let right: [Int: String] = [ 2: "String 2" ]

left += right
````

- Parameter left: a dictionary<T:V>
- Parameter right: a dictionary<T:V>
- Returns: a dictionary containing keys and values from left and right
*/
public func +=<K, V>(left: inout [K:V], right: [K:V]) {
for (k, v) in right {
left.updateValue(v, forKey: k)
}
}

/**
Function to concat two dictionaries of the same types using addition operator.
If the dictionaries contain common keys, the right values will me taken.

Usage example :
````
let left: [Int: String] = [ 1: "String 1" ]
let right: [Int: String] = [ 2: "String 2" ]

let result = left + right
````

- Parameter left: a dictionary<T:V>
- Parameter right: a dictionary<T:V>
- Returns: a dictionary containing keys and values from left and right
*/
public func +<K, V>(left: [K: V], right: [K: V]) -> Dictionary<K, V> {
var map = Dictionary<K, V>()

for (k, v) in left {
map[k] = v
}
for (k, v) in right {
map[k] = v
}

return map
}
90 changes: 90 additions & 0 deletions SwiftiumKitTests/DictionaryOperatorsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// DictionaryOperatorsTests.swift
// SwiftiumKit
//
// Created by Drusy on 04/11/2016.
// Copyright © 2016 Openium. All rights reserved.
//

import XCTest

import SwiftiumKit

class DictionaryOperatorsTests: XCTestCase {

override func setUp() {
super.setUp()
}

override func tearDown() {
super.tearDown()
}

func testConcatDictionary_usingAssignmentBySum_shouldFillDictionary() {
// Given
var given: [Int: String] = [ 1: "String 1" ]
let toAdd: [Int: String] = [ 2: "String 2" ]

// When
given += toAdd

// Expect
XCTAssertEqual(given, [
1: "String 1",
2: "String 2"
]
)
}

func testConcatDictionary_usingAssignmentBySum_shouldReplaceDictionaryValues() {
// Given
var given: [Int: String] = [
1: "String 1",
2: "String 2"
]
let expected: [Int: String] = [
1: "Remplacement 1",
2: "Remplacement 2"
]

// When
given += expected

// Expect
XCTAssertEqual(given, expected)
}

func testConcatDictionary_usingAddition_shouldFillDictionary() {
// Given
let given: [Int: String] = [ 1: "String 1" ]
let toAdd: [Int: String] = [ 2: "String 2" ]

// When
let result = given + toAdd

// Expect
XCTAssertEqual(result, [
1: "String 1",
2: "String 2"
]
)
}

func testConcatDictionary_usingAddition_shouldReplaceDictionaryValues() {
// Given
let given: [Int: String] = [
1: "String 1",
2: "String 2"
]
let expected: [Int: String] = [
1: "Remplacement 1",
2: "Remplacement 2"
]

// When
let result = given + expected

// Expect
XCTAssertEqual(result, expected)
}
}

0 comments on commit 2ce4f0e

Please sign in to comment.