Add transforms empty() and equalzero()

This commit is contained in:
Adam Fowler
2021-03-23 18:17:44 +00:00
parent 40ef59c897
commit f7bd02267e
2 changed files with 14 additions and 0 deletions

View File

@@ -64,6 +64,8 @@ extension Array: HBMustacheTransformable {
return reversed() return reversed()
case "count": case "count":
return count return count
case "empty":
return isEmpty
default: default:
if let comparableSeq = self as? HBComparableSequence { if let comparableSeq = self as? HBComparableSequence {
return comparableSeq.comparableTransform(name) return comparableSeq.comparableTransform(name)
@@ -95,6 +97,8 @@ extension Dictionary: HBMustacheTransformable {
switch name { switch name {
case "count": case "count":
return count return count
case "empty":
return isEmpty
case "enumerated": case "enumerated":
return map { (key: $0.key, value: $0.value) } return map { (key: $0.key, value: $0.value) }
default: default:
@@ -125,6 +129,8 @@ public extension FixedWidthInteger {
/// - Returns: Result /// - Returns: Result
func transform(_ name: String) -> Any? { func transform(_ name: String) -> Any? {
switch name { switch name {
case "equalzero":
return self == 0
case "plusone": case "plusone":
return self + 1 return self + 1
case "minusone": case "minusone":

View File

@@ -128,6 +128,14 @@ final class TransformTests: XCTestCase {
""") """)
} }
func testDictionaryEmpty() throws {
let template = try HBMustacheTemplate(string: """
{{#empty(array)}}Array{{/empty(array)}}{{#empty(dictionary)}}Dictionary{{/empty(dictionary)}}
""")
let object: [String: Any] = ["array":[], "dictionary":[:]]
XCTAssertEqual(template.render(object), "ArrayDictionary")
}
func testListOutput() throws { func testListOutput() throws {
let object = [1, 2, 3, 4] let object = [1, 2, 3, 4]
let template = try HBMustacheTemplate(string: "{{#.}}{{.}}{{^last()}}, {{/last()}}{{/.}}") let template = try HBMustacheTemplate(string: "{{#.}}{{.}}{{^last()}}, {{/last()}}{{/.}}")