Add methods for Array and Dictionary

This commit is contained in:
Adam Fowler
2021-03-12 17:37:25 +00:00
parent c9e33153f3
commit 8df4e63432
7 changed files with 163 additions and 50 deletions

View File

@@ -22,6 +22,32 @@ extension String: HBMustacheBaseMethods {
switch name { switch name {
case "lowercased": case "lowercased":
return self.lowercased() return self.lowercased()
case "uppercased":
return self.uppercased()
default:
return nil
}
}
}
extension Array: HBMustacheBaseMethods {
func runMethod(_ name: String) -> Any? {
switch name {
case "reversed":
return self.reversed()
case "enumerated":
return self.enumerated()
default:
return nil
}
}
}
extension Dictionary: HBMustacheBaseMethods {
func runMethod(_ name: String) -> Any? {
switch name {
case "enumerated":
return self.enumerated()
default: default:
return nil return nil
} }

View File

@@ -24,21 +24,21 @@ extension HBMustacheTemplate {
switch parser.current() { switch parser.current() {
case "#": case "#":
parser.unsafeAdvance() parser.unsafeAdvance()
let (name, _) = try parseName(&parser) let (name, method) = try parseName(&parser)
if parser.current() == "\n" { if parser.current() == "\n" {
parser.unsafeAdvance() parser.unsafeAdvance()
} }
let sectionTokens = try parse(&parser, sectionName: name) let sectionTokens = try parse(&parser, sectionName: name)
tokens.append(.section(name, HBMustacheTemplate(sectionTokens))) tokens.append(.section(name: name, method: method, template: HBMustacheTemplate(sectionTokens)))
case "^": case "^":
parser.unsafeAdvance() parser.unsafeAdvance()
let (name, _) = try parseName(&parser) let (name, method) = try parseName(&parser)
if parser.current() == "\n" { if parser.current() == "\n" {
parser.unsafeAdvance() parser.unsafeAdvance()
} }
let sectionTokens = try parse(&parser, sectionName: name) let sectionTokens = try parse(&parser, sectionName: name)
tokens.append(.invertedSection(name, HBMustacheTemplate(sectionTokens))) tokens.append(.invertedSection(name: name, method: method, template: HBMustacheTemplate(sectionTokens)))
case "/": case "/":
parser.unsafeAdvance() parser.unsafeAdvance()
@@ -53,9 +53,9 @@ extension HBMustacheTemplate {
case "{": case "{":
parser.unsafeAdvance() parser.unsafeAdvance()
let (name, _) = try parseName(&parser) let (name, method) = try parseName(&parser)
guard try parser.read("}") else { throw Error.unfinishedName } guard try parser.read("}") else { throw Error.unfinishedName }
tokens.append(.unescapedVariable(name)) tokens.append(.unescapedVariable(name: name, method: method))
case "!": case "!":
parser.unsafeAdvance() parser.unsafeAdvance()
@@ -68,7 +68,7 @@ extension HBMustacheTemplate {
default: default:
let (name, method) = try parseName(&parser) let (name, method) = try parseName(&parser)
tokens.append(.variable(name, method)) tokens.append(.variable(name: name, method: method))
} }
} }
// should never get here if reading section // should never get here if reading section

View File

@@ -7,29 +7,23 @@ 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 var child = getChild(named: variable, from: object) { if let child = getChild(named: variable, from: object, method: method) {
if let method = method,
let runnable = child as? HBMustacheBaseMethods {
if let result = runnable.runMethod(method) {
child = result
}
}
if let template = child as? HBMustacheTemplate { if let template = child as? HBMustacheTemplate {
string += template.render(object, library: library) string += template.render(object, library: library)
} else { } else {
string += htmlEscape(String(describing: child)) string += htmlEscape(String(describing: child))
} }
} }
case .unescapedVariable(let variable): case .unescapedVariable(let variable, let method):
if let child = getChild(named: variable, from: object) { if let child = getChild(named: variable, from: object, method: method) {
string += String(describing: child) string += String(describing: child)
} }
case .section(let variable, let template): case .section(let variable, let method, let template):
let child = getChild(named: variable, from: object) let child = getChild(named: variable, from: object, method: method)
string += renderSection(child, parent: object, with: template, library: library) string += renderSection(child, parent: object, with: template, library: library)
case .invertedSection(let variable, let template): case .invertedSection(let variable, let method, let template):
let child = getChild(named: variable, from: object) let child = getChild(named: variable, from: object, method: method)
string += renderInvertedSection(child, parent: object, with: template, library: library) string += renderInvertedSection(child, parent: object, with: template, library: library)
case .partial(let name): case .partial(let name):
@@ -67,7 +61,7 @@ extension HBMustacheTemplate {
} }
} }
func getChild(named name: String, from object: Any) -> Any? { func getChild(named name: String, from object: Any, method: String?) -> 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?
@@ -82,9 +76,20 @@ extension HBMustacheTemplate {
return _getChild(named: names2, from: childObject!) return _getChild(named: names2, from: childObject!)
} }
if name == "." { return object } let child: Any?
if name == "." {
child = object
} else {
let nameSplit = name.split(separator: ".").map { String($0) } let nameSplit = name.split(separator: ".").map { String($0) }
return _getChild(named: nameSplit[...], from: object) child = _getChild(named: nameSplit[...], from: object)
}
if let method = method,
let runnable = child as? HBMustacheBaseMethods {
if let result = runnable.runMethod(method) {
return result
}
}
return child
} }
private static let htmlEscapedCharacters: [Character: String] = [ private static let htmlEscapedCharacters: [Character: String] = [
@@ -140,3 +145,37 @@ extension Array: HBSequence {
return "" return ""
} }
} }
extension ReversedCollection: HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
var string = ""
for obj in self {
string += template.render(obj, library: library)
}
return string
}
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
if count == 0 {
return template.render(self, library: library)
}
return ""
}
}
extension EnumeratedSequence: HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
var string = ""
for obj in self {
string += template.render(obj, library: library)
}
return string
}
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
if self.underestimatedCount == 0 {
return template.render(self, library: library)
}
return ""
}
}

View File

@@ -10,10 +10,10 @@ public class HBMustacheTemplate {
enum Token { enum Token {
case text(String) case text(String)
case variable(String, String? = nil) case variable(name: String, method: String? = nil)
case unescapedVariable(String) case unescapedVariable(name: String, method: String? = nil)
case section(String, HBMustacheTemplate) case section(name: String, method: String? = nil, template: HBMustacheTemplate)
case invertedSection(String, HBMustacheTemplate) case invertedSection(name: String, method: String? = nil, template: HBMustacheTemplate)
case partial(String) case partial(String)
} }

View File

@@ -0,0 +1,60 @@
import XCTest
@testable import HummingbirdMustache
final class MethodTests: XCTestCase {
func testLowercased() throws {
let template = try HBMustacheTemplate(string: """
{{ lowercased(name) }}
""")
let object: [String: Any] = ["name": "Test"]
XCTAssertEqual(template.render(object), "test")
}
func testUppercased() throws {
let template = try HBMustacheTemplate(string: """
{{ uppercased(name) }}
""")
let object: [String: Any] = ["name": "Test"]
XCTAssertEqual(template.render(object), "TEST")
}
func testReversed() throws {
let template = try HBMustacheTemplate(string: """
{{#reversed(repo)}}
<b>{{ name }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
<b>rip</b>
<b>hub</b>
<b>resque</b>
""")
}
func testArrayEnumerated() throws {
let template = try HBMustacheTemplate(string: """
{{#enumerated(repo)}}
<b>{{ offset }}) {{ element.name }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
<b>0) resque</b>
<b>1) hub</b>
<b>2) rip</b>
""")
}
func testDictionaryEnumerated() throws {
let template = try HBMustacheTemplate(string: """
{{#enumerated(.)}}<b>{{ element.key }} = {{ element.value }}</b>{{/.}}
""")
let object: [String: Any] = ["one": 1, "two": 2]
let result = template.render(object)
XCTAssertTrue(result == "<b>one = 1</b><b>two = 2</b>" || result == "<b>two = 2</b><b>one = 1</b>")
}
}

View File

@@ -9,17 +9,17 @@ final class TemplateParserTests: XCTestCase {
func testVariable() throws { func testVariable() throws {
let template = try HBMustacheTemplate(string: "test {{variable}}") let template = try HBMustacheTemplate(string: "test {{variable}}")
XCTAssertEqual(template.tokens, [.text("test "), .variable("variable")]) XCTAssertEqual(template.tokens, [.text("test "), .variable(name: "variable")])
} }
func testSection() throws { func testSection() throws {
let template = try HBMustacheTemplate(string: "test {{#section}}text{{/section}}") let template = try HBMustacheTemplate(string: "test {{#section}}text{{/section}}")
XCTAssertEqual(template.tokens, [.text("test "), .section("section", .init([.text("text")]))]) XCTAssertEqual(template.tokens, [.text("test "), .section(name: "section", template: .init([.text("text")]))])
} }
func testInvertedSection() throws { func testInvertedSection() throws {
let template = try HBMustacheTemplate(string: "test {{^section}}text{{/section}}") let template = try HBMustacheTemplate(string: "test {{^section}}text{{/section}}")
XCTAssertEqual(template.tokens, [.text("test "), .invertedSection("section", .init([.text("text")]))]) XCTAssertEqual(template.tokens, [.text("test "), .invertedSection(name: "section", template: .init([.text("text")]))])
} }
func testComment() throws { func testComment() throws {
@@ -29,7 +29,7 @@ final class TemplateParserTests: XCTestCase {
func testWhitespace() throws { func testWhitespace() throws {
let template = try HBMustacheTemplate(string: "{{ section }}") let template = try HBMustacheTemplate(string: "{{ section }}")
XCTAssertEqual(template.tokens, [.variable("section")]) XCTAssertEqual(template.tokens, [.variable(name: "section")])
} }
func testSectionEndError() throws { func testSectionEndError() throws {
@@ -79,10 +79,12 @@ extension HBMustacheTemplate.Token: Equatable {
return lhs == rhs return lhs == rhs
case (.variable(let lhs, let lhs2), .variable(let rhs, let rhs2)): case (.variable(let lhs, let lhs2), .variable(let rhs, let rhs2)):
return lhs == rhs && lhs2 == rhs2 return lhs == rhs && lhs2 == rhs2
case (.section(let lhs1, let lhs2), .section(let rhs1, let rhs2)): case (.section(let lhs1, let lhs2, let lhs3), .section(let rhs1, let rhs2, let rhs3)):
return lhs1 == rhs1 && lhs2 == rhs2 return lhs1 == rhs1 && lhs2 == rhs2 && lhs3 == rhs3
case (.invertedSection(let lhs1, let lhs2), .invertedSection(let rhs1, let rhs2)): case (.invertedSection(let lhs1, let lhs2, let lhs3), .invertedSection(let rhs1, let rhs2, let rhs3)):
return lhs1 == rhs1 && lhs2 == rhs2 return lhs1 == rhs1 && lhs2 == rhs2 && lhs3 == rhs3
case (.partial(let name1), .partial(let name2)):
return name1 == name2
default: default:
return false return false
} }

View File

@@ -192,20 +192,6 @@ final class TemplateRendererTests: XCTestCase {
""") """)
} }
func testLowercased() throws {
let template = try HBMustacheTemplate(string: """
{{#repo}}
<b>{{ lowercased(name) }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "Resque"], ["name": "Hub"], ["name": "RIP"]]]
XCTAssertEqual(template.render(object), """
<b>resque</b>
<b>hub</b>
<b>rip</b>
""")
}
func testPerformance() throws { func testPerformance() throws {
let template = try HBMustacheTemplate(string: """ let template = try HBMustacheTemplate(string: """
{{#repo}} {{#repo}}