Giter Club home page Giter Club logo

rxoptional's Introduction

RxOptional

CircleCI Version License Platform

RxSwift extensions for Swift optionals and "Occupiable" types.

Usage

All operators are also available on Driver and Signal, unless otherwise noted.

Optional Operators

filterNil
Observable<String?>
    .of("One", nil, "Three")
    .filterNil()
    // Type is now Observable<String>
    .subscribe { print($0) }
next(One)
next(Three)
completed
replaceNilWith
Observable<String?>
    .of("One", nil, "Three")
    .replaceNilWith("Two")
    // Type is now Observable<String>
    .subscribe { print($0) }
next(One)
next(Two)
next(Three)
completed
errorOnNil

Unavailable on Driver, because Drivers cannot error out.

By default errors with RxOptionalError.foundNilWhileUnwrappingOptional.

Observable<String?>
    .of("One", nil, "Three")
    .errorOnNil()
    // Type is now Observable<String>
    .subscribe { print($0) }
next(One)
error(Found nil while trying to unwrap type <Optional<String>>)
catchOnNil
Observable<String?>
    .of("One", nil, "Three")
    .catchOnNil {
        return Observable<String>.just("A String from a new Observable")
    }
    // Type is now Observable<String>
    .subscribe { print($0) }
next(One)
next(A String from a new Observable)
next(Three)
completed
distinctUntilChanged
Observable<Int?>
    .of(5, 6, 6, nil, nil, 3)
    .distinctUntilChanged()
    .subscribe { print($0) }
next(Optional(5))
next(Optional(6))
next(nil)
next(Optional(3))
completed

Occupiable Operators

Occupiables are:

  • String
  • Array
  • Dictionary
  • Set

Currently in Swift protocols cannot be extended to conform to other protocols. For now the types listed above conform to Occupiable. You can also conform custom types to Occupiable.

filterEmpty
Observable<[String]>
    .of(["Single Element"], [], ["Two", "Elements"])
    .filterEmpty()
    .subscribe { print($0) }
next(["Single Element"])
next(["Two", "Elements"])
completed
errorOnEmpty

Unavailable on Driver, because Drivers cannot error out.

By default errors with RxOptionalError.emptyOccupiable.

Observable<[String]>
    .of(["Single Element"], [], ["Two", "Elements"])
    .errorOnEmpty()
    .subscribe { print($0) }
next(["Single Element"])
error(Empty occupiable of type <Array<String>>)
catchOnEmpty
Observable<[String]>
    .of(["Single Element"], [], ["Two", "Elements"])
    .catchOnEmpty {
        return Observable<[String]>.just(["Not Empty"])
    }
    .subscribe { print($0) }
next(["Single Element"])
next(["Not Empty"])
next(["Two", "Elements"])
completed

Running Examples.playground

  • Run pod install in Example directory
  • Select RxOptional Examples target
  • Build
  • Show Debug Area (cmd+shift+Y)
  • Click blue play button in Debug Area

Requirements

Installation

RxOptional is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'RxOptional'

Add this to Cartfile

github "RxSwiftCommunity/RxOptional" ~> 3.1.3
$ carthage update

Author

Thane Gill, [email protected]

License

RxOptional is available under the MIT license. See the LICENSE file for more info.

rxoptional's People

Contributors

ashfurrow avatar clementleroy avatar cruisediary avatar dentelezhkin avatar e-kazakov avatar freak4pc avatar iandundas avatar ikesyo avatar ivanbruel avatar ivanmisuno-albelli avatar lutzifer avatar muukii avatar pinddfull avatar rynecheow avatar scotteg avatar sendyhalim avatar siuying avatar thanegill avatar tonyarnold avatar vhbit avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.