Skip to content

Commit

Permalink
Add Rx string limiting extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
mayasaxena committed Jun 20, 2018
1 parent 3dd552d commit 522c16a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: d81cbe2f98140887848195204b8b667819be7198

COCOAPODS: 1.3.1
COCOAPODS: 1.4.0
8 changes: 8 additions & 0 deletions SwiftWisdom.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
80D63D241C4F231C00C88D00 /* Synchronize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80D63D231C4F231C00C88D00 /* Synchronize.swift */; };
80D63D261C4F236F00C88D00 /* Not.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80D63D251C4F236F00C88D00 /* Not.swift */; };
80D63D2F1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80D63D2C1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift */; };
A53A4EE71EEF330100CC53C6 /* Rx+String.swift in Sources */ = {isa = PBXBuildFile; fileRef = A53A4EE61EEF330100CC53C6 /* Rx+String.swift */; };
A53A4EE91EF01F8000CC53C6 /* String+Trimmed.swift in Sources */ = {isa = PBXBuildFile; fileRef = A53A4EE81EF01F8000CC53C6 /* String+Trimmed.swift */; };
CD16463F1CC16A410049BBD5 /* UILabel+Typography.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD16463E1CC16A410049BBD5 /* UILabel+Typography.swift */; };
CDCDE2561E67A6EC007B3784 /* ResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCDE2551E67A6EC007B3784 /* ResultTests.swift */; };
CDCDE2591E67C574007B3784 /* NSDateComparableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCDE2581E67C574007B3784 /* NSDateComparableTests.swift */; };
Expand Down Expand Up @@ -231,6 +233,8 @@
80D63D2C1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UnsignedInteger+Extensions.swift"; sourceTree = "<group>"; };
94555EC02E932CF736415F8E /* Pods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods.framework; sourceTree = BUILT_PRODUCTS_DIR; };
96E808C6BB3914E0E49C738D /* Pods_SwiftWisdom.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SwiftWisdom.framework; sourceTree = BUILT_PRODUCTS_DIR; };
A53A4EE61EEF330100CC53C6 /* Rx+String.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Rx+String.swift"; sourceTree = "<group>"; };
A53A4EE81EF01F8000CC53C6 /* String+Trimmed.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Trimmed.swift"; sourceTree = "<group>"; };
CD16463E1CC16A410049BBD5 /* UILabel+Typography.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UILabel+Typography.swift"; sourceTree = "<group>"; };
CDCDE2551E67A6EC007B3784 /* ResultTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResultTests.swift; sourceTree = "<group>"; };
CDCDE2581E67C574007B3784 /* NSDateComparableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NSDateComparableTests.swift; path = Date/NSDateComparableTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -636,6 +640,7 @@
1C99FB571DD51DEC009FC493 /* String+Data.swift */,
1C99FB5B1DD525FF009FC493 /* String+Numbers.swift */,
311178581E16CACF00294C25 /* String+Attributed.swift */,
A53A4EE81EF01F8000CC53C6 /* String+Trimmed.swift */,
);
path = String;
sourceTree = "<group>";
Expand Down Expand Up @@ -711,6 +716,7 @@
8037D86D1CAD9981008F324C /* Rx+Extensions.swift */,
E7E2576B1E3F99FF00CA3DBD /* Rx+DelayElements.swift */,
E7A613971E3FB27F006382DA /* Rx+RepeatingTimeouts.swift */,
A53A4EE61EEF330100CC53C6 /* Rx+String.swift */,
);
path = Rx;
sourceTree = "<group>";
Expand Down Expand Up @@ -1062,6 +1068,7 @@
1C16E3031D27029D008A1001 /* NSLock+Extensions.swift in Sources */,
CD16463F1CC16A410049BBD5 /* UILabel+Typography.swift in Sources */,
80232C091BF2F1BD00818B6E /* UIColor+Hex.swift in Sources */,
A53A4EE91EF01F8000CC53C6 /* String+Trimmed.swift in Sources */,
80D63D021C4F16FE00C88D00 /* UIViewController+Nibs.swift in Sources */,
802C4BB41C1B6E4600C69D80 /* Downloader.swift in Sources */,
80D63D261C4F236F00C88D00 /* Not.swift in Sources */,
Expand Down Expand Up @@ -1090,6 +1097,7 @@
802C4BBD1C1B6FF300C69D80 /* TimeoutOperation.swift in Sources */,
80232C061BF2F1BD00818B6E /* Set+Utilities.swift in Sources */,
8002059E1BF683E0005852C9 /* Result.swift in Sources */,
A53A4EE71EEF330100CC53C6 /* Rx+String.swift in Sources */,
800205A21BF684C7005852C9 /* NSDate+Comparable.swift in Sources */,
0CED7D671CD6971400672913 /* Double+Extensions.swift in Sources */,
80D63D2F1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift in Sources */,
Expand Down
19 changes: 19 additions & 0 deletions SwiftWisdom/Core/StandardLibrary/String/String+Trimmed.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// String+Trimmed.swift
// SwiftWisdom
//
// Created by Maya Saxena on 6/13/17.
// Copyright © 2017 Intrepid. All rights reserved.
//

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
}
}
}
34 changes: 34 additions & 0 deletions SwiftWisdom/Rx/Rx+String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Rx+String.swift
// SwiftWisdom
//
// Created by Maya Saxena on 6/12/17.
// Copyright © 2017 Intrepid. All rights reserved.
//

import Foundation
import RxSwift

extension ObservableType where E == String {
public func ip_limitLength(to limit: Int) -> Observable<E> {
return self.map { $0.ip_trimmed(toLength: limit) }
}
}

extension ObservableType where E == String? {
public func ip_limitLength(to limit: Int) -> Observable<E> {
return self.map { $0?.ip_trimmed(toLength: limit) }
}
}

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 {
return text.ip_limitLength(to: limit).bind(to: text)
}
}

0 comments on commit 522c16a

Please sign in to comment.