Applied code guidelines for consistent code.

This commit is contained in:
Christiano, Kevin 2019-03-21 14:18:57 -04:00
parent d7bf5d0882
commit 157f951021

View File

@ -8,52 +8,48 @@
import Foundation
public extension Dictionary {
/// Returns a Dictionary using the specified chain. An empty dictionary is returned if a dictionary for the chain does not exist
public func dictionaryWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [String : Any] {
public func dictionaryWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [String: Any] {
guard let dictionary = optionalDictionaryWithChainOfKeysOrIndexes(keysOrIndexes) else {
return [:]
}
guard let dictionary = optionalDictionaryWithChainOfKeysOrIndexes(keysOrIndexes) else { return [:] }
return dictionary
}
/// Returns an optional Dictionary using the specified chain. Returns nil if a dictionary for the chain does not exist
public func optionalDictionaryWithChainOfKeysOrIndexes (_ keysOrIndexes: [Any]) -> [String : Any]? {
public func optionalDictionaryWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [String: Any]? {
return objectChainOfKeysOrIndexes(keysOrIndexes) as? [String: Any]
}
public func optionalDictionaryForKey(_ key: String) -> [String : Any]? {
public func optionalDictionaryForKey(_ key: String) -> [String: Any]? {
return objectChainOfKeysOrIndexes([key]) as? [String: Any]
}
/// Returns a String using the specified chain. An empty string is returned if a string for the chain does not exist
public func stringWithChainOfKeysOrIndexes(_ keysOrIndexes:[Any]) -> String {
public func stringWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> String {
guard let string = objectChainOfKeysOrIndexes(keysOrIndexes) as? String else {
return ""
}
guard let string = objectChainOfKeysOrIndexes(keysOrIndexes) as? String else { return "" }
return string
}
public func stringOptionalWithChainOfKeysOrIndexes(_ keysOrIndexes:[Any]) -> String? {
public func stringOptionalWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> String? {
return objectChainOfKeysOrIndexes(keysOrIndexes) as? String
}
/// Returns an Array using the specified chain. Returns an empty array if an array for the chain does not exist
public func arrayForChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [Any] {
guard let array = objectChainOfKeysOrIndexes(keysOrIndexes) as? [Any] else {
return []
}
guard let array = objectChainOfKeysOrIndexes(keysOrIndexes) as? [Any] else { return [] }
return array
}
/// Returns a Dictionary after looking up the specified key. An empty dictionary is returned if a dictionary for the key does not exist
public func dictionaryForKey(_ key : String) -> [String : Any] {
public func dictionaryForKey(_ key: String) -> [String: Any] {
return dictionaryWithChainOfKeysOrIndexes([key])
}
@ -66,104 +62,87 @@ public extension Dictionary {
/// Returns a String after looking up the specified key. Nil will be returned if a string for the key does not exist
public func optionalStringForKey(_ key: String) -> String? {
return objectChainOfKeysOrIndexes([key]) as? String
}
/// Returns an Array after looking up the specified key. An empty array will be returned if an array for the key does not exist
public func arrayForKey(_ key : String) -> [Any] {
public func arrayForKey(_ key: String) -> [Any] {
return arrayForChainOfKeysOrIndexes([key])
}
/// Return a Bool after looking up the specified key. This will return false if the key does not exist
public func boolForKey(_ key : String) -> Bool {
public func boolForKey(_ key: String) -> Bool {
guard let bool = objectChainOfKeysOrIndexes([key]) as? Bool else {
return false
}
guard let bool = objectChainOfKeysOrIndexes([key]) as? Bool else { return false }
return bool
}
/// Return a Bool after looking up the specified key. This will return false if the key does not exist
public func optionalBoolForKey(_ key : String) -> Bool? {
public func optionalBoolForKey(_ key: String) -> Bool? {
guard let bool = objectChainOfKeysOrIndexes([key]) as? Bool else {
return nil
}
guard let bool = objectChainOfKeysOrIndexes([key]) as? Bool else { return nil }
return bool
}
public func lenientBoolForKey(_ key: String) -> Bool {
guard let key = key as? Key, let object = self[key] else {
return false
}
guard let key = key as? Key, let object = self[key] else { return false }
if let object = object as? NSNumber {
return object.boolValue
} else if let object = object as? NSString {
return object.boolValue
}
return false
}
public func boolForChainOfKeysOrIndexes(_ keysOrIndexes:[Any])-> Bool {
guard let bool = objectChainOfKeysOrIndexes(keysOrIndexes) as? Bool else {
return false
}
public func boolForChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> Bool {
guard let bool = objectChainOfKeysOrIndexes(keysOrIndexes) as? Bool else { return false }
return bool
}
/// Return a float from a string created by looking up the specified key. This will return 0.0 if the key does not exist
public func floatForKey(_ key: String) -> Float {
guard let floatValue = objectChainOfKeysOrIndexes([key]) as? Float else {
return 0.0
}
guard let floatValue = objectChainOfKeysOrIndexes([key]) as? Float else { return 0.0 }
return floatValue
}
public func floatFromStringForKey(_ key:String) -> Float {
public func floatFromStringForKey(_ key: String) -> Float {
let stringValue = stringForkey(key)
guard let floatValue = Float(stringValue) else {
return 0.0
}
guard let floatValue = Float(stringValue) else { return 0.0 }
return floatValue
}
public func int32ForKey(_ key:String) -> Int32 {
guard let intValue = objectChainOfKeysOrIndexes([key]) as? Int32 else {
return 0
}
public func int32ForKey(_ key: String) -> Int32 {
guard let intValue = objectChainOfKeysOrIndexes([key]) as? Int32 else { return 0 }
return intValue
}
private func objectChainOfKeysOrIndexes(_ keysOrIndexes:[Any]) -> Any? {
private func objectChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> Any? {
var previousObject : Any? = self
var previousObject: Any? = self
for keyOrIndex in keysOrIndexes {
if let nextDictionary = previousObject as? [String:Any],
let keyOrIndex = keyOrIndex as? String {
if let nextDictionary = previousObject as? [String: Any], let keyOrIndex = keyOrIndex as? String {
previousObject = nextDictionary[keyOrIndex]
continue
}
else if let nextArray = previousObject as? [Any],
let keyOrIndex = keyOrIndex as? Int {
} else if let nextArray = previousObject as? [Any], let keyOrIndex = keyOrIndex as? Int {
previousObject = nextArray[keyOrIndex]
continue
}
else {
} else {
previousObject = nil
break
}
@ -171,4 +150,5 @@ public extension Dictionary {
return previousObject
}
}