Add support for first/last functions

This commit is contained in:
Adam Fowler
2021-03-14 12:47:00 +00:00
parent 5f49dd6953
commit 33b488fe40
5 changed files with 50 additions and 10 deletions

View File

@@ -1,10 +1,22 @@
struct HBMustacheContext { struct HBMustacheContext: HBMustacheMethods {
let first: Bool var first: Bool
let last: Bool var last: Bool
init(first: Bool = false, last: Bool = false) { init(first: Bool = false, last: Bool = false) {
self.first = first self.first = first
self.last = last self.last = last
} }
func runMethod(_ name: String) -> Any? {
switch name {
case "first":
return self.first
case "last":
return self.last
default:
return nil
}
}
} }

View File

@@ -7,9 +7,19 @@ protocol HBMustacheSequence {
extension Sequence { extension Sequence {
func renderSection(with template: HBMustacheTemplate) -> String { func renderSection(with template: HBMustacheTemplate) -> String {
var string = "" var string = ""
for obj in self { var context = HBMustacheContext(first: true)
string += template.render(obj) var iterator = self.makeIterator()
guard var currentObject = iterator.next() else { return "" }
while let object = iterator.next() {
string += template.render(currentObject, context: context)
currentObject = object
context.first = false
} }
context.last = true
string += template.render(currentObject, context: context)
return string return string
} }

View File

@@ -7,7 +7,7 @@ extension HBMustacheTemplate {
case .text(let text): case .text(let text):
string += text string += text
case .variable(let variable, let method): case .variable(let variable, let method):
if let child = getChild(named: variable, from: object, method: method) { if let child = getChild(named: variable, from: object, method: method, context: context) {
if let template = child as? HBMustacheTemplate { if let template = child as? HBMustacheTemplate {
string += template.render(object) string += template.render(object)
} else { } else {
@@ -15,15 +15,15 @@ extension HBMustacheTemplate {
} }
} }
case .unescapedVariable(let variable, let method): case .unescapedVariable(let variable, let method):
if let child = getChild(named: variable, from: object, method: method) { if let child = getChild(named: variable, from: object, method: method, context: context) {
string += String(describing: child) string += String(describing: child)
} }
case .section(let variable, let method, let template): case .section(let variable, let method, let template):
let child = getChild(named: variable, from: object, method: method) let child = getChild(named: variable, from: object, method: method, context: context)
string += renderSection(child, parent: object, with: template) string += renderSection(child, parent: object, with: template)
case .invertedSection(let variable, let method, let template): case .invertedSection(let variable, let method, let template):
let child = getChild(named: variable, from: object, method: method) let child = getChild(named: variable, from: object, method: method, context: context)
string += renderInvertedSection(child, parent: object, with: template) string += renderInvertedSection(child, parent: object, with: template)
case .partial(let name): case .partial(let name):
@@ -63,7 +63,7 @@ extension HBMustacheTemplate {
} }
} }
func getChild(named name: String, from object: Any, method: String?) -> Any? { func getChild(named name: String, from object: Any, method: String?, context: HBMustacheContext?) -> Any? {
func _getChild(named names: ArraySlice<String>, from object: Any) -> Any? { func _getChild(named names: ArraySlice<String>, from object: Any) -> Any? {
guard let name = names.first else { return object } guard let name = names.first else { return object }
let childObject: Any? let childObject: Any?
@@ -81,6 +81,8 @@ extension HBMustacheTemplate {
let child: Any? let child: Any?
if name == "." { if name == "." {
child = object child = object
} else if name == "", method != nil {
child = context
} else { } else {
let nameSplit = name.split(separator: ".").map { String($0) } let nameSplit = name.split(separator: ".").map { String($0) }
child = _getChild(named: nameSplit[...], from: object) child = _getChild(named: nameSplit[...], from: object)

View File

@@ -18,6 +18,21 @@ final class MethodTests: XCTestCase {
XCTAssertEqual(template.render(object), "TEST") XCTAssertEqual(template.render(object), "TEST")
} }
func testFirstLast() throws {
let template = try HBMustacheTemplate(string: """
{{#repo}}
<b>{{#first()}}first: {{/}}{{#last()}}last: {{/}}{{ name }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
<b>first: resque</b>
<b>hub</b>
<b>last: rip</b>
""")
}
func testReversed() throws { func testReversed() throws {
let template = try HBMustacheTemplate(string: """ let template = try HBMustacheTemplate(string: """
{{#reversed(repo)}} {{#reversed(repo)}}

View File

@@ -25,6 +25,7 @@ final class TemplateRendererTests: XCTestCase {
func testArraySection() throws { func testArraySection() throws {
let template = try HBMustacheTemplate(string: "test {{#value}}*{{.}}{{/value}}") let template = try HBMustacheTemplate(string: "test {{#value}}*{{.}}{{/value}}")
XCTAssertEqual(template.render(["value": ["test2", "bool"]]), "test *test2*bool") XCTAssertEqual(template.render(["value": ["test2", "bool"]]), "test *test2*bool")
XCTAssertEqual(template.render(["value": ["test2"]]), "test *test2")
XCTAssertEqual(template.render(["value": []]), "test ") XCTAssertEqual(template.render(["value": []]), "test ")
} }