Added sorted methods
This commit is contained in:
@@ -18,6 +18,10 @@ struct HBMustacheContext: HBMustacheMethods {
|
|||||||
return self.last
|
return self.last
|
||||||
case "index":
|
case "index":
|
||||||
return self.index
|
return self.index
|
||||||
|
case "even":
|
||||||
|
return (self.index & 1) == 0
|
||||||
|
case "odd":
|
||||||
|
return (self.index & 1) == 1
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,19 +10,39 @@ extension String: HBMustacheMethods {
|
|||||||
return self.lowercased()
|
return self.lowercased()
|
||||||
case "uppercased":
|
case "uppercased":
|
||||||
return self.uppercased()
|
return self.uppercased()
|
||||||
|
case "reversed":
|
||||||
|
return self.reversed()
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protocol HBComparableSequence {
|
||||||
|
func runComparableMethod(_ name: String) -> Any?
|
||||||
|
}
|
||||||
|
|
||||||
extension Array: HBMustacheMethods {
|
extension Array: HBMustacheMethods {
|
||||||
func runMethod(_ name: String) -> Any? {
|
func runMethod(_ name: String) -> Any? {
|
||||||
switch name {
|
switch name {
|
||||||
case "reversed":
|
case "reversed":
|
||||||
return self.reversed()
|
return self.reversed()
|
||||||
case "enumerated":
|
case "count":
|
||||||
return self.enumerated()
|
return self.count
|
||||||
|
default:
|
||||||
|
if let comparableSeq = self as? HBComparableSequence {
|
||||||
|
return comparableSeq.runComparableMethod(name)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Array: HBComparableSequence where Element: Comparable {
|
||||||
|
func runComparableMethod(_ name: String) -> Any? {
|
||||||
|
switch name {
|
||||||
|
case "sorted":
|
||||||
|
return self.sorted()
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -32,8 +52,24 @@ extension Array: HBMustacheMethods {
|
|||||||
extension Dictionary: HBMustacheMethods {
|
extension Dictionary: HBMustacheMethods {
|
||||||
func runMethod(_ name: String) -> Any? {
|
func runMethod(_ name: String) -> Any? {
|
||||||
switch name {
|
switch name {
|
||||||
|
case "count":
|
||||||
|
return self.count
|
||||||
case "enumerated":
|
case "enumerated":
|
||||||
return self.enumerated()
|
return self.map { (key: $0.key, value: $0.value) }
|
||||||
|
default:
|
||||||
|
if let comparableSeq = self as? HBComparableSequence {
|
||||||
|
return comparableSeq.runComparableMethod(name)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Dictionary: HBComparableSequence where Key: Comparable {
|
||||||
|
func runComparableMethod(_ name: String) -> Any? {
|
||||||
|
switch name {
|
||||||
|
case "sorted":
|
||||||
|
return self.map { (key: $0.key, value: $0.value) }.sorted { $0.key < $1.key }
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -43,7 +79,7 @@ extension Dictionary: HBMustacheMethods {
|
|||||||
extension Int: HBMustacheMethods {
|
extension Int: HBMustacheMethods {
|
||||||
func runMethod(_ name: String) -> Any? {
|
func runMethod(_ name: String) -> Any? {
|
||||||
switch name {
|
switch name {
|
||||||
case "plus1":
|
case "plusone":
|
||||||
return self + 1
|
return self + 1
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ final class MethodTests: XCTestCase {
|
|||||||
func testIndex() throws {
|
func testIndex() throws {
|
||||||
let template = try HBMustacheTemplate(string: """
|
let template = try HBMustacheTemplate(string: """
|
||||||
{{#repo}}
|
{{#repo}}
|
||||||
<b>{{#index()}}{{plus1(.)}}) {{/}}{{ name }}</b>
|
<b>{{#index()}}{{plusone(.)}}{{/}}) {{ name }}</b>
|
||||||
{{/repo}}
|
{{/repo}}
|
||||||
""")
|
""")
|
||||||
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
|
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
|
||||||
@@ -48,6 +48,21 @@ final class MethodTests: XCTestCase {
|
|||||||
""")
|
""")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testEvenOdd() throws {
|
||||||
|
let template = try HBMustacheTemplate(string: """
|
||||||
|
{{#repo}}
|
||||||
|
<b>{{index()}}) {{#even()}}even {{/}}{{#odd()}}odd {{/}}{{ name }}</b>
|
||||||
|
{{/repo}}
|
||||||
|
""")
|
||||||
|
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
|
||||||
|
XCTAssertEqual(template.render(object), """
|
||||||
|
<b>0) even resque</b>
|
||||||
|
<b>1) odd hub</b>
|
||||||
|
<b>2) even rip</b>
|
||||||
|
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
|
||||||
func testReversed() throws {
|
func testReversed() throws {
|
||||||
let template = try HBMustacheTemplate(string: """
|
let template = try HBMustacheTemplate(string: """
|
||||||
{{#reversed(repo)}}
|
{{#reversed(repo)}}
|
||||||
@@ -63,10 +78,10 @@ final class MethodTests: XCTestCase {
|
|||||||
""")
|
""")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testArrayEnumerated() throws {
|
func testArrayIndex() throws {
|
||||||
let template = try HBMustacheTemplate(string: """
|
let template = try HBMustacheTemplate(string: """
|
||||||
{{#enumerated(repo)}}
|
{{#repo}}
|
||||||
<b>{{ offset }}) {{ element.name }}</b>
|
<b>{{ index() }}) {{ name }}</b>
|
||||||
{{/repo}}
|
{{/repo}}
|
||||||
""")
|
""")
|
||||||
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
|
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
|
||||||
@@ -78,13 +93,37 @@ final class MethodTests: XCTestCase {
|
|||||||
""")
|
""")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testArraySorted() throws {
|
||||||
|
let template = try HBMustacheTemplate(string: """
|
||||||
|
{{#sorted(repo)}}
|
||||||
|
<b>{{ index() }}) {{ . }}</b>
|
||||||
|
{{/repo}}
|
||||||
|
""")
|
||||||
|
let object: [String: Any] = ["repo": ["resque", "hub", "rip"]]
|
||||||
|
XCTAssertEqual(template.render(object), """
|
||||||
|
<b>0) hub</b>
|
||||||
|
<b>1) resque</b>
|
||||||
|
<b>2) rip</b>
|
||||||
|
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
|
||||||
func testDictionaryEnumerated() throws {
|
func testDictionaryEnumerated() throws {
|
||||||
let template = try HBMustacheTemplate(string: """
|
let template = try HBMustacheTemplate(string: """
|
||||||
{{#enumerated(.)}}<b>{{ element.key }} = {{ element.value }}</b>{{/.}}
|
{{#enumerated(.)}}<b>{{ key }} = {{ value }}</b>{{/.}}
|
||||||
""")
|
""")
|
||||||
let object: [String: Any] = ["one": 1, "two": 2]
|
let object: [String: Any] = ["one": 1, "two": 2]
|
||||||
let result = template.render(object)
|
let result = template.render(object)
|
||||||
XCTAssertTrue(result == "<b>one = 1</b><b>two = 2</b>" || result == "<b>two = 2</b><b>one = 1</b>")
|
XCTAssertTrue(result == "<b>one = 1</b><b>two = 2</b>" || result == "<b>two = 2</b><b>one = 1</b>")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testDictionarySortedByKey() throws {
|
||||||
|
let template = try HBMustacheTemplate(string: """
|
||||||
|
{{#sorted(.)}}<b>{{ key }} = {{ value }}</b>{{/.}}
|
||||||
|
""")
|
||||||
|
let object: [String: Any] = ["one": 1, "two": 2, "three": 3]
|
||||||
|
let result = template.render(object)
|
||||||
|
XCTAssertEqual(result, "<b>one = 1</b><b>three = 3</b><b>two = 2</b>")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user