dtop / SwiftFilter

Chainable filters that can be applied to collections to filter (change / correct) form inputs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SwiftFilter

Build Status Compatibility DependencyManagement Carthage compatible GitHub license codebeat badge codecov.io GitHub release

Chainable filters that can be applied to collections to filter (change / correct) form inputs

By Danilo Topalovic.

Introduction

When I wrote SwiftValidate I encountered some problems in form validation. A bunch of field values weren't really errorneous but needed to be changed in some way.

Of course a validator should not change the value in any way so I decided to do another project with filtering.

Similar to Zend\Filter this also works with a filter chain which applies a bunch of filters to a single value. I Use SwiftFilter along with SwiftValidate together with Eureka.

Requirements

  • iOS 8.0+
  • Xcode 7.0+

Installation

See CocoaPods for easy installation into your project

Add SwiftFilter to your Podfile

platform :ios, '8.0'
use_frameworks!

pod 'SwiftValidate'

See Carthage

Add ``SwiftFilterto yourCartfile`


github "dtop/SwiftFilter"

Usage

let inValue: String = "  \t \n  Some foo String   \t\t \n "

let chain = FilterChain()
	<~~ TrimFilter()
	<~~ ReplaceFilter {
		$0.replaceType = .ReplaceString(searchStr: "foo", replacementStr: "bar")
	}
	
do {

	let outValue: String? = chain.filter(inValue, nil)
	
} catch _ {

}

Included Filters

CallbackFilter

Enables you to add a verry custom behavior to the filter chain by adding your callback which is executed on the value

Configuration

value type default description
callback closure ! the callback you have to provide

Parameters

input output explination
Any Any -

CharCaseFilter

Allows to change the char case of the given string

Configuration

value type default description
charCase CharCaseFilter.Case ! .Lowercase, .Uppercase, .Ucfirst, .Ucwords

Parameters

input output explination
String String only String is supported

ReplaceFilter

Allows to replace a String with another

Configuration

value type default description
replaceType ReplaceFilter.Case ! @example see below
	// searches and replaces all string occurences
	let filter = ReplaceFilter {
		$0.replaceType = .ReplaceString(searchStr: "foo", replacementStr: "bar")
	}

	// replaces a given range that is seered by advancements of stringIndex
	let filter = ReplaceFilter {
		$0.replaceType = .ReplaceRange(advStart: 0, advEnd: 3, replacementStr: "bar")
	}
	

Parameters

input output explination
String String only String is supported

TrimFilter

Trims whitespace around the given String

Configuration

none

Parameters

input output explination
String String only String is supported

RegexReplaceFilter

Allows the replacement of a substring by regex pattern matching

Configuration

value type default description
pattern String ! the pattern to match against
options NSRegularExpressionOptions 0 options for the regular expression
matchingOptions NSMatchingOptions 0 options for the matching
replacement String ! the replacement

Parameters

input output explination
String String only String is supported

Extensibility

Adding own filters that are working with the chain is quite easy

class MyFilter: FilterProtocol {
	
	var mySetting: String!
	
	required init(@noescape _ initialize: (RegexReplaceFilter) -> Void = { _ in }) {
        
       initialize(self)
   }
   
   func filter<I: Any, O: Any>(value: I?, _ context: [String: AnyObject]?) throws -> O? {
        
        // your filtering
   }
}

// can be used:

let chain = FilterChain()
	<~~ MyFilter {
		$0.mySetting = "foo"
	}

About

Chainable filters that can be applied to collections to filter (change / correct) form inputs

License:MIT License


Languages

Language:Swift 94.6%Language:Ruby 2.1%Language:Objective-C 2.0%Language:Shell 1.3%