Implement Recursive Transforms (#37)

* Implement Recursive Transforms

* Correct test names

* apply suggestions

* format

* add comments

* move the parse function

* refine `parseTransforms()` function

* refinements

* format

* Swift Format again

---------

Co-authored-by: Adam Fowler <adamfowler71@gmail.com>
This commit is contained in:
Mahdi Bahrami
2024-07-14 15:19:16 +03:30
committed by GitHub
parent a66a7d139c
commit 58b9c3b00c
6 changed files with 203 additions and 51 deletions

View File

@@ -48,7 +48,7 @@ public extension StringProtocol {
case "uppercased":
return uppercased()
case "reversed":
return reversed()
return Substring(self.reversed())
default:
return nil
}
@@ -66,7 +66,7 @@ private protocol ComparableSequence {
extension Array: MustacheTransformable {
/// Transform Array.
///
/// Transforms available are `first`, `last`, `reversed`, `count` and for arrays
/// Transforms available are `first`, `last`, `reversed`, `count`, `empty` and for arrays
/// with comparable elements `sorted`.
/// - Parameter name: transform name
/// - Returns: Result
@@ -102,6 +102,78 @@ extension Array: ComparableSequence where Element: Comparable {
}
}
extension Set: MustacheTransformable {
/// Transform Set.
///
/// Transforms available are `count`, `empty` and for sets
/// with comparable elements `sorted`.
/// - Parameter name: transform name
/// - Returns: Result
public func transform(_ name: String) -> Any? {
switch name {
case "count":
return count
case "empty":
return isEmpty
default:
if let comparableSeq = self as? ComparableSequence {
return comparableSeq.comparableTransform(name)
}
return nil
}
}
}
extension Set: ComparableSequence where Element: Comparable {
func comparableTransform(_ name: String) -> Any? {
switch name {
case "sorted":
return sorted()
default:
return nil
}
}
}
extension ReversedCollection: MustacheTransformable {
/// Transform ReversedCollection.
///
/// Transforms available are `first`, `last`, `reversed`, `count`, `empty` and for collections
/// with comparable elements `sorted`.
/// - Parameter name: transform name
/// - Returns: Result
public func transform(_ name: String) -> Any? {
switch name {
case "first":
return first
case "last":
return last
case "reversed":
return reversed()
case "count":
return count
case "empty":
return isEmpty
default:
if let comparableSeq = self as? ComparableSequence {
return comparableSeq.comparableTransform(name)
}
return nil
}
}
}
extension ReversedCollection: ComparableSequence where Element: Comparable {
func comparableTransform(_ name: String) -> Any? {
switch name {
case "sorted":
return sorted()
default:
return nil
}
}
}
extension Dictionary: MustacheTransformable {
/// Transform Dictionary
///