Skip to content

Commit

Permalink
parametrised negative filter name
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka committed May 12, 2018
1 parent b9df27e commit 17ecb61
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- Added support for resolving superclass properties for not-NSObject subclasses
- The `{% for %}` tag can now iterate over tuples, structures and classes via
their stored properties.
- Added method to add boolean filters with their negative counterparts prefixed with `!`
- Added method to add boolean filters with their negative counterparts

### Bug Fixes

Expand Down
4 changes: 2 additions & 2 deletions Sources/Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ open class Extension {
}

/// Registers boolean filter with it's negative counterpart prefixed with `!`
public func registerBooleanFilter(_ name: String, filter: @escaping (Any?) throws -> Bool?) {
public func registerBooleanFilter(name: String, negativeFilterName: String, filter: @escaping (Any?) throws -> Bool?) {
filters[name] = .simple(filter)
filters["!\(name)"] = .simple {
filters[negativeFilterName] = .simple {
guard let result = try filter($0) else { return nil }
return !result
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/StencilTests/FilterSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func testFilter() {

$0.it("can register boolean filters") {
let repeatExtension = Extension()
repeatExtension.registerBooleanFilter("isPositive") { (value: Any?) in
repeatExtension.registerBooleanFilter(name: "isPositive", negativeFilterName: "isNegative") { (value: Any?) in
if let value = value as? Int {
return value > 0
}
Expand All @@ -196,7 +196,7 @@ func testFilter() {
.render(Context(dictionary: ["value": 1], environment: Environment(extensions: [repeatExtension])))
try expect(result) == "true"

let negativeResult = try Template(templateString: "{{ value|!isPositive }}")
let negativeResult = try Template(templateString: "{{ value|isNegative }}")
.render(Context(dictionary: ["value": -1], environment: Environment(extensions: [repeatExtension])))
try expect(negativeResult) == "true"
}
Expand Down

0 comments on commit 17ecb61

Please sign in to comment.