Skip to content

Commit 3643e26

Browse files
committed
Add Rx string limiting extensions
1 parent e605abb commit 3643e26

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

SwiftWisdom.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
80D63D241C4F231C00C88D00 /* Synchronize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80D63D231C4F231C00C88D00 /* Synchronize.swift */; };
8383
80D63D261C4F236F00C88D00 /* Not.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80D63D251C4F236F00C88D00 /* Not.swift */; };
8484
80D63D2F1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80D63D2C1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift */; };
85+
A53A4EE71EEF330100CC53C6 /* Rx+String.swift in Sources */ = {isa = PBXBuildFile; fileRef = A53A4EE61EEF330100CC53C6 /* Rx+String.swift */; };
86+
A53A4EE91EF01F8000CC53C6 /* String+Trimmed.swift in Sources */ = {isa = PBXBuildFile; fileRef = A53A4EE81EF01F8000CC53C6 /* String+Trimmed.swift */; };
8587
CD16463F1CC16A410049BBD5 /* UILabel+Typography.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD16463E1CC16A410049BBD5 /* UILabel+Typography.swift */; };
8688
CDCDE2561E67A6EC007B3784 /* ResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCDE2551E67A6EC007B3784 /* ResultTests.swift */; };
8789
CDCDE2591E67C574007B3784 /* NSDateComparableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCDE2581E67C574007B3784 /* NSDateComparableTests.swift */; };
@@ -195,6 +197,8 @@
195197
80D63D2C1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UnsignedInteger+Extensions.swift"; sourceTree = "<group>"; };
196198
94555EC02E932CF736415F8E /* Pods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods.framework; sourceTree = BUILT_PRODUCTS_DIR; };
197199
96E808C6BB3914E0E49C738D /* Pods_SwiftWisdom.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SwiftWisdom.framework; sourceTree = BUILT_PRODUCTS_DIR; };
200+
A53A4EE61EEF330100CC53C6 /* Rx+String.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Rx+String.swift"; sourceTree = "<group>"; };
201+
A53A4EE81EF01F8000CC53C6 /* String+Trimmed.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Trimmed.swift"; sourceTree = "<group>"; };
198202
CD16463E1CC16A410049BBD5 /* UILabel+Typography.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UILabel+Typography.swift"; sourceTree = "<group>"; };
199203
CDCDE2551E67A6EC007B3784 /* ResultTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResultTests.swift; sourceTree = "<group>"; };
200204
CDCDE2581E67C574007B3784 /* NSDateComparableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NSDateComparableTests.swift; path = Date/NSDateComparableTests.swift; sourceTree = "<group>"; };
@@ -583,6 +587,7 @@
583587
FFCA61AF1C8A1F1B00FD35A7 /* String+Empty.swift */,
584588
1C99FB571DD51DEC009FC493 /* String+Data.swift */,
585589
1C99FB5B1DD525FF009FC493 /* String+Numbers.swift */,
590+
A53A4EE81EF01F8000CC53C6 /* String+Trimmed.swift */,
586591
);
587592
path = String;
588593
sourceTree = "<group>";
@@ -652,6 +657,7 @@
652657
8037D86D1CAD9981008F324C /* Rx+Extensions.swift */,
653658
E7E2576B1E3F99FF00CA3DBD /* Rx+DelayElements.swift */,
654659
E7A613971E3FB27F006382DA /* Rx+RepeatingTimeouts.swift */,
660+
A53A4EE61EEF330100CC53C6 /* Rx+String.swift */,
655661
);
656662
path = Rx;
657663
sourceTree = "<group>";
@@ -957,6 +963,7 @@
957963
1C16E3031D27029D008A1001 /* NSLock+Extensions.swift in Sources */,
958964
CD16463F1CC16A410049BBD5 /* UILabel+Typography.swift in Sources */,
959965
80232C091BF2F1BD00818B6E /* UIColor+Hex.swift in Sources */,
966+
A53A4EE91EF01F8000CC53C6 /* String+Trimmed.swift in Sources */,
960967
80D63D021C4F16FE00C88D00 /* UIViewController+Nibs.swift in Sources */,
961968
802C4BB41C1B6E4600C69D80 /* Downloader.swift in Sources */,
962969
80D63D261C4F236F00C88D00 /* Not.swift in Sources */,
@@ -982,6 +989,7 @@
982989
802C4BBD1C1B6FF300C69D80 /* TimeoutOperation.swift in Sources */,
983990
80232C061BF2F1BD00818B6E /* Set+Utilities.swift in Sources */,
984991
8002059E1BF683E0005852C9 /* Result.swift in Sources */,
992+
A53A4EE71EEF330100CC53C6 /* Rx+String.swift in Sources */,
985993
800205A21BF684C7005852C9 /* NSDate+Comparable.swift in Sources */,
986994
0CED7D671CD6971400672913 /* Double+Extensions.swift in Sources */,
987995
80D63D2F1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift in Sources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
if ip_length > length {
14+
return substring(to: index(startIndex, offsetBy: length))
15+
} else {
16+
return self
17+
}
18+
}
19+
}

SwiftWisdom/Rx/Rx+String.swift

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
12+
extension ObservableType where E == String {
13+
public func ip_limitLength(to limit: Int) -> Observable<E> {
14+
return self.map { $0.ip_trimmed(toLength: limit) }
15+
}
16+
}
17+
18+
extension ObservableType where E == String? {
19+
public func ip_limitLength(to limit: Int) -> Observable<E> {
20+
return self.map { $0?.ip_trimmed(toLength: limit) }
21+
}
22+
}
23+
24+
extension Reactive where Base: UITextField {
25+
/** Limit the length of input to a text field
26+
27+
Usage Example:
28+
29+
textField.rx.ip_limitLength(to: 5)
30+
*/
31+
public func ip_limitLength(to limit: Int) -> Disposable {
32+
return text.ip_limitLength(to: limit).bind(to: text)
33+
}
34+
}

0 commit comments

Comments
 (0)