From f7bd02267e0506a3eb3b060dee67ad5fa049a744 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Tue, 23 Mar 2021 18:17:44 +0000 Subject: [PATCH] Add transforms empty() and equalzero() --- Sources/HummingbirdMustache/Transform.swift | 6 ++++++ Tests/HummingbirdMustacheTests/TransformTests.swift | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/Sources/HummingbirdMustache/Transform.swift b/Sources/HummingbirdMustache/Transform.swift index 869ed14..ecc0a45 100644 --- a/Sources/HummingbirdMustache/Transform.swift +++ b/Sources/HummingbirdMustache/Transform.swift @@ -64,6 +64,8 @@ extension Array: HBMustacheTransformable { return reversed() case "count": return count + case "empty": + return isEmpty default: if let comparableSeq = self as? HBComparableSequence { return comparableSeq.comparableTransform(name) @@ -95,6 +97,8 @@ extension Dictionary: HBMustacheTransformable { switch name { case "count": return count + case "empty": + return isEmpty case "enumerated": return map { (key: $0.key, value: $0.value) } default: @@ -125,6 +129,8 @@ public extension FixedWidthInteger { /// - Returns: Result func transform(_ name: String) -> Any? { switch name { + case "equalzero": + return self == 0 case "plusone": return self + 1 case "minusone": diff --git a/Tests/HummingbirdMustacheTests/TransformTests.swift b/Tests/HummingbirdMustacheTests/TransformTests.swift index 1d68611..f69355a 100644 --- a/Tests/HummingbirdMustacheTests/TransformTests.swift +++ b/Tests/HummingbirdMustacheTests/TransformTests.swift @@ -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 { let object = [1, 2, 3, 4] let template = try HBMustacheTemplate(string: "{{#.}}{{.}}{{^last()}}, {{/last()}}{{/.}}")