Add transforms empty() and equalzero()
This commit is contained in:
@@ -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":
|
||||
|
||||
@@ -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()}}{{/.}}")
|
||||
|
||||
Reference in New Issue
Block a user