Files
XcodeGen/Sources/ProjectSpec/Dictionary+Extension.swift
T
Maciej Piotrowski 4d6e63e880 Adds uncluttering dumped YAML manifest from nil entries. (#858)
* Adds uncluttering dumped YAML manifest from `nil` entries.

* Uses required type of a dictionary

* Update CHANGELOG.md

Co-authored-by: Maciej Piotrowski <maciej.piotrowski@allegro.pl>
2020-05-11 20:51:01 +10:00

34 lines
1.2 KiB
Swift

extension Dictionary where Key == String, Value == Any? {
func removingEmptyArraysDictionariesAndNils() -> [String: Any] {
var new: [String: Any] = [:]
filter(outNil).forEach { pair in
let value: Any
if let array = pair.value as? [[String: Any?]] {
value = array.removingEmptyArraysDictionariesAndNils()
} else if let dictionary = pair.value as? [String: Any?] {
value = dictionary.removingEmptyArraysDictionariesAndNils()
} else {
value = pair.value! // nil is filtered out :)
}
new[pair.key] = value
}
return new
.filter(outEmptyArrays)
.filter(outEmptyDictionaries)
}
func outEmptyArrays(_ pair: (key: String, value: Any)) -> Bool {
guard let array = pair.value as? [Any] else { return true }
return !array.isEmpty
}
func outEmptyDictionaries(_ pair: (key: String, value: Any)) -> Bool {
guard let dictionary = pair.value as? [String: Any] else { return true }
return !dictionary.isEmpty
}
func outNil(_ pair: (key: String, value: Any?)) -> Bool {
return pair.value != nil
}
}