refactor: Simplify string filters to use stringify

This commit is contained in:
Kyle Fuller
2016-12-08 16:01:30 +00:00
parent 8f6b403aa9
commit fdde1dec02
2 changed files with 17 additions and 35 deletions

View File

@@ -1,35 +1,13 @@
func toString(_ value: Any?) -> String? {
if let value = value as? String {
return value
} else if let value = value as? CustomStringConvertible {
return value.description
}
return nil
}
func capitalise(_ value: Any?) -> Any? {
if let value = toString(value) {
return value.capitalized
}
return value
return stringify(value).capitalized
}
func uppercase(_ value: Any?) -> Any? {
if let value = toString(value) {
return value.uppercased()
}
return value
return stringify(value).uppercased()
}
func lowercase(_ value: Any?) -> Any? {
if let value = toString(value) {
return value.lowercased()
}
return value
return stringify(value).lowercased()
}
func defaultFilter(value: Any?, arguments: [Any?]) -> Any? {

View File

@@ -70,7 +70,12 @@ public class VariableNode : NodeType {
public func render(_ context: Context) throws -> String {
let result = try variable.resolve(context)
return stringify(result)
}
}
func stringify(_ result: Any?) -> String {
if let result = result as? String {
return result
} else if let result = result as? CustomStringConvertible {
@@ -80,5 +85,4 @@ public class VariableNode : NodeType {
}
return ""
}
}