Skip to content

Commit

Permalink
Add inline documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mkj-is committed Sep 22, 2021
1 parent 29673bc commit fe58851
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/BindingKit/Binding+Coalescing.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import SwiftUI

extension Binding {
/// Coalesces nil value with value from the right-side of the operator.
/// - Parameters:
/// - lhs: Binding with optional value.
/// - rhs: Value returned when the binding has nil wrapped value.
/// - Returns: Binding with the same type returning value from right side of the operator.
/// The type of the values is the same and remains optional.
public static func ?? <T>(lhs: Self, rhs: Value) -> Binding<Value> where Value == T? {
Binding {
lhs.wrappedValue ?? rhs
Expand All @@ -10,6 +16,12 @@ extension Binding {
.transaction(lhs.transaction)
}

/// Coalesces nil value with value from the right-side of the operator.
/// - Parameters:
/// - lhs: Binding with optional value.
/// - rhs: Value returned when the binding has nil wrapped value.
/// - Returns: Binding with unwrapped value returning value from right side of the operator
/// instead of nil values.
public static func ?? <T>(lhs: Self, rhs: T) -> Binding<T> where Value == T? {
Binding<T> {
lhs.wrappedValue ?? rhs
Expand Down
6 changes: 6 additions & 0 deletions Sources/BindingKit/Binding+Effects.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import SwiftUI

extension Binding {
/// Executes side-effect every time after a new value is set to the wrapped value of the binding.
/// - Parameter effect: Function called every time after a new value is set to the binding.
/// - Returns: Binding of the same type with the same wrapped value.
public func didSet(effect: @escaping (Value) -> Void) -> Self {
Binding(
get: { self.wrappedValue },
Expand All @@ -12,6 +15,9 @@ extension Binding {
.transaction(transaction)
}

/// Executes side-effect every time before a new value is set to the wrapped value of the binding.
/// - Parameter effect: Function called every time before a new value is set to the binding.
/// - Returns: Binding of the same type with the same wrapped value.
public func willSet(effect: @escaping (Value) -> Void) -> Self {
Binding(
get: { self.wrappedValue },
Expand Down
4 changes: 4 additions & 0 deletions Sources/BindingKit/Binding+KeyPath.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import SwiftUI

extension Binding {
/// Initializes a binding from a reference type and a key path referring to one of its properties.
/// - Parameters:
/// - root: A reference type which has a writable key path.
/// - keyPath: A writable key path referencing one of the root type properties.
public init<Root>(_ root: Root, keyPath: ReferenceWritableKeyPath<Root, Value>) {
self.init {
root[keyPath: keyPath]
Expand Down
8 changes: 8 additions & 0 deletions Sources/BindingKit/Binding+Map.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import SwiftUI

extension Binding {
/// Maps to a new binding using two back and forth transformation closures.
/// - Parameters:
/// - forth: Closure which transforms the wrapped value forth when retrieved from the returned binding.
/// - back: Closure which transforms the wrapped value back when set to the returned binding.
/// - Returns: A new binding where the wrapped value is transformed using provided closures.
public func map<T>(forth: @escaping (Value) -> T, back: @escaping (T) -> Value) -> Binding<T> {
Binding<T>(
get: { forth(self.wrappedValue) },
Expand All @@ -11,6 +16,9 @@ extension Binding {
.transaction(transaction)
}

/// Maps to a new binding using two-way transformation.
/// - Parameter transformation: Two-way transformation.
/// - Returns: A new binding where the wrapped value is transformed using the provided two-way transformation.
public func map<T>(_ transformation: Transformation<Value, T>) -> Binding<T> {
map(forth: transformation.forth, back: transformation.back)
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/BindingKit/Binding+StoredValue.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import SwiftUI

extension Binding {
/// Initializes a binding which stores the provided value.
/// - Parameter storedValue: Value which will be stored and referenced in the Binding get and set closures.
public init(storedValue: Value) {
var innerValue = storedValue
self.init { innerValue }
Expand Down
7 changes: 7 additions & 0 deletions Sources/BindingKit/Transformation.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/// A two-way transformation from source to destination and back.
public struct Transformation<Source, Destination> {
/// Forward transformation from source to destination.
public let forth: (Source) -> Destination
/// Backward transformation from destination to source.
public let back: (Destination) -> Source

/// Initializes a transformation from provided back and forth closures.
/// - Parameters:
/// - forth: Forward transformation closure from source to destination.
/// - back: Backward transformation closure from destination to source.
public init(forth: @escaping (Source) -> Destination, back: @escaping (Destination) -> Source) {
self.forth = forth
self.back = back
Expand Down

0 comments on commit fe58851

Please sign in to comment.