Merge pull request #4 from 0xLeif/feature/flatten

Feature/flatten
This commit is contained in:
Zach Eriksen 2021-03-01 20:21:58 -06:00 committed by GitHub
commit f91b21ccfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 11 deletions

View File

@ -63,6 +63,36 @@ public extension Variable {
}
}
public extension Variable {
var flatten: Variable {
guard case .array(let value) = self else {
return self
}
guard !value.isEmpty else {
return .array([])
}
guard value.count > 1 else {
return value[0]
}
let flattenedArray = value.map(\.flatten)
var flatArray = [Variable]()
for variable in flattenedArray {
if case .array(let values) = variable {
flatArray.append(contentsOf: values.map(\.flatten))
} else {
flatArray.append(variable)
}
}
return .array(flatArray)
}
}
public extension Variable {
/// Update the Variable's Value
/// - Returns: A new Variable with the type of T
@ -153,7 +183,7 @@ extension Variable: ExpressibleByDictionaryLiteral {
}
}
extension AnyHashable {
public extension AnyHashable {
var variable: Variable {
if let variable = self as? Variable {
return variable