-
Notifications
You must be signed in to change notification settings - Fork 13
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 #16 from AckeeCZ/th/newFeatures
Th/new features
- Loading branch information
Showing
8 changed files
with
181 additions
and
6 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,83 @@ | ||
// | ||
// FoundationExtensions.swift | ||
// Pods | ||
// | ||
// Created by Tomas Holka on 29/06/2017. | ||
// | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
public protocol OptionalProtocol { | ||
associatedtype WrappedValue | ||
var optional: WrappedValue? {get} | ||
} | ||
|
||
extension Optional: OptionalProtocol { | ||
public typealias WrappedValue = Wrapped | ||
public var optional: WrappedValue? { return self } | ||
} | ||
|
||
|
||
extension Dictionary where Value: OptionalProtocol { | ||
|
||
|
||
// Removes nils from dictionary | ||
public var nilsRemoved: [Key: Value.WrappedValue] { | ||
return self.reduce([:]) { acc, element -> [Key: Value.WrappedValue] in | ||
|
||
if let value = element.value.optional { | ||
var result = acc | ||
result[element.key] = value | ||
return result | ||
} else { | ||
return acc | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
/** | ||
Simulation of classic valueForKeyPath method. | ||
|
||
- Parameter keyPath: Dot separated key path | ||
*/ | ||
public func value<T>(for keyPath: String) -> T? { | ||
let components = keyPath.components(separatedBy: ".") | ||
|
||
var result: Any? = self | ||
components.forEach { key in | ||
guard let key = key as? Key else { | ||
return | ||
} | ||
result = (result as? Dictionary<Key, Value>)?[key] | ||
} | ||
return result as? T | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
extension Optional where Wrapped == String { | ||
public var isEmpty: Bool { | ||
switch self { | ||
case .none: | ||
return true | ||
case .some(let value): | ||
return value.isEmpty | ||
} | ||
} | ||
} | ||
|
||
|
||
extension NumberFormatter { | ||
|
||
public func string(from number: Int) -> String? { | ||
return self.string(from: NSNumber(value: number)) | ||
} | ||
|
||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,64 @@ | ||
// | ||
// FoundationTests.swift | ||
// ACKategories | ||
// | ||
// Created by Tomas Holka on 29/06/2017. | ||
// Copyright © 2017 CocoaPods. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Quick | ||
import Nimble | ||
import ACKategories | ||
|
||
class FoundationSpec: QuickSpec { | ||
override func spec() { | ||
describe("Dictionary") { | ||
|
||
it("counts correctly") { | ||
let dict: [String:Any?] = ["key":1, "key2":"value2", "key3":nil] | ||
let dictWithoutNils = dict.nilsRemoved | ||
|
||
expect(dict.count) == 3 | ||
expect(dictWithoutNils.count) == 2 | ||
} | ||
|
||
|
||
it("gets correct value") { | ||
let dict: [String:Any?] = ["key":1, "key2":["key3":3.17]] | ||
let value1: Int = dict.value(for: "key")! | ||
let value2: Double = dict.value(for: "key2.key3")! | ||
|
||
expect(value1) == 1 | ||
expect(value2) == 3.17 | ||
} | ||
} | ||
|
||
|
||
describe("Optional") { | ||
|
||
it("checks if string is empty") { | ||
|
||
let string1: String? = "test" | ||
let string2: String? = nil | ||
let string3: String? = "" | ||
|
||
expect(string1.isEmpty) == false | ||
expect(string2.isEmpty) == true | ||
expect(string3.isEmpty) == true | ||
} | ||
} | ||
|
||
|
||
describe("NumberFormatter") { | ||
|
||
it("converts int to string") { | ||
let numberFormatter = NumberFormatter() | ||
let integer = 10 | ||
let numberString = numberFormatter.string(from: integer) | ||
|
||
expect(numberString) == "10" | ||
} | ||
} | ||
} | ||
} |
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