Skip to content

Commit 8b2aa90

Browse files
committed
Address PR comments
1 parent 3643e26 commit 8b2aa90

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

SwiftWisdom/Core/StandardLibrary/String/String+Trimmed.swift

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import Foundation
1010

1111
extension String {
1212
func ip_trimmed(toLength length: Int) -> String {
13-
if ip_length > length {
14-
return substring(to: index(startIndex, offsetBy: length))
15-
} else {
16-
return self
17-
}
13+
return String(characters.prefix(length))
1814
}
1915
}

SwiftWisdom/Rx/Rx+String.swift

+16-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import Foundation
1010
import RxSwift
11+
import RxCocoa
1112

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

25+
extension ControlPropertyType where E == String {
26+
public func ip_limited(toLength length: Int) -> ControlProperty<String> {
27+
let values: Observable<String> = asObservable().map { $0.ip_trimmed(toLength: length) }
28+
let valueSink: AnyObserver<String> = mapObserver { $0 }
29+
return ControlProperty<String>(values: values, valueSink: valueSink)
30+
}
31+
}
32+
2433
extension Reactive where Base: UITextField {
2534
/** Limit the length of input to a text field
2635

2736
Usage Example:
2837

2938
textField.rx.ip_limitLength(to: 5)
30-
*/
31-
public func ip_limitLength(to limit: Int) -> Disposable {
39+
40+
Note: This does not restrict length when the text property of the text field is modified directly e.g.
41+
42+
textField.text = "A string over the limit"
43+
44+
*/
45+
public func ip_limitInputLength(to limit: Int) -> Disposable {
3246
return text.ip_limitLength(to: limit).bind(to: text)
3347
}
3448
}

0 commit comments

Comments
 (0)