Skip to content

Commit

Permalink
change filter to filterEach and added filter to apply dynamic filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka committed Mar 13, 2018
1 parent 94acc6e commit f41765d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
their stored properties.
- Allow default string filters to be applied to arrays
- Similar filters are suggested when unknown filter is used
- Added `indent`, `split`, `map`, `compact` and `filter` filters
- Added `indent`, `split`, `map`, `compact`, `filterEach` and `filter` filters

### Bug Fixes

Expand Down
1 change: 1 addition & 0 deletions Sources/Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class DefaultExtension: Extension {
registerFilter("indent", filter: indentFilter)
registerFilter("map", filter: mapFilter)
registerFilter("compact", filter: compactFilter)
registerFilter("filterEach", filter: filterEachFilter)
registerFilter("filter", filter: filterFilter)
}
}
Expand Down
19 changes: 17 additions & 2 deletions Sources/Filters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ func compactFilter(value: Any?, arguments: [Any?], context: Context) throws -> A
})
}

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

let attribute = stringify(arguments[0])
Expand All @@ -176,3 +176,18 @@ func filterFilter(value: Any?, arguments: [Any?], context: Context) throws -> An
return value
}

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

let attribute = stringify(arguments[0])
let parser = TokenParser(tokens: [], environment: context.environment)

let expr = try parser.compileFilter("$0|\(attribute)")
return try context.push(dictionary: ["$0": value]) {
try expr.resolve(context)
}
}

22 changes: 19 additions & 3 deletions Tests/StencilTests/FilterSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,24 +316,40 @@ func testFilter() {

}

describe("filter filter") {
describe("filterEach filter") {

$0.it("can filter using filter") {
let env = environmentWithFilter("isPositive") { (value) -> Any? in
if let number = toNumber(value: value as Any) { return number > 0 }
else { return nil }
}

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

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

}

describe("filter filter") {

$0.it("can apply dynamic filter") {
let template = Template(templateString: "{{ name|filter:somefilter }}")
let result = try template.render(Context(dictionary: ["name": "Jhon", "somefilter": "uppercase"]))
try expect(result) == "JHON"
}

$0.it("can apply dynamic filter on array") {
let template = Template(templateString: "{{ values|filter:joinfilter }}")
let result = try template.render(Context(dictionary: ["values": [1, 2, 3], "joinfilter": "join:\", \""]))
try expect(result) == "1, 2, 3"
}

}
}

0 comments on commit f41765d

Please sign in to comment.