Skip to content

Commit

Permalink
feat: apply string filters to arrays (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka authored and kylef committed Jan 22, 2018
1 parent c305974 commit c4a84a6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- The `{% for %}` tag can now iterate over tuples, structures and classes via
their stored properties.
- Added `split` filter
- Allow default string filters to be applied to arrays

### Bug Fixes

Expand Down
18 changes: 15 additions & 3 deletions Sources/Filters.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
func capitalise(_ value: Any?) -> Any? {
return stringify(value).capitalized
if let array = value as? [Any?] {
return array.map { stringify($0).capitalized }
} else {
return stringify(value).capitalized
}
}

func uppercase(_ value: Any?) -> Any? {
return stringify(value).uppercased()
if let array = value as? [Any?] {
return array.map { stringify($0).uppercased() }
} else {
return stringify(value).uppercased()
}
}

func lowercase(_ value: Any?) -> Any? {
return stringify(value).lowercased()
if let array = value as? [Any?] {
return array.map { stringify($0).lowercased() }
} else {
return stringify(value).lowercased()
}
}

func defaultFilter(value: Any?, arguments: [Any?]) -> Any? {
Expand Down
53 changes: 33 additions & 20 deletions Tests/StencilTests/FilterSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,32 +89,45 @@ func testFilter() {
}
}

describe("string filters") {
$0.context("given string") {
$0.it("transforms a string to be capitalized") {
let template = Template(templateString: "{{ name|capitalize }}")
let result = try template.render(Context(dictionary: ["name": "kyle"]))
try expect(result) == "Kyle"
}

describe("capitalize filter") {
let template = Template(templateString: "{{ name|capitalize }}")
$0.it("transforms a string to be uppercase") {
let template = Template(templateString: "{{ name|uppercase }}")
let result = try template.render(Context(dictionary: ["name": "kyle"]))
try expect(result) == "KYLE"
}

$0.it("capitalizes a string") {
let result = try template.render(Context(dictionary: ["name": "kyle"]))
try expect(result) == "Kyle"
$0.it("transforms a string to be lowercase") {
let template = Template(templateString: "{{ name|lowercase }}")
let result = try template.render(Context(dictionary: ["name": "Kyle"]))
try expect(result) == "kyle"
}
}
}


describe("uppercase filter") {
let template = Template(templateString: "{{ name|uppercase }}")

$0.it("transforms a string to be uppercase") {
let result = try template.render(Context(dictionary: ["name": "kyle"]))
try expect(result) == "KYLE"
}
}
$0.context("given array of strings") {
$0.it("transforms a string to be capitalized") {
let template = Template(templateString: "{{ names|capitalize }}")
let result = try template.render(Context(dictionary: ["names": ["kyle", "kyle"]]))
try expect(result) == "[\"Kyle\", \"Kyle\"]"
}

describe("lowercase filter") {
let template = Template(templateString: "{{ name|lowercase }}")
$0.it("transforms a string to be uppercase") {
let template = Template(templateString: "{{ names|uppercase }}")
let result = try template.render(Context(dictionary: ["names": ["kyle", "kyle"]]))
try expect(result) == "[\"KYLE\", \"KYLE\"]"
}

$0.it("transforms a string to be lowercase") {
let result = try template.render(Context(dictionary: ["name": "Kyle"]))
try expect(result) == "kyle"
$0.it("transforms a string to be lowercase") {
let template = Template(templateString: "{{ names|lowercase }}")
let result = try template.render(Context(dictionary: ["names": ["Kyle", "Kyle"]]))
try expect(result) == "[\"kyle\", \"kyle\"]"
}
}
}

Expand Down

0 comments on commit c4a84a6

Please sign in to comment.