refactor: Simplify string filters to use stringify
This commit is contained in:
@@ -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? {
|
func capitalise(_ value: Any?) -> Any? {
|
||||||
if let value = toString(value) {
|
return stringify(value).capitalized
|
||||||
return value.capitalized
|
|
||||||
}
|
|
||||||
|
|
||||||
return value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func uppercase(_ value: Any?) -> Any? {
|
func uppercase(_ value: Any?) -> Any? {
|
||||||
if let value = toString(value) {
|
return stringify(value).uppercased()
|
||||||
return value.uppercased()
|
|
||||||
}
|
|
||||||
|
|
||||||
return value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func lowercase(_ value: Any?) -> Any? {
|
func lowercase(_ value: Any?) -> Any? {
|
||||||
if let value = toString(value) {
|
return stringify(value).lowercased()
|
||||||
return value.lowercased()
|
|
||||||
}
|
|
||||||
|
|
||||||
return value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultFilter(value: Any?, arguments: [Any?]) -> Any? {
|
func defaultFilter(value: Any?, arguments: [Any?]) -> Any? {
|
||||||
|
|||||||
@@ -70,15 +70,19 @@ public class VariableNode : NodeType {
|
|||||||
|
|
||||||
public func render(_ context: Context) throws -> String {
|
public func render(_ context: Context) throws -> String {
|
||||||
let result = try variable.resolve(context)
|
let result = try variable.resolve(context)
|
||||||
|
return stringify(result)
|
||||||
if let result = result as? String {
|
|
||||||
return result
|
|
||||||
} else if let result = result as? CustomStringConvertible {
|
|
||||||
return result.description
|
|
||||||
} else if let result = result as? NSObject {
|
|
||||||
return result.description
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func stringify(_ result: Any?) -> String {
|
||||||
|
if let result = result as? String {
|
||||||
|
return result
|
||||||
|
} else if let result = result as? CustomStringConvertible {
|
||||||
|
return result.description
|
||||||
|
} else if let result = result as? NSObject {
|
||||||
|
return result.description
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user