Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mayasaxena committed Jul 31, 2017
1 parent 3643e26 commit 8b2aa90
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 1 addition & 5 deletions SwiftWisdom/Core/StandardLibrary/String/String+Trimmed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import Foundation

extension String {
func ip_trimmed(toLength length: Int) -> String {
if ip_length > length {
return substring(to: index(startIndex, offsetBy: length))
} else {
return self
}
return String(characters.prefix(length))
}
}
18 changes: 16 additions & 2 deletions SwiftWisdom/Rx/Rx+String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation
import RxSwift
import RxCocoa

extension ObservableType where E == String {
public func ip_limitLength(to limit: Int) -> Observable<E> {
Expand All @@ -21,14 +22,27 @@ extension ObservableType where E == String? {
}
}

extension ControlPropertyType where E == String {
public func ip_limited(toLength length: Int) -> ControlProperty<String> {
let values: Observable<String> = asObservable().map { $0.ip_trimmed(toLength: length) }
let valueSink: AnyObserver<String> = mapObserver { $0 }
return ControlProperty<String>(values: values, valueSink: valueSink)
}
}

extension Reactive where Base: UITextField {
/** Limit the length of input to a text field

Usage Example:

textField.rx.ip_limitLength(to: 5)
*/
public func ip_limitLength(to limit: Int) -> Disposable {

Note: This does not restrict length when the text property of the text field is modified directly e.g.

textField.text = "A string over the limit"

*/
public func ip_limitInputLength(to limit: Int) -> Disposable {
return text.ip_limitLength(to: limit).bind(to: text)
}
}

0 comments on commit 8b2aa90

Please sign in to comment.