Type-safe value transformers with error handling, inspired by Mantle 2.0's MTLTransformerErrorHandling.
Carthage is a simple, decentralized dependency manager for Cocoa.
- Add ValueTransformer to your Cartfile:
github "felixjendrusch/ValueTransformer" ~> 2.3
-
Run
carthage update
to fetch and build ValueTransformer and its dependencies.
A ValueTransformer
supports closure-based forward value transformation. It conforms to ValueTransformerType
.
let uppercase = ValueTransformer<String, String, NSError> { value in
return success(value.uppercaseString)
}
let result = uppercase.transform("example") // "EXAMPLE"
Value transformers can be chained together using the left-to-right and right-to-left composition operators (>>>
and <<<
respectively):
let join = ValueTransformer<[String], String, NSError> { values in
return success(" ".join(values))
}
let result = (join >>> uppercase).transform([ "hello", "world" ]) // "HELLO WORLD"
The value and transformed value type of a value transformer can be lifted into an Optional
:
let lifted: ValueTransformer<String, String?, NSError> = lift(uppercase)
let result = lifted.transform("example") // .Some("EXAMPLE")
let lifted: ValueTransformer<String?, String, NSError> = lift(uppercase, defaultTransformedValue: "default")
let result1 = lifted.transform("example") // "EXAMPLE"
let result2 = lifted.transform(nil) // "default"
let lifted: ValueTransformer<String?, String?, NSError> = lift(uppercase)
let result = lifted.transform(nil) // nil
The value and transformed value type of a value transformer can also be lifted into an Array
:
let lifted: ValueTransformer<[String], [String], NSError> = lift(uppercase)
let result = lifted.transform([ "hello", "world" ]) // [ "HELLO", "WORLD" ]
A ReversibleValueTransformer
supports closure-based forward and backward value transformation. It conforms to ReversibleValueTransformerType
, which in turn conforms to ValueTransformerType
.
let caze = ReversibleValueTransformer<String, String, NSError>(transformClosure: { value in
return success(value.uppercaseString)
}, reverseTransformClosure: { transformedValue in
return success(transformedValue.lowercaseString)
})
let result1 = caze.transform("example") // "EXAMPLE"
let result2 = caze.reverseTransform("EXAMPLE") // "example"
The same operations that can be applied to a ValueTransformer
may also be applied to a ReverisbleValueTransformer
. In addition, the following operations are supported.
Two suitable value transformers can be combined into a reversible value transformer:
let lowercase = ValueTransformer<String, String, NSError> { value in
return success(value.lowercaseString)
}
let combined = combine(uppercase, lowercase)
let result1 = combined.transform("example") // "EXAMPLE"
let result2 = combined.reverseTransform("EXAMPLE") // "example"
A reversible value transformer can be flipped:
let flipped = flip(combined)
let result1 = flipped.transform("EXAMPLE") // "example"
let result2 = flipped.reverseTransform("example") // "EXAMPLE"
When lifting the transformed value type of a reversible value transformer into an Optional
, a default reverse transformed value must be provided:
let lifted = lift(flipped, defaultReverseTransformedValue: "default")
let result1 = lifted.reverseTransform("example") // "EXAMPLE"
let result2 = lifted.reverseTransform(nil) // "default"
All transformations and reverse transformations return a Result
, which either holds the (reverse) transformed value or an error. This enables you to gracefully handle transformation errors.