Skip to content

Commit

Permalink
renamed filterEach to select
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka committed Dec 8, 2019
1 parent 169aea8 commit a6e8373
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ _None_
, i.e. `myfilter = "uppercase"` and then use it to invoke this filter with `{{ string|filter:myfilter }}`.
[Ilya Puchka](https://github.com/ilyapuchka)
[#203](https://github.com/stencilproject/Stencil/pull/203)

- Added `map`, `compact` and `select` filters.
[Ilya Puchka](https://github.com/ilyapuchka)
[#189](https://github.com/stencilproject/Stencil/pull/189)

### Deprecations

Expand Down Expand Up @@ -126,7 +130,6 @@ _None_
[Ilya Puchka](https://github.com/ilyapuchka)
[#167](https://github.com/stencilproject/Stencil/pull/167)

- Added `map`, `compact` and `filterEach` filters

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion Sources/Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class DefaultExtension: Extension {
registerFilter("filter", filter: filterFilter)
registerFilter("map", filter: mapFilter)
registerFilter("compact", filter: compactFilter)
registerFilter("filterEach", filter: filterEachFilter)
registerFilter("select", filter: selectFilter)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Filters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ func compactFilter(value: Any?, arguments: [Any?], context: Context) throws -> A
}
}

func filterEachFilter(value: Any?, arguments: [Any?], context: Context) throws -> Any? {
func selectFilter(value: Any?, arguments: [Any?], context: Context) throws -> Any? {
guard arguments.count == 1 else {
throw TemplateSyntaxError("'filterEach' filter takes one argument")
throw TemplateSyntaxError("'select' filter takes one argument")
}

guard let token = Lexer(templateString: stringify(arguments[0])).tokenize().first else {
Expand Down
4 changes: 2 additions & 2 deletions Tests/StencilTests/EnvironmentSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ final class EnvironmentTests: XCTestCase {
}

it("reports syntax error on invalid filter expression") {
self.template = "{{ array|filterEach: $0|isPositive }}"
self.template = "{{ array|select: $0|isPositive }}"
let ext = Extension()
ext.registerFilter("isPositive") { (value) -> Any? in
if let number = toNumber(value: value as Any) { return number > 0 }
else { return nil }
}
self.environment = Environment(extensions: [ext])
try self.expectError(reason: "Can't parse filter expression", token: "array|filterEach: $0|isPositive")
try self.expectError(reason: "Can't parse filter expression", token: "array|select: $0|isPositive")
}
}

Expand Down
8 changes: 4 additions & 4 deletions Tests/StencilTests/FilterSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ final class FilterTests: XCTestCase {
}
}

func testFilterEachFilter() throws {
func testSelectFilter() throws {
it("can filter using filter") {
let ext = Extension()
ext.registerFilter("isPositive") { (value) -> Any? in
Expand All @@ -435,19 +435,19 @@ final class FilterTests: XCTestCase {
}
let env = Environment(extensions: [ext])

let template = Template(templateString: #"{{ array|filterEach:"$0|isPositive" }}"#)
let template = Template(templateString: #"{{ array|select:"$0|isPositive" }}"#)
let result = try template.render(Context(dictionary: ["array": [1, -1, 2, -2, 3, -3]], environment: env))
try expect(result) == "[1, 2, 3]"
}

it("can filter using boolean expression") {
let template = Template(templateString: #"{{ array|filterEach:"$0 > 0" }}"#)
let template = Template(templateString: #"{{ array|select:"$0 > 0" }}"#)
let result = try template.render(Context(dictionary: ["array": [1, -1, 2, -2, 3, -3]]))
try expect(result) == "[1, 2, 3]"
}

it("can filter using variable expression") {
let template = Template(templateString: "{{ array|filterEach:\"$0\" }}")
let template = Template(templateString: "{{ array|select:\"$0\" }}")
let result = try template.render(Context(dictionary: ["array": [1, -1, nil, 2, -2, 3, -3]]))
try expect(result) == "[1, 2, 3]"
}
Expand Down

0 comments on commit a6e8373

Please sign in to comment.