From 8b2aa90c547dbe673e0b7f64de2b4944c4bb28aa Mon Sep 17 00:00:00 2001 From: Maya Saxena Date: Mon, 31 Jul 2017 12:28:29 -0400 Subject: [PATCH] Address PR comments --- .../String/String+Trimmed.swift | 6 +----- SwiftWisdom/Rx/Rx+String.swift | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/SwiftWisdom/Core/StandardLibrary/String/String+Trimmed.swift b/SwiftWisdom/Core/StandardLibrary/String/String+Trimmed.swift index 10161e1..8814edc 100644 --- a/SwiftWisdom/Core/StandardLibrary/String/String+Trimmed.swift +++ b/SwiftWisdom/Core/StandardLibrary/String/String+Trimmed.swift @@ -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)) } } diff --git a/SwiftWisdom/Rx/Rx+String.swift b/SwiftWisdom/Rx/Rx+String.swift index 183e213..c1e9a7b 100644 --- a/SwiftWisdom/Rx/Rx+String.swift +++ b/SwiftWisdom/Rx/Rx+String.swift @@ -8,6 +8,7 @@ import Foundation import RxSwift +import RxCocoa extension ObservableType where E == String { public func ip_limitLength(to limit: Int) -> Observable { @@ -21,14 +22,27 @@ extension ObservableType where E == String? { } } +extension ControlPropertyType where E == String { + public func ip_limited(toLength length: Int) -> ControlProperty { + let values: Observable = asObservable().map { $0.ip_trimmed(toLength: length) } + let valueSink: AnyObserver = mapObserver { $0 } + return ControlProperty(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) } }