-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Drusy/master
Add dictionary addition operators
- Loading branch information
Showing
3 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |