From e9dee42a8b6fccd7ba88b29b7843f1fa906061a6 Mon Sep 17 00:00:00 2001 From: Maya Saxena Date: Tue, 13 Jun 2017 09:20:43 -0400 Subject: [PATCH] Add Rx string limiting extensions --- Podfile.lock | 2 +- SwiftWisdom.xcodeproj/project.pbxproj | 8 ++++ .../String/String+Trimmed.swift | 15 ++++++ SwiftWisdom/Rx/Rx+String.swift | 48 +++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 SwiftWisdom/Core/StandardLibrary/String/String+Trimmed.swift create mode 100644 SwiftWisdom/Rx/Rx+String.swift diff --git a/Podfile.lock b/Podfile.lock index cdc61ed..cf5f666 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -20,4 +20,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: d81cbe2f98140887848195204b8b667819be7198 -COCOAPODS: 1.3.1 +COCOAPODS: 1.4.0 diff --git a/SwiftWisdom.xcodeproj/project.pbxproj b/SwiftWisdom.xcodeproj/project.pbxproj index 503c275..d086ed4 100644 --- a/SwiftWisdom.xcodeproj/project.pbxproj +++ b/SwiftWisdom.xcodeproj/project.pbxproj @@ -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 */; }; @@ -231,6 +233,8 @@ 80D63D2C1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UnsignedInteger+Extensions.swift"; sourceTree = ""; }; 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 = ""; }; + A53A4EE81EF01F8000CC53C6 /* String+Trimmed.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Trimmed.swift"; sourceTree = ""; }; CD16463E1CC16A410049BBD5 /* UILabel+Typography.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UILabel+Typography.swift"; sourceTree = ""; }; CDCDE2551E67A6EC007B3784 /* ResultTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResultTests.swift; sourceTree = ""; }; CDCDE2581E67C574007B3784 /* NSDateComparableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NSDateComparableTests.swift; path = Date/NSDateComparableTests.swift; sourceTree = ""; }; @@ -636,6 +640,7 @@ 1C99FB571DD51DEC009FC493 /* String+Data.swift */, 1C99FB5B1DD525FF009FC493 /* String+Numbers.swift */, 311178581E16CACF00294C25 /* String+Attributed.swift */, + A53A4EE81EF01F8000CC53C6 /* String+Trimmed.swift */, ); path = String; sourceTree = ""; @@ -711,6 +716,7 @@ 8037D86D1CAD9981008F324C /* Rx+Extensions.swift */, E7E2576B1E3F99FF00CA3DBD /* Rx+DelayElements.swift */, E7A613971E3FB27F006382DA /* Rx+RepeatingTimeouts.swift */, + A53A4EE61EEF330100CC53C6 /* Rx+String.swift */, ); path = Rx; sourceTree = ""; @@ -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 */, @@ -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 */, diff --git a/SwiftWisdom/Core/StandardLibrary/String/String+Trimmed.swift b/SwiftWisdom/Core/StandardLibrary/String/String+Trimmed.swift new file mode 100644 index 0000000..f3173e3 --- /dev/null +++ b/SwiftWisdom/Core/StandardLibrary/String/String+Trimmed.swift @@ -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)) + } +} diff --git a/SwiftWisdom/Rx/Rx+String.swift b/SwiftWisdom/Rx/Rx+String.swift new file mode 100644 index 0000000..2c97f40 --- /dev/null +++ b/SwiftWisdom/Rx/Rx+String.swift @@ -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 { + return self.map { $0.ip_trimmed(toLength: limit) } + } +} + +extension ObservableType where E == String? { + public func ip_limitLength(to limit: Int) -> Observable { + return self.map { $0?.ip_trimmed(toLength: limit) } + } +} + +extension ControlPropertyType where E == String { + public func ip_limited(toLength length: Int) -> ControlProperty { + let values: Observable = asObservable().map { $0.ip_trimmed(toLength: length) } + let valueSink: AnyObserver = mapObserver { $0 } + return ControlProperty(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) + } +}