Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rx string limiting extensions #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 15 additions & 0 deletions SwiftWisdom/Core/StandardLibrary/String/String+Trimmed.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// 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 {
return String(prefix(length))
}
}
48 changes: 48 additions & 0 deletions SwiftWisdom/Rx/Rx+String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Rx+String.swift
// SwiftWisdom
//
// Created by Maya Saxena on 6/12/17.
// Copyright © 2017 Intrepid. All rights reserved.
//

import Foundation
import RxSwift
import RxCocoa

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 ControlPropertyType where E == String {
public func ip_limited(toLength length: Int) -> ControlProperty<String> {
let values: Observable<String> = asObservable().map { $0.ip_trimmed(toLength: length) }
let valueSink: AnyObserver<String> = mapObserver { $0 }
return ControlProperty<String>(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)

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)
}
}