Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sources/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ extension Dictionary {
copy.removeValue(forKey: key)
return copy
}

/// returns a new directory by merging all values for respective keys.
public func merging(_ dictionary: Dictionary<Key, Value>) -> Dictionary<Key, Value> {
var copy = self
dictionary.forEach { copy.updateValue($1, forKey: $0) }
return copy
}
}
5 changes: 5 additions & 0 deletions Tests/ImmutableTests/DictinoaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class DictionaryTests: XCTestCase {
XCTAssertEqual(["a": 10].removingValue(forKey: "a"), [:])
}

func testMergingValues() {
XCTAssertEqual(["a": 10, "b": 20].merging(["b": 30, "c": 40]),
["a": 10, "b": 30, "c": 40])
}


static var allTests : [(String, (DictionaryTests) -> () throws -> Void)] {
return [
Expand Down