Added method to register boolean filters (#160)

* added method to register boolean filters

* parametrised negative filter name

* Update Extension.swift

* Update CHANGELOG.md

* renamed registerBooleanFilter to registerFilter

* updated docs
This commit is contained in:
Ilya Puchka
2018-09-25 23:29:21 +01:00
committed by GitHub
parent f7bda226e8
commit fce3dc5e48
4 changed files with 41 additions and 0 deletions

View File

@@ -19,6 +19,9 @@
- You can now use parentheses in boolean expressions to change operator precedence. - You can now use parentheses in boolean expressions to change operator precedence.
[Ilya Puchka](https://github.com/ilyapuchka) [Ilya Puchka](https://github.com/ilyapuchka)
[#165](https://github.com/stencilproject/Stencil/pull/165) [#165](https://github.com/stencilproject/Stencil/pull/165)
- Added method to add boolean filters with their negative counterparts.
[Ilya Puchka](https://github.com/ilyapuchka)
[#160](https://github.com/stencilproject/Stencil/pull/160)
### New Features ### New Features

View File

@@ -19,6 +19,15 @@ open class Extension {
}) })
} }
/// Registers boolean filter with it's negative counterpart
public func registerFilter(name: String, negativeFilterName: String, filter: @escaping (Any?) throws -> Bool?) {
filters[name] = .simple(filter)
filters[negativeFilterName] = .simple {
guard let result = try filter($0) else { return nil }
return !result
}
}
/// Registers a template filter with the given name /// Registers a template filter with the given name
public func registerFilter(_ name: String, filter: @escaping (Any?) throws -> Any?) { public func registerFilter(_ name: String, filter: @escaping (Any?) throws -> Any?) {
filters[name] = .simple(filter) filters[name] = .simple(filter)

View File

@@ -23,6 +23,24 @@ class FilterTests: XCTestCase {
try expect(result) == "Kyle Kyle" try expect(result) == "Kyle Kyle"
} }
$0.it("allows you to register boolean filters") {
let repeatExtension = Extension()
repeatExtension.registerFilter(name: "isPositive", negativeFilterName: "isNotPositive") { (value: Any?) in
if let value = value as? Int {
return value > 0
}
return nil
}
let result = try Template(templateString: "{{ value|isPositive }}")
.render(Context(dictionary: ["value": 1], environment: Environment(extensions: [repeatExtension])))
try expect(result) == "true"
let negativeResult = try Template(templateString: "{{ value|isNotPositive }}")
.render(Context(dictionary: ["value": -1], environment: Environment(extensions: [repeatExtension])))
try expect(negativeResult) == "true"
}
$0.it("allows you to register a custom filter which accepts single argument") { $0.it("allows you to register a custom filter which accepts single argument") {
let template = Template(templateString: """ let template = Template(templateString: """
{{ name|repeat:'value1, "value2"' }} {{ name|repeat:'value1, "value2"' }}

View File

@@ -48,6 +48,17 @@ Registering custom filters with arguments:
return value return value
} }
Registering custom boolean filters:
.. code-block:: swift
ext.registerFilter("ordinary", negativeFilterName: "odd") { (value: Any?) in
if let value = value as? Int {
return myInt % 2 == 0
}
return nil
}
Custom Tags Custom Tags
----------- -----------