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

Conversation

mayasaxena
Copy link

  • Added String extension for trimming to specified length
  • Added ObservableType extensions for limiting length of optional and non-optional Strings
  • Added Reactive extension for limiting length of UITextField input

@mayasaxena mayasaxena requested a review from Tom-Le June 13, 2017 13:43
@tzm41
Copy link
Member

tzm41 commented Jun 13, 2017

Can it be made into a ControlProperty<String>, something similar to the implementation of orEmpty, so that it is available for bidirectional binding? I've made this:

extension ControlPropertyType where E == String {
    public var stripReturns: ControlProperty<String> {
        let values: Observable<String> = asObservable().map { $0.replacingOccurrences(of: "\n", with: "") }
        let valueSink: AnyObserver<String> = mapObserver { $0 }
        return ControlProperty<String>(values: values, valueSink: valueSink)
    }
}

so that it could be used like this:

titleField.textView.rx.text.orEmpty.stripReturns <-> viewModel.opportunityTitle >>> rx_disposeBag

extension String {
func ip_trimmed(toLength length: Int) -> String {
if ip_length > length {
return substring(to: index(startIndex, offsetBy: length))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swift 3 has a prefix method that is slightly shorter. See
"The New Substring Type" in https://www.swiftbysundell.com/posts/exploring-the-new-string-api-in-swift-4 for Swift 3 and Swift 4 ways to do this.

@mayasaxena mayasaxena force-pushed the maya/rx-limit-text-length branch from 8b2aa90 to f390a8a Compare June 20, 2018 20:43
@mayasaxena mayasaxena force-pushed the maya/rx-limit-text-length branch from f390a8a to e9dee42 Compare June 20, 2018 20:48
@ghost
Copy link

ghost commented Jun 20, 2018

52 Errors
🚫 Array+Utilities.swift#L125 - SwiftWisdom/Core/StandardLibrary/Array/Array+Utilities.swift#L125: unexpected version number in ‘available’ attribute for non-specific platform ‘*’
@available(*, deprecated: 0.7.11, message: "Use `ip_safely` instead.")
🚫 CellConfiguring.swift#L34 - SwiftWisdom/Core/UIKit/CellConfiguring.swift#L34: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_registerCell<T>(_: T.Type, identifier: String = T.ip_identifier) where T: UICollectionViewCell {
🚫 CellConfiguring.swift#L42 - SwiftWisdom/Core/UIKit/CellConfiguring.swift#L42: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_registerHeader<T>(_: T.Type, identifier: String = T.ip_identifier) where T: UICollectionReusableView {
🚫 CellConfiguring.swift#L46 - SwiftWisdom/Core/UIKit/CellConfiguring.swift#L46: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_registerFooter<T>(_: T.Type, identifier: String = T.ip_identifier) where T: UICollectionReusableView {
🚫 CellConfiguring.swift#L50 - SwiftWisdom/Core/UIKit/CellConfiguring.swift#L50: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_registerSupplementary<T>(_: T.Type, kind: String, identifier: String = T.ip_identifier) where T: UICollectionReusableView {
🚫 CellConfiguring.swift#L61 - SwiftWisdom/Core/UIKit/CellConfiguring.swift#L61: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_dequeueCell<T>(_ indexPath: IndexPath, identifier: String = T.ip_identifier) -> T where T: UICollectionViewCell {
🚫 CellConfiguring.swift#L65 - SwiftWisdom/Core/UIKit/CellConfiguring.swift#L65: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_dequeueHeader<T>(_ indexPath: IndexPath, identifier: String = T.ip_identifier) -> T where T: UICollectionReusableView {
🚫 CellConfiguring.swift#L69 - SwiftWisdom/Core/UIKit/CellConfiguring.swift#L69: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_dequeueFooter<T>(_ indexPath: IndexPath, identifier: String = T.ip_identifier) -> T where T: UICollectionReusableView {
🚫 CellConfiguring.swift#L73 - SwiftWisdom/Core/UIKit/CellConfiguring.swift#L73: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_dequeueSupplementary<T>(_ indexPath: IndexPath, kind: String, identifier: String = T.ip_identifier) -> T where T: UICollectionReusableView {
🚫 CellConfiguring.swift#L85 - SwiftWisdom/Core/UIKit/CellConfiguring.swift#L85: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_registerCell<T>(_: T.Type, identifier: String = T.ip_identifier) where T: UITableViewCell {
🚫 CellConfiguring.swift#L93 - SwiftWisdom/Core/UIKit/CellConfiguring.swift#L93: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_registerHeader<T>(_: T.Type, identifier: String = T.ip_identifier) where T: UITableViewHeaderFooterView {
🚫 CellConfiguring.swift#L104 - SwiftWisdom/Core/UIKit/CellConfiguring.swift#L104: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_dequeueCell<T>(_ indexPath: IndexPath, identifier: String = T.ip_identifier) -> T where T: UITableViewCell {
🚫 CellConfiguring.swift#L108 - SwiftWisdom/Core/UIKit/CellConfiguring.swift#L108: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_dequeueHeader<T>(_ section: Int, identifier: String = T.ip_identifier) -> T where T: UITableViewHeaderFooterView {
🚫 CellConfiguring.swift#L112 - SwiftWisdom/Core/UIKit/CellConfiguring.swift#L112: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_cellForRowAtIndexPath<T>(_ indexPath: IndexPath) -> T where T: UITableViewCell {
🚫 Data+Extensions.swift#L113 - SwiftWisdom/Core/Foundation/Data/Data+Extensions.swift#L113: invalid redeclaration of ‘subscript(ip_safely:)’
public subscript(ip_safely range: Range<Int>) -> Data? {
🚫 Geometry.swift#L10 - SwiftWisdom/Core/Geometry/Geometry.swift#L10: ‘public’ modifier is redundant for property declared in a public extension
public var ip_radians: CGFloat {
🚫 Geometry.swift#L16 - SwiftWisdom/Core/Geometry/Geometry.swift#L16: ‘public’ modifier is redundant for property declared in a public extension
public var ip_radians: CGFloat {
🚫 Geometry.swift#L20 - SwiftWisdom/Core/Geometry/Geometry.swift#L20: ‘public’ modifier is redundant for property declared in a public extension
public var ip_degrees: CGFloat {
🚫 Geometry.swift#L24 - SwiftWisdom/Core/Geometry/Geometry.swift#L24: ‘public’ modifier is redundant for property declared in a public extension
public var ip_squared: CGFloat {
🚫 Geometry.swift#L29 - SwiftWisdom/Core/Geometry/Geometry.swift#L29: ‘public’ modifier is redundant for property declared in a public extension
public var ip_complementOfPercentage: CGFloat {
🚫 Geometry.swift#L35 - SwiftWisdom/Core/Geometry/Geometry.swift#L35: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_angle(to comparisonPoint: CGPoint) -> CGFloat {
🚫 Geometry.swift#L46 - SwiftWisdom/Core/Geometry/Geometry.swift#L46: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_distance(to point: CGPoint) -> CGFloat {
🚫 Geometry.swift#L77 - SwiftWisdom/Core/Geometry/Geometry.swift#L77: ‘public’ modifier is redundant for property declared in a public extension
public var ip_width: CGFloat {
🚫 Geometry.swift#L86 - SwiftWisdom/Core/Geometry/Geometry.swift#L86: ‘public’ modifier is redundant for property declared in a public extension
public var ip_halfWidth: CGFloat {
🚫 Geometry.swift#L95 - SwiftWisdom/Core/Geometry/Geometry.swift#L95: ‘public’ modifier is redundant for property declared in a public extension
public var ip_height: CGFloat {
🚫 Geometry.swift#L104 - SwiftWisdom/Core/Geometry/Geometry.swift#L104: ‘public’ modifier is redundant for property declared in a public extension
public var ip_halfHeight: CGFloat {
🚫 Geometry.swift#L113 - SwiftWisdom/Core/Geometry/Geometry.swift#L113: ‘public’ modifier is redundant for property declared in a public extension
public var ip_center: CGPoint {
🚫 Geometry.swift#L123 - SwiftWisdom/Core/Geometry/Geometry.swift#L123: ‘public’ modifier is redundant for property declared in a public extension
public var ip_midX: CGFloat {
🚫 Geometry.swift#L132 - SwiftWisdom/Core/Geometry/Geometry.swift#L132: ‘public’ modifier is redundant for property declared in a public extension
public var ip_midY: CGFloat {
🚫 Geometry.swift#L141 - SwiftWisdom/Core/Geometry/Geometry.swift#L141: ‘public’ modifier is redundant for property declared in a public extension
public var ip_shortestEdge: CGFloat {
🚫 Geometry.swift#L145 - SwiftWisdom/Core/Geometry/Geometry.swift#L145: ‘public’ modifier is redundant for property declared in a public extension
public var ip_longestEdge: CGFloat {
🚫 PercentAnimator.swift#L20 - SwiftWisdom/Core/UIKit/PercentAnimator/PercentAnimator.swift#L20: ‘public’ modifier is redundant for static method declared in a public extension
public static func pa_percentAnimate(_ duration: TimeInterval, animation: @escaping (AnimationState) -> Void) {
🚫 Set+Utilities.swift#L2 - SwiftWisdom/Core/StandardLibrary/Set/Set+Utilities.swift#L2: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_toArray() -> [Element] {
🚫 Set+Utilities.swift#L7 - SwiftWisdom/Core/StandardLibrary/Set/Set+Utilities.swift#L7: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_passes(test: (Element) -> Bool) -> Bool {
🚫 Set+Utilities.swift#L16 - SwiftWisdom/Core/StandardLibrary/Set/Set+Utilities.swift#L16: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_filter(_ include: (Element) -> Bool) -> Set<Element> {
🚫 String+Indexing.swift#L12 - SwiftWisdom/Core/StandardLibrary/String/String+Indexing.swift#L12: invalid redeclaration of ‘subscript(_:)’
public subscript(range: CountableRange<Int>) -> String {
🚫 UIColor+Hex.swift#L12 - SwiftWisdom/Core/UIKit/UIColor+Hex.swift#L12: ‘public’ modifier is redundant for initializer declared in a public extension
public convenience init(ip_hex hex: String) {
🚫 UIView+GetConstraints.swift#L17 - SwiftWisdom/Core/UIKit/UIView+GetConstraints.swift#L17: ‘public’ modifier is redundant for property declared in a public extension
public var ip_widthConstraint: NSLayoutConstraint? {
🚫 UIView+GetConstraints.swift#L22 - SwiftWisdom/Core/UIKit/UIView+GetConstraints.swift#L22: ‘public’ modifier is redundant for property declared in a public extension
public var ip_heightConstraint: NSLayoutConstraint? {
🚫 UIView+GetConstraints.swift#L27 - SwiftWisdom/Core/UIKit/UIView+GetConstraints.swift#L27: ‘public’ modifier is redundant for property declared in a public extension
public var ip_topConstraint: NSLayoutConstraint? {
🚫 UIView+GetConstraints.swift#L32 - SwiftWisdom/Core/UIKit/UIView+GetConstraints.swift#L32: ‘public’ modifier is redundant for property declared in a public extension
public var ip_bottomConstraint: NSLayoutConstraint? {
🚫 UIView+GetConstraints.swift#L37 - SwiftWisdom/Core/UIKit/UIView+GetConstraints.swift#L37: ‘public’ modifier is redundant for property declared in a public extension
public var ip_leadingConstraint: NSLayoutConstraint? {
🚫 UIView+GetConstraints.swift#L42 - SwiftWisdom/Core/UIKit/UIView+GetConstraints.swift#L42: ‘public’ modifier is redundant for property declared in a public extension
public var ip_trailingConstraint: NSLayoutConstraint? {
🚫 UIView+GetConstraints.swift#L47 - SwiftWisdom/Core/UIKit/UIView+GetConstraints.swift#L47: ‘public’ modifier is redundant for property declared in a public extension
public var ip_centerXConstraint: NSLayoutConstraint? {
🚫 UIView+GetConstraints.swift#L52 - SwiftWisdom/Core/UIKit/UIView+GetConstraints.swift#L52: ‘public’ modifier is redundant for property declared in a public extension
public var ip_centerYConstraint: NSLayoutConstraint? {
🚫 UIView+GetConstraints.swift#L57 - SwiftWisdom/Core/UIKit/UIView+GetConstraints.swift#L57: ‘public’ modifier is redundant for property declared in a public extension
public var ip_aspectRatioConstraint: NSLayoutConstraint? {
🚫 UIView+GetConstraints.swift#L71 - SwiftWisdom/Core/UIKit/UIView+GetConstraints.swift#L71: ‘public’ modifier is redundant for instance method declared in a public extension
public func ip_constraintForAttribute(_ attribute: NSLayoutAttribute, onView: UIView? = nil, toView: UIView? = nil, viewAttribute: NSLayoutAttribute? = nil) -> NSLayoutConstraint? {
🚫 UIView+Nib.swift#L12 - SwiftWisdom/Core/UIKit/UIView+Nib.swift#L12: ‘public’ modifier is redundant for class method declared in a public extension
public class func ip_fromNib(_ nibNameOrNil: String? = nil) -> Self {
🚫 UIView+Nib.swift#L16 - SwiftWisdom/Core/UIKit/UIView+Nib.swift#L16: ‘public’ modifier is redundant for class method declared in a public extension
public class func ip_fromNib<T: UIView>(_ nibNameOrNil: String? = nil, type: T.Type) -> T {
🚫 UIView+Nib.swift#L21 - SwiftWisdom/Core/UIKit/UIView+Nib.swift#L21: ‘public’ modifier is redundant for class method declared in a public extension
public class func ip_fromNib<T: UIView>(_ nibNameOrNil: String? = nil, type: T.Type) -> T? {
🚫 UIView+Nib.swift#L39 - SwiftWisdom/Core/UIKit/UIView+Nib.swift#L39: ‘public’ modifier is redundant for class property declared in a public extension
public class var ip_nibName: String {
🚫 UIView+Nib.swift#L43 - SwiftWisdom/Core/UIKit/UIView+Nib.swift#L43: ‘public’ modifier is redundant for class property declared in a public extension
public class var ip_nib: UINib? {

Generated by 🚫 Danger

Copy link
Contributor

@paulrolfe paulrolfe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebase and :shipit:

@paulrolfe
Copy link
Contributor

@mayasaxena Do you still want this in? Or would you like us to close this pull request?

@mayasaxena
Copy link
Author

@paulrolfe If we still think it's useful I can update and merge

@paulrolfe
Copy link
Contributor

@mayasaxena That would be great, please do! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants