Skip to content

Commit e9dee42

Browse files
committed
Add Rx string limiting extensions
1 parent 3dd552d commit e9dee42

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

Podfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ SPEC CHECKSUMS:
2020

2121
PODFILE CHECKSUM: d81cbe2f98140887848195204b8b667819be7198
2222

23-
COCOAPODS: 1.3.1
23+
COCOAPODS: 1.4.0

SwiftWisdom.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
80D63D241C4F231C00C88D00 /* Synchronize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80D63D231C4F231C00C88D00 /* Synchronize.swift */; };
101101
80D63D261C4F236F00C88D00 /* Not.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80D63D251C4F236F00C88D00 /* Not.swift */; };
102102
80D63D2F1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80D63D2C1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift */; };
103+
A53A4EE71EEF330100CC53C6 /* Rx+String.swift in Sources */ = {isa = PBXBuildFile; fileRef = A53A4EE61EEF330100CC53C6 /* Rx+String.swift */; };
104+
A53A4EE91EF01F8000CC53C6 /* String+Trimmed.swift in Sources */ = {isa = PBXBuildFile; fileRef = A53A4EE81EF01F8000CC53C6 /* String+Trimmed.swift */; };
103105
CD16463F1CC16A410049BBD5 /* UILabel+Typography.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD16463E1CC16A410049BBD5 /* UILabel+Typography.swift */; };
104106
CDCDE2561E67A6EC007B3784 /* ResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCDE2551E67A6EC007B3784 /* ResultTests.swift */; };
105107
CDCDE2591E67C574007B3784 /* NSDateComparableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCDE2581E67C574007B3784 /* NSDateComparableTests.swift */; };
@@ -231,6 +233,8 @@
231233
80D63D2C1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UnsignedInteger+Extensions.swift"; sourceTree = "<group>"; };
232234
94555EC02E932CF736415F8E /* Pods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods.framework; sourceTree = BUILT_PRODUCTS_DIR; };
233235
96E808C6BB3914E0E49C738D /* Pods_SwiftWisdom.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SwiftWisdom.framework; sourceTree = BUILT_PRODUCTS_DIR; };
236+
A53A4EE61EEF330100CC53C6 /* Rx+String.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Rx+String.swift"; sourceTree = "<group>"; };
237+
A53A4EE81EF01F8000CC53C6 /* String+Trimmed.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Trimmed.swift"; sourceTree = "<group>"; };
234238
CD16463E1CC16A410049BBD5 /* UILabel+Typography.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UILabel+Typography.swift"; sourceTree = "<group>"; };
235239
CDCDE2551E67A6EC007B3784 /* ResultTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResultTests.swift; sourceTree = "<group>"; };
236240
CDCDE2581E67C574007B3784 /* NSDateComparableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NSDateComparableTests.swift; path = Date/NSDateComparableTests.swift; sourceTree = "<group>"; };
@@ -636,6 +640,7 @@
636640
1C99FB571DD51DEC009FC493 /* String+Data.swift */,
637641
1C99FB5B1DD525FF009FC493 /* String+Numbers.swift */,
638642
311178581E16CACF00294C25 /* String+Attributed.swift */,
643+
A53A4EE81EF01F8000CC53C6 /* String+Trimmed.swift */,
639644
);
640645
path = String;
641646
sourceTree = "<group>";
@@ -711,6 +716,7 @@
711716
8037D86D1CAD9981008F324C /* Rx+Extensions.swift */,
712717
E7E2576B1E3F99FF00CA3DBD /* Rx+DelayElements.swift */,
713718
E7A613971E3FB27F006382DA /* Rx+RepeatingTimeouts.swift */,
719+
A53A4EE61EEF330100CC53C6 /* Rx+String.swift */,
714720
);
715721
path = Rx;
716722
sourceTree = "<group>";
@@ -1062,6 +1068,7 @@
10621068
1C16E3031D27029D008A1001 /* NSLock+Extensions.swift in Sources */,
10631069
CD16463F1CC16A410049BBD5 /* UILabel+Typography.swift in Sources */,
10641070
80232C091BF2F1BD00818B6E /* UIColor+Hex.swift in Sources */,
1071+
A53A4EE91EF01F8000CC53C6 /* String+Trimmed.swift in Sources */,
10651072
80D63D021C4F16FE00C88D00 /* UIViewController+Nibs.swift in Sources */,
10661073
802C4BB41C1B6E4600C69D80 /* Downloader.swift in Sources */,
10671074
80D63D261C4F236F00C88D00 /* Not.swift in Sources */,
@@ -1090,6 +1097,7 @@
10901097
802C4BBD1C1B6FF300C69D80 /* TimeoutOperation.swift in Sources */,
10911098
80232C061BF2F1BD00818B6E /* Set+Utilities.swift in Sources */,
10921099
8002059E1BF683E0005852C9 /* Result.swift in Sources */,
1100+
A53A4EE71EEF330100CC53C6 /* Rx+String.swift in Sources */,
10931101
800205A21BF684C7005852C9 /* NSDate+Comparable.swift in Sources */,
10941102
0CED7D671CD6971400672913 /* Double+Extensions.swift in Sources */,
10951103
80D63D2F1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift in Sources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// String+Trimmed.swift
3+
// SwiftWisdom
4+
//
5+
// Created by Maya Saxena on 6/13/17.
6+
// Copyright © 2017 Intrepid. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension String {
12+
func ip_trimmed(toLength length: Int) -> String {
13+
return String(prefix(length))
14+
}
15+
}

SwiftWisdom/Rx/Rx+String.swift

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// Rx+String.swift
3+
// SwiftWisdom
4+
//
5+
// Created by Maya Saxena on 6/12/17.
6+
// Copyright © 2017 Intrepid. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import RxSwift
11+
import RxCocoa
12+
13+
extension ObservableType where E == String {
14+
public func ip_limitLength(to limit: Int) -> Observable<E> {
15+
return self.map { $0.ip_trimmed(toLength: limit) }
16+
}
17+
}
18+
19+
extension ObservableType where E == String? {
20+
public func ip_limitLength(to limit: Int) -> Observable<E> {
21+
return self.map { $0?.ip_trimmed(toLength: limit) }
22+
}
23+
}
24+
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+
33+
extension Reactive where Base: UITextField {
34+
/** Limit the length of input to a text field
35+
36+
Usage Example:
37+
38+
textField.rx.ip_limitLength(to: 5)
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 {
46+
return text.ip_limitLength(to: limit).bind(to: text)
47+
}
48+
}

0 commit comments

Comments
 (0)