Permalink
Browse files

Add merge, pluck, and omit extensions to Dictionary

  • Loading branch information...
tptee committed Sep 7, 2015
1 parent 42462b8 commit b7a1e9e24a230eb9e2c1f5f718a96497c70f6456
Showing with 99 additions and 4 deletions.
  1. +45 −3 Example/Tests/Tests.swift
  2. +1 −1 Oriole.podspec
  3. +53 −0 Pod/Classes/Oriole.swift
View
@@ -86,12 +86,54 @@ class OrioleSpec: QuickSpec {
"Test": 1,
"ing": 2
]
expect(dict.mapValues {
$0 * 2
}) == [
expect(dict.mapValues { $0 * 2 }) == [
"Test": 2,
"ing": 4
]
let anotherDict = [
"Doot": "doot",
"Mr.": "Skeltal"
]
expect(anotherDict.mapValues { $0 + " doot!"} ) == [
"Doot": "doot doot!",
"Mr.": "Skeltal doot!"
]
}
it("merges two dictionaries") {
let original = ["This": "is a test"]
let new = [
"No need": "for alarm",
"This": "is not a drill"
]
expect(original.merge(new)) == [
"This": "is not a drill",
"No need": "for alarm"
]
}
it("returns a new dictionary where each pair's key is in a whitelist") {
let dict = [
"I want": "this",
"I also want": "this",
"I don't want": "this"
]
expect(dict.pluck(["I want", "I also want"])) == [
"I want": "this",
"I also want": "this"
]
}
it("returns a new dictionary where each pair's key isn't in a blacklist") {
let dict = [
"I want": "this",
"I also want": "this",
"I don't want": "this"
]
expect(dict.omit(["I don't want"])) == [
"I want": "this",
"I also want": "this"
]
}
}
}
View
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Oriole"
s.version = "0.4.0"
s.version = "0.5.0"
s.summary = "Oriole is a set of protocol extensions that add useful helper methods to Swift collections."
s.description = <<-DESC
Oriole is a set of protocol extensions that add useful helper methods to Swift collections. Oriole resembles libraries like Dollar and ExSwift (which in turn take inspiration from Lodash), but with some ideological differences:
View
@@ -202,4 +202,57 @@ public extension Dictionary {
return result
}
}
/**
Merges another dictionary with the original dictionary. Right takes precedence.
:param: dict A dictionary to merge into this one.
:returns: A merged dictionary.
*/
public func merge(dict: Dictionary) -> Dictionary {
var original = self
for (key, value) in dict {
original.updateValue(value, forKey: key)
}
return original
}
/*
Returns a dictionary containing pairs whose keys are in a whitelist.
:param: whitelist An array of keys used to whitelist pairs.
:returns: A dictionary with the whitelisted pairs.
**/
public func pluck(whitelist: [Key]) -> Dictionary {
let pairs = self.filter { pairs in
let (key, _) = pairs
return whitelist.contains(key)
}
var plucked = [Key: Value]()
for (key, value) in pairs {
plucked.updateValue(value, forKey: key)
}
return plucked
}
/*
Returns a dictionary containing pairs whose keys aren't in a blacklist.
:param: whitelist An array of keys used to blacklist pairs.
:returns: A dictionary without the whitelisted pairs.
**/
public func omit(blacklist: [Key]) -> Dictionary {
let pairs = self.filter { pairs in
let (key, _) = pairs
return !blacklist.contains(key)
}
var filtered = [Key: Value]()
for (key, value) in pairs {
filtered.updateValue(value, forKey: key)
}
return filtered
}
}

0 comments on commit b7a1e9e

Please sign in to comment.