Added sorted methods
This commit is contained in:
@@ -18,6 +18,10 @@ struct HBMustacheContext: HBMustacheMethods {
|
||||
return self.last
|
||||
case "index":
|
||||
return self.index
|
||||
case "even":
|
||||
return (self.index & 1) == 0
|
||||
case "odd":
|
||||
return (self.index & 1) == 1
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -10,19 +10,39 @@ extension String: HBMustacheMethods {
|
||||
return self.lowercased()
|
||||
case "uppercased":
|
||||
return self.uppercased()
|
||||
case "reversed":
|
||||
return self.reversed()
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protocol HBComparableSequence {
|
||||
func runComparableMethod(_ name: String) -> Any?
|
||||
}
|
||||
|
||||
extension Array: HBMustacheMethods {
|
||||
func runMethod(_ name: String) -> Any? {
|
||||
switch name {
|
||||
case "reversed":
|
||||
return self.reversed()
|
||||
case "enumerated":
|
||||
return self.enumerated()
|
||||
case "count":
|
||||
return self.count
|
||||
default:
|
||||
if let comparableSeq = self as? HBComparableSequence {
|
||||
return comparableSeq.runComparableMethod(name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Array: HBComparableSequence where Element: Comparable {
|
||||
func runComparableMethod(_ name: String) -> Any? {
|
||||
switch name {
|
||||
case "sorted":
|
||||
return self.sorted()
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
@@ -32,8 +52,24 @@ extension Array: HBMustacheMethods {
|
||||
extension Dictionary: HBMustacheMethods {
|
||||
func runMethod(_ name: String) -> Any? {
|
||||
switch name {
|
||||
case "count":
|
||||
return self.count
|
||||
case "enumerated":
|
||||
return self.enumerated()
|
||||
return self.map { (key: $0.key, value: $0.value) }
|
||||
default:
|
||||
if let comparableSeq = self as? HBComparableSequence {
|
||||
return comparableSeq.runComparableMethod(name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Dictionary: HBComparableSequence where Key: Comparable {
|
||||
func runComparableMethod(_ name: String) -> Any? {
|
||||
switch name {
|
||||
case "sorted":
|
||||
return self.map { (key: $0.key, value: $0.value) }.sorted { $0.key < $1.key }
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
@@ -43,7 +79,7 @@ extension Dictionary: HBMustacheMethods {
|
||||
extension Int: HBMustacheMethods {
|
||||
func runMethod(_ name: String) -> Any? {
|
||||
switch name {
|
||||
case "plus1":
|
||||
case "plusone":
|
||||
return self + 1
|
||||
default:
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user