Swiftpack.co -  GodL/Binder as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
GodL/Binder
Binder is a data binding library driven by PropertyWrapper.
.package(url: "https://github.com/GodL/Binder.git", from: "1.0.2")

Binder

Binder is a data binding library driven by PropertyWrapper.

Binder implements a type-safe binding API. and some transform operator functions like Rxswift. These operator functions can be combined with each other indefinitely.

Operator

Filter

like Rxswift's filter

let person = Person()
$age.filter { age in
    age > 0 && age <= 18
} ~> person.bind[\.age]
age = -1

Map

like Rxswift's map

let person = Person()

$age.map {
    "\($0)"
} ~> person.bind[\.ageText]

age = 10

Do

like Rxswift's do

let person = Person()

$age.do { age in
    XCTAssertEqual(age, 10)
} ~> person.bind[\.age]

age = 10

Merge

like Rxswift's merge

@Binding var age1: Int = 0

@Binding var age2: Int = 0

let person = Person()
$age.merge($age1, $age2) ~> person.bind[\.age]

age = 10
XCTAssertEqual(person.age, 10)

age1 = 11
XCTAssertEqual(person.age, 11)

age2 = 12
XCTAssertEqual(person.age, 12)

Skip

like Rxswift's skip

let person = Person()

$age.skip(3) ~> person.bind[\.age]

age = 10
XCTAssertNotEqual(person.age, 10)
age = 11
XCTAssertNotEqual(person.age, 11)
age = 12
XCTAssertNotEqual(person.age, 12)
age = 13

XCTAssertEqual(person.age, 13)

Take

like Rxswift's take

let person = Person()
$age.take(3) ~> person.bind[\.age]

age = 10
XCTAssertEqual(person.age, 10)
age = 11
XCTAssertEqual(person.age, 11)
age = 12
XCTAssertEqual(person.age, 12)
age = 13
XCTAssertNotEqual(person.age, 13)
XCTAssertEqual(person.age, 12)

Delay

like Rxswift's delay

let person = Person()

$age.delay(.seconds(3)) ~> person.bind[\.age]

age = 18

XCTAssertEqual(person.age, 1)

let expectation = self.expectation(description: "testDelay")
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
    XCTAssertEqual(person.age, 18)
    expectation.fulfill()
}

DistinctUntilChanged

like Rxswift's distinctUntilChanged

let person = Person()

$age.distinctUntilChanged() ~> person.bind[\.age]
age = 10
age = 10
age = 18
age = 18
XCTAssertEqual(person.age, 18)
XCTAssertEqual(person.ageDidSetCount, 2)

Combine

Each combination

let person = Person()

$age.filter { age in
    return age >= 11
}.map { age in
    return String(age)
}.do { age in
    XCTAssertEqual(age, String(self.age))
}.skip(2).take(10).distinctUntilChanged().delay(.seconds(3)) ~> person.bind[\.ageText]

UIKit

let label = UILabel()

$name ~> label.bind.text

Disposable

The binding can unbind, use disposable.dispose to unbind

let person = Person()
let dispose = $age ~> person.bind[\.age]

$age.filter { age in
    age >= 18
}.map { age in
    String(age)
} ~> person.bind[\.ageText]

age = 18

XCTAssertEqual(person.age, 18)
XCTAssertEqual(person.ageText, "18")

dispose.dispose()

age = 19
XCTAssertEqual(person.age, 18)
XCTAssertEqual(person.ageText, "19")

Installation

Swift Package Manager

Swift Package Manager is Apple's decentralized dependency manager to integrate libraries to your Swift projects. It is now fully integrated with Xcode 11

To integrate ResponderChain into your project using SPM, specify it in your Package.swift file:

let package = Package(
    …
    dependencies: [
        .package(url: "https://github.com/GodL/Binder.git", from: "1.0.2"),
    ],
    targets: [
        .target(name: "YourTarget", dependencies: ["Binder", …])
        …
    ]
)

Cocoapods

pod 'PropertyWrapperBinder', '~> 1.0.2'

Author

GodL, 547188371@qq.com. Github GodL

I am happy to answer any questions you may have. Just create a new issue.

License

MIT License

Copyright (c) 2021 GodL

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

GitHub

link
Stars: 6
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API