Skip to content

Commit

Permalink
Merge pull request #65 from YusukeHosonuma/feature/operator-syntax
Browse files Browse the repository at this point in the history
Welcome: operator-based API
  • Loading branch information
YusukeHosonuma committed Mar 8, 2020
2 parents a192c7f + a8e3fff commit 3d91889
Show file tree
Hide file tree
Showing 5 changed files with 368 additions and 266 deletions.
34 changes: 28 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,36 @@ Debug.prettyPrint(["Hello", "World"], option: Debug.Option(indent: 2))
// ]
```

## Alias API - `p()` and `pp()`
## Operator-based API

You can use `p()` and `pp()` that like Ruby's API.
You can use operator based alias API that like Ruby.

- `p(_:, debug: Bool = false)`
- Equatable to `print()` and `debugPrint()`
- `pp(_:, debug: Bool = false)`
- Equatable to `prettyPrint()` and `debugPrettyPrint()`
This is no need to enclose in parenthese that convenient to long expression.

```swift
Debug.p >>> 42
// => 42

Debug.p >>> 42 + 1 // It can also be applied to expression
// => 43

Debug.p >>> String(string.reversed()).hasSuffix("eH")
// => true

Debug.pp >>> ["Hello", "World"]
// =>
// [
// "Hello",
// "World"
// ]
```

| Operator syntax | Equatable to |
|--------------------|------------------------------|
| `Debug.p >>> 42` | `Debug.print(42)` |
| `Debug.pp >>> 42` | `Debug.prettyPrint(42)` |
| `Debug.pd >>> 42` | `Debug.debugPrint(42)` |
| `Debug.ppd >>> 42` | `Debug.debugPrettyPrint(42)` |

## Develoopment

Expand Down
34 changes: 0 additions & 34 deletions Sources/API/Debug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,3 @@ extension Debug {
}
}
}

// MARK: Alias API

extension Debug {
/// Alias to `print()` and `debugPrint()`
/// - Parameters:
/// - target: target
/// - debug: debuggable output if `true` (default: `false`)
/// - Returns: String that is the same as output console.
@discardableResult
public static func p<T>(_ target: T, debug: Bool = false) -> String {
// Note: `option` is meaningless in `not-pretty` print currently.
if debug {
return debugPrint(target)
} else {
return print(target)
}
}

/// Alias to `prettyPrint()` and `debugPrettyPrint()`
/// - Parameters:
/// - target: target
/// - debug: debuggable output if `true` (default: `false`)
/// - option: option (default: `Debug.sharedOption)`
/// - Returns: String that is the same as output console.
@discardableResult
public static func pp<T>(_ target: T, debug: Bool = false, option: Option = Debug.sharedOption) -> String {
if debug {
return debugPrettyPrint(target, option: option)
} else {
return prettyPrint(target, option: option)
}
}
}
68 changes: 68 additions & 0 deletions Sources/API/Operator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Operator.swift
// SwiftPrettyPrint
//
// Created by Yusuke Hosonuma on 2020/03/06.
//

// ----------------------------------------------------------------------------
//
// Provide a simplicity description for print.
//
// No need to enclose in parenthese unlike `print()` or `debugPrint()`.
//
// For example:
//
// ```
// let string = "Hello, world."
// string.hasPrefix("Hello")
//
// // when use `print()` - needs enclose in parenthese
// Debug.print(string.hasPrefix("Hello"))
//
// // when use `p` - not needs enclose in parenthese
// Debug.p >>> string.hasPrefix("Hello")
// ```
//
// Also the operator `>>>` has lower precedence than all standard operators in Swift,
// Therefore could be a apply to expression too.
//
// ----------------------------------------------------------------------------

extension Debug {
public struct P {}
public struct PP {}
public struct PD {}
public struct PPD {}

public static var p: P { P() }
public static var pp: PP { PP() }
public static var pd: PD { PD() }
public static var ppd: PPD { PPD() }
}

precedencegroup PrintPrecedence {
lowerThan: FunctionArrowPrecedence
}

infix operator >>>: PrintPrecedence

@discardableResult
public func >>> (_: Debug.P, target: Any) -> String {
Debug.print(target)
}

@discardableResult
public func >>> (_: Debug.PD, target: Any) -> String {
Debug.debugPrint(target)
}

@discardableResult
public func >>> (_: Debug.PP, target: Any) -> String {
Debug.prettyPrint(target)
}

@discardableResult
public func >>> (_: Debug.PPD, target: Any) -> String {
Debug.debugPrettyPrint(target)
}
Loading

0 comments on commit 3d91889

Please sign in to comment.