Skip to content

Commit

Permalink
fix(default): Check for wrapped nil in filter (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka authored and kylef committed Dec 24, 2017
1 parent 9357df3 commit 1223efb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Bug Fixes

- Fixed rendering `{{ block.super }}` with several levels of inheritance
- Fixed checking dictionary values for nil in `default` filter


## 0.10.1
Expand Down
3 changes: 2 additions & 1 deletion Sources/Filters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ func lowercase(_ value: Any?) -> Any? {
}

func defaultFilter(value: Any?, arguments: [Any?]) -> Any? {
if let value = value {
// value can be optional wrapping nil, so this way we check for underlying value
if let value = value, String(describing: value) != "nil" {
return value
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/StencilTests/FilterSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ func testFilter() {
let result = try template.render(Context(dictionary: [:]))
try expect(result) == "Hello World"
}

$0.it("checks for underlying nil value correctly") {
let template = Template(templateString: "Hello {{ user.name|default:\"anonymous\" }}")
let nilName: String? = nil
let user: [String: Any?] = ["name": nilName]
let result = try template.render(Context(dictionary: ["user": user]))
try expect(result) == "Hello anonymous"
}
}

describe("join filter") {
Expand Down

0 comments on commit 1223efb

Please sign in to comment.