feat(filters): Allow filters with arguments

This commit is contained in:
Kyle Fuller
2016-11-27 01:59:57 +00:00
parent 1e3afc0dd5
commit 60b378d482
6 changed files with 95 additions and 10 deletions

View File

@@ -1,3 +1,26 @@
public protocol FilterType {
func invoke(value: Any?, arguments: [Any?]) throws -> Any?
}
enum Filter: FilterType {
case simple(((Any?) throws -> Any?))
case arguments(((Any?, [Any?]) throws -> Any?))
func invoke(value: Any?, arguments: [Any?]) throws -> Any? {
switch self {
case let .simple(filter):
if !arguments.isEmpty {
throw TemplateSyntaxError("cannot invoke filter with an argument")
}
return try filter(value)
case let .arguments(filter):
return try filter(value, arguments)
}
}
}
open class Namespace {
public typealias TagParser = (TokenParser, Token) throws -> NodeType
@@ -40,7 +63,12 @@ open class Namespace {
}
/// Registers a template filter with the given name
open func registerFilter(_ name: String, filter: @escaping Filter) {
filters[name] = filter
open func registerFilter(_ name: String, filter: @escaping (Any?) throws -> Any?) {
filters[name] = .simple(filter)
}
/// Registers a template filter with the given name
open func registerFilter(_ name: String, filter: @escaping (Any?, [Any?]) throws -> Any?) {
filters[name] = .arguments(filter)
}
}