ValueTransformer toolkit for swift

Overview

ValueTransformerKit

Build Status Swift 5.1 Stargazers Issues MIT License Sponsor Become a Patron! Buy me a coffee

A closure and protocol based framework for ValueTransformer, helpful functions to register ValueTransformer by identifier.

See Some implementations

Create a ValueTransformer

Using closures

let transformer = ValueTransformer.closure { object in
   return ...
}

A ValueTransformer subclass is no more necessary using this method.

Using protocol implementation

Implement ValueTransformerType or ResersableValueTransformerType and an a computed property transformer will be accessible.

Using enum

Define your enum, a list of all your enum case in transformers, implement the function transformedValue of ValueTransformerType protocol

enum StringTransformers: String, ValueTransformers, ValueTransformerType {
    case capitalized, lowercased, uppercased

    public static let transformers: [StringTransformers] = [.capitalized, .lowercased, .uppercased]

    public func transformedValue(_ value: Any?) -> Any? { ../* string manipulation */ }
}

Register it

You can retrieve a value transformer using optional initializer of ValueTransformer: init?(forName: NSValueTransformerName)

A protocol ValueTransformerRegisterable help you to register your ValueTransformer By providing a value transformer and an identifier name, a new method will be available

myValueTransformer.register()

So just defined an identifier name in your ValueTransformerType

struct MyTransformer: ValueTransformerType, ValueTransformerRegisterable {
    var name = NSValueTransformerName(rawValue: "MyTransformation")

For a singleton instance

You can define a singleton instance using ValueTransformerSingleton

struct MyTransformer: ValueTransformerType, ValueTransformerRegisterable, ValueTransformerSingleton {
    var name = NSValueTransformerName(rawValue: "MyTransformation")
    public static let instance = MyTransformer()

or a static function will help you to register it

MyTransformer.register() // same as MyTransformer.instance.register()

For the previous enum example

enum StringTransformers: String, ValueTransformers, ValueTransformerType {
  ...
  var name: NSValueTransformerName {
     return NSValueTransformerName("String" + self.rawValue.capitalized)
  }

then you can register one by one

StringTransformers.capitalized.register()

or all case

StringTransformers.register()

Some implementations

String

  • capitalized
  • uppercased
  • lowercased

Image

  • PNG Representation
  • JPEG Representation

Date and Time

  • RFC 2822 Timestamp*
  • Using DateFormatter.Style

Number

  • Using NumberFormatter.Style

Locale component

  • Using NSLocale.Key

Check if empty or not

  • Using IsEmpty(resp. IsNotEmpty) you could transform String, NSString, Array, NSArray, Dictionnary, etc... to boolean. true(resp. false) if empty

Apple Doc

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ValueTransformers/ValueTransformers.html

License

ValueTransformerKit is available under the MIT license. See the LICENSE file for more info.

You might also like...
JASON is a faster JSON deserializer written in Swift.
JASON is a faster JSON deserializer written in Swift.

JASON is a faster JSON deserializer written in Swift. JASON is the best framework we found to manage JSON at Swapcard. This is by far the fastest and

ObjectMapper is a framework written in Swift that makes it easy for you to convert your model objects to and from JSON.

ObjectMapper is a framework written in Swift that makes it easy for you to convert your model objects (classes and structs) to and from J

PMJSON provides a pure-Swift strongly-typed JSON encoder/decoder

Now Archived and Forked PMJSON will not be maintained in this repository going forward. Please use, create issues on, and make PRs to the fork of PMJS

SwiftyJSON makes it easy to deal with JSON data in Swift.

SwiftyJSON SwiftyJSON makes it easy to deal with JSON data in Swift. Platform Build Status *OS Linux Why is the typical JSON handling in Swift NOT goo

macOS app to generate Swift 5 code for models from JSON (with Codeable)
macOS app to generate Swift 5 code for models from JSON (with Codeable)

SwiftyJSONAccelerator - MacOS app Codeable Model file Generator For Swift 5 Version v2.2 Generate initializer function for classes Application Downloa

The easy to use Swift JSON decoder
The easy to use Swift JSON decoder

Unbox is deprecated in favor of Swift’s built-in Codable API and the Codextended project. All current users are highly encouraged to migrate to Codable as soon as possible.

YamlSwift - Load YAML and JSON documents using Swift

YamlSwift parses a string of YAML document(s) (or a JSON document) and returns a Yaml enum value representing that string.

Swift implementation of JMESPath, a query language for JSON.

Swift implementation of JMESPath, the JSON query language

Precise JSON decimal parsing for Swift 🧮

PreciseDecimal Introduction Swift has long suffered a problem with its Decimal type: unapparent loss of precision. This happens with all common ways o

Owner
Eric Marchand
🧙🍺🥊🥋🏓
Eric Marchand
Swift-json - High-performance json parsing in swift

json 0.1.4 swift-json is a pure-Swift JSON parsing library designed for high-per

kelvin 43 Dec 15, 2022
Codable code is a Swift Package that allows you to convert JSON Strings into Swift structs

Codable code is a Swift Package that allows you to convert JSON Strings into Swift structs.

Julio Cesar Guzman Villanueva 2 Oct 6, 2022
Implement dynamic JSON decoding within the constraints of Swift's sound type system by working on top of Swift's Codable implementations.

DynamicCodableKit DynamicCodableKit helps you to implement dynamic JSON decoding within the constraints of Swift's sound type system by working on top

SwiftyLab 15 Oct 16, 2022
AlamofireObjectMappe - An Alamofire extension which converts JSON response data into swift objects using ObjectMapper

AlamofireObjectMapper An extension to Alamofire which automatically converts JSON response data into swift objects using ObjectMapper. Usage Given a U

Tristan Himmelman 2.6k Dec 29, 2022
Decodable Simple and strict, yet powerful object mapping made possible by Swift 2's error handling.

Decodable Simple and strict, yet powerful object mapping made possible by Swift 2's error handling. Greatly inspired by Argo, but without a bizillion

Johannes Lund 1k Jul 15, 2022
Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable

Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable. Elevate should no longer be used for

Nike Inc. 611 Oct 23, 2022
Freddy - A reusable framework for parsing JSON in Swift.

Why Freddy? Parsing JSON elegantly and safely can be hard, but Freddy is here to help. Freddy is a reusable framework for parsing JSON in Swift. It ha

Big Nerd Ranch 1.1k Jan 1, 2023
[Deprecated] A shiny JSON parsing library in Swift :sparkles: Loved by many from 2015-2021

?? Deprecation Notice ?? Gloss has been deprecated in favor of Swift's Codable framework. The existing Gloss source is not going away, however updates

Harlan Kellaway 1.6k Nov 24, 2022
HandyJSON is a framework written in Swift which to make converting model objects to and from JSON easy on iOS.

HandyJSON To deal with crash on iOS 14 beta4 please try version 5.0.3-beta HandyJSON is a framework written in Swift which to make converting model ob

Alibaba 4.1k Dec 29, 2022
Himotoki (紐解き) is a type-safe JSON decoding library written purely in Swift.

Himotoki Himotoki (紐解き) is a type-safe JSON decoding library written purely in Swift. This library is highly inspired by the popular Swift JSON parsin

IKEDA Sho 799 Dec 6, 2022