Skip to content

Commit

Permalink
Optional extensions (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
omaralbeik committed Apr 13, 2018
1 parent 8e6a8ac commit 0bb4f03
Show file tree
Hide file tree
Showing 20 changed files with 144 additions and 127 deletions.
2 changes: 0 additions & 2 deletions .swiftlint.yml
@@ -1,5 +1,3 @@
disabled_rules:
- line_length
- file_length
- legacy_constructor
- xctfail_message
23 changes: 15 additions & 8 deletions CHANGELOG.md
@@ -1,22 +1,29 @@
# CHANGELOG
The changelog for **SwifterSwift**. Also see the [releases](https://github.com/SwifterSwift/SwifterSwift/releases) on GitHub.

> # Upcoming release
>
> ### Added
# Upcoming release

### Added
- **UITableViewExtentions**:
- Added `isValidIndexPath(_ indexPath:)` method to check whether given IndexPath is valid within UITableView. [#441](https://github.com/SwifterSwift/SwifterSwift/pull/441) by [setoelkahfi](https://github.com/setoelkahfi).
> ### Changed
**Optional**:
- Added `isNilOrEmpty` property to check whether an optional is nil or empty collection.

### Changed
- **UITableViewExtentions**:
- `dequeueReusableCell(withClass:for)`, `dequeueReusableCell(withClass)` now return `UITableViewCell` object, `fatalError(...)` if not found. [#439](https://github.com/SwifterSwift/SwifterSwift/pull/439) by [jdisho](https://github.com/jdisho)
- `dequeueReusableHeaderFooterView(withClass)`now returns `UITableViewHeaderFooterView` object, `fatalError(...)` if not found. [#439](https://github.com/SwifterSwift/SwifterSwift/pull/439) by [jdisho](https://github.com/jdisho)
- **UICollectionView**:
- `dequeueReusableCell(withClass:for)` now return `UICollectionViewCell` object, `fatalError(...)` if not found. [#439](https://github.com/SwifterSwift/SwifterSwift/pull/439) by [jdisho](https://github.com/jdisho)
- `dequeueReusableSupplementaryView(ofKind:withClass:for)`now returns `UICollectionReusableView` object, `fatalError(...)` if not found. [#439](https://github.com/SwifterSwift/SwifterSwift/pull/439) by [jdisho](https://github.com/jdisho)
> ### Deprecated
> ### Removed
> ### Fixed
> ### Security

### Deprecated

### Removed

### Fixed

### Security

---

Expand Down
9 changes: 3 additions & 6 deletions Sources/Extensions/CoreGraphics/CGPointExtensions.swift
Expand Up @@ -80,9 +80,8 @@ public extension CGPoint {
/// - lhs: self
/// - rhs: CGPoint to add.
public static func += (lhs: inout CGPoint, rhs: CGPoint) {
// swiftlint:disable shorthand_operator
// swiftlint:disable next shorthand_operator
lhs = lhs + rhs
// swiftlint:enable shorthand_operator
}

/// SwifterSwift: Subtract two CGPoints.
Expand Down Expand Up @@ -111,9 +110,8 @@ public extension CGPoint {
/// - lhs: self
/// - rhs: CGPoint to subtract.
public static func -= (lhs: inout CGPoint, rhs: CGPoint) {
// swiftlint:disable shorthand_operator
// swiftlint:disable next shorthand_operator
lhs = lhs - rhs
// swiftlint:enable shorthand_operator
}

/// SwifterSwift: Multiply a CGPoint with a scalar
Expand Down Expand Up @@ -141,9 +139,8 @@ public extension CGPoint {
/// - scalar: scalar value.
/// - Returns: result of multiplication of the given CGPoint with the scalar.
public static func *= (point: inout CGPoint, scalar: CGFloat) {
// swiftlint:disable shorthand_operator
// swiftlint:disable next shorthand_operator
point = point * scalar
// swiftlint:enable shorthand_operator
}

/// SwifterSwift: Multiply a CGPoint with a scalar
Expand Down
12 changes: 4 additions & 8 deletions Sources/Extensions/Shared/ColorExtensions.swift
Expand Up @@ -32,7 +32,7 @@ public extension Color {
return Color(red: red, green: green, blue: blue)!
}

// swiftlint:disable large_tuple
// swiftlint:disable next large_tuple
/// SwifterSwift: RGB components for a Color (between 0 and 255).
///
/// UIColor.red.rgbComponents.red -> 255
Expand All @@ -50,9 +50,8 @@ public extension Color {
let blue = components[2]
return (red: Int(red * 255.0), green: Int(green * 255.0), blue: Int(blue * 255.0))
}
// swiftlint:enable large_tuple

// swiftlint:disable large_tuple
// swiftlint:disable next large_tuple
/// SwifterSwift: RGB components for a Color represented as CGFloat numbers (between 0 and 1)
///
/// UIColor.red.rgbComponents.red -> 1.0
Expand All @@ -70,9 +69,8 @@ public extension Color {
let blue = components[2]
return (red: red, green: green, blue: blue)
}
// swiftlint:enable large_tuple

// swiftlint:disable large_tuple
// swiftlint:disable next large_tuple
/// SwifterSwift: Get components of hue, saturation, and brightness, and alpha (read-only).
public var hsbaComponents: (hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat) {
var hue: CGFloat = 0.0
Expand All @@ -83,7 +81,6 @@ public extension Color {
getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)
return (hue: hue, saturation: saturation, brightness: brightness, alpha: alpha)
}
// swiftlint:enable large_tuple

/// SwifterSwift: Hexadecimal value string (read-only).
public var hexString: String {
Expand Down Expand Up @@ -357,7 +354,7 @@ public extension Color {

}

// swiftlint:disable type_body_length
// swiftlint:disable next type_body_length
// MARK: - Social
public extension Color {

Expand Down Expand Up @@ -1805,4 +1802,3 @@ public extension Color {

}
#endif
// swiftlint:enable type_body_length
Expand Up @@ -48,7 +48,7 @@ extension String {
return count
}

// swiftlint:disable identifier_name
// swiftlint:disable next identifier_name
/// SwifterSwift: Sliced string from a start index.
///
/// "Hello World".slicing(at: 6) -> "World"
Expand All @@ -62,7 +62,6 @@ extension String {
}
return self[safe: i..<count]
}
// swiftlint:enable identifier_name

/// SwifterSwift: Sliced string from a start index to an end index.
///
Expand Down
3 changes: 1 addition & 2 deletions Sources/Extensions/SwiftStdlib/DoubleExtensions.swift
Expand Up @@ -47,7 +47,7 @@ public func ** (lhs: Double, rhs: Double) -> Double {
return pow(lhs, rhs)
}

// swiftlint:disable identifier_name
// swiftlint:disable next identifier_name
prefix operator
/// SwifterSwift: Square root of double.
///
Expand All @@ -57,4 +57,3 @@ public prefix func √ (double: Double) -> Double {
// http://nshipster.com/swift-operators/
return sqrt(double)
}
// swiftlint:enable identifier_name
3 changes: 1 addition & 2 deletions Sources/Extensions/SwiftStdlib/FloatExtensions.swift
Expand Up @@ -47,7 +47,7 @@ public func ** (lhs: Float, rhs: Float) -> Float {
return pow(lhs, rhs)
}

// swiftlint:disable identifier_name
// swiftlint:disable next identifier_name
prefix operator
/// SwifterSwift: Square root of float.
///
Expand All @@ -57,4 +57,3 @@ public prefix func √ (float: Float) -> Float {
// http://nshipster.com/swift-operators/
return sqrt(float)
}
// swiftlint:enable identifier_name
6 changes: 2 additions & 4 deletions Sources/Extensions/SwiftStdlib/FloatingPointExtensions.swift
Expand Up @@ -100,7 +100,7 @@ public extension FloatingPoint {

// MARK: - Operators

// swiftlint:disable identifier_name
// swiftlint:disable next identifier_name
infix operator ±
/// SwifterSwift: Tuple of plus-minus operation.
///
Expand All @@ -112,9 +112,8 @@ public func ±<T: FloatingPoint> (lhs: T, rhs: T) -> (T, T) {
// http://nshipster.com/swift-operators/
return (lhs + rhs, lhs - rhs)
}
// swiftlint:enable identifier_name

// swiftlint:disable identifier_name
// swiftlint:disable next identifier_name
prefix operator ±
/// SwifterSwift: Tuple of plus-minus operation.
///
Expand All @@ -124,4 +123,3 @@ public prefix func ±<T: FloatingPoint> (number: T) -> (T, T) {
// http://nshipster.com/swift-operators/
return 0 ± number
}
// swiftlint:enable identifier_name
12 changes: 4 additions & 8 deletions Sources/Extensions/SwiftStdlib/IntExtensions.swift
Expand Up @@ -165,12 +165,11 @@ public extension Int {
return romanValue
}

// swiftlint:disable identifier_name
// swiftlint:disable next identifier_name
/// SwifterSwift: Rounds to the closest multiple of n
public func roundToNearest(_ n: Int) -> Int {
return n == 0 ? self : Int(round(Double(self) / Double(n))) * n
}
// swiftlint:enable identifier_name

}

Expand Down Expand Up @@ -210,7 +209,7 @@ public func ** (lhs: Int, rhs: Int) -> Double {
return pow(Double(lhs), Double(rhs))
}

// swiftlint:disable identifier_name
// swiftlint:disable next identifier_name
prefix operator
/// SwifterSwift: Square root of integer.
///
Expand All @@ -220,9 +219,8 @@ public prefix func √ (int: Int) -> Double {
// http://nshipster.com/swift-operators/
return sqrt(Double(int))
}
// swiftlint:enable identifier_name

// swiftlint:disable identifier_name
// swiftlint:disable next identifier_name
infix operator ±
/// SwifterSwift: Tuple of plus-minus operation.
///
Expand All @@ -234,9 +232,8 @@ public func ± (lhs: Int, rhs: Int) -> (Int, Int) {
// http://nshipster.com/swift-operators/
return (lhs + rhs, lhs - rhs)
}
// swiftlint:enable identifier_name

// swiftlint:disable identifier_name
// swiftlint:disable next identifier_name
prefix operator ±
/// SwifterSwift: Tuple of plus-minus operation.
///
Expand All @@ -246,4 +243,3 @@ public prefix func ± (int: Int) -> (Int, Int) {
// http://nshipster.com/swift-operators/
return 0 ± int
}
// swiftlint:enable identifier_name
105 changes: 58 additions & 47 deletions Sources/Extensions/SwiftStdlib/OptionalExtensions.swift
Expand Up @@ -9,22 +9,22 @@
// MARK: - Methods
public extension Optional {

/// SwifterSwift: Get self of default value (if self is nil).
///
/// let foo: String? = nil
/// print(foo.unwrapped(or: "bar")) -> "bar"
///
/// let bar: String? = "bar"
/// print(bar.unwrapped(or: "foo")) -> "bar"
///
/// - Parameter defaultValue: default value to return if self is nil.
/// - Returns: self if not nil or default value if nil.
public func unwrapped(or defaultValue: Wrapped) -> Wrapped {
// http://www.russbishop.net/improving-optionals
return self ?? defaultValue
}
/// SwifterSwift: Get self of default value (if self is nil).
///
/// let foo: String? = nil
/// print(foo.unwrapped(or: "bar")) -> "bar"
///
/// let bar: String? = "bar"
/// print(bar.unwrapped(or: "foo")) -> "bar"
///
/// - Parameter defaultValue: default value to return if self is nil.
/// - Returns: self if not nil or default value if nil.
public func unwrapped(or defaultValue: Wrapped) -> Wrapped {
// http://www.russbishop.net/improving-optionals
return self ?? defaultValue
}

/// SwifterSwift: Gets the wrapped value of an optional. If the optional is `nil`, throw a custom error.
/// SwifterSwift: Gets the wrapped value of an optional. If the optional is `nil`, throw a custom error.
///
/// let foo: String? = nil
/// try print(foo.unwrapped(or: MyError.notFound)) -> error: MyError.notFound
Expand All @@ -40,39 +40,50 @@ public extension Optional {
return wrapped
}

/// SwifterSwift: Runs a block to Wrapped if not nil
///
/// let foo: String? = nil
/// foo.run { unwrappedFoo in
/// // block will never run sice foo is nill
/// print(unwrappedFoo)
/// }
///
/// let bar: String? = "bar"
/// bar.run { unwrappedBar in
/// // block will run sice bar is not nill
/// print(unwrappedBar) -> "bar"
/// }
///
/// - Parameter block: a block to run if self is not nil.
public func run(_ block: (Wrapped) -> Void) {
// http://www.russbishop.net/improving-optionals
_ = self.map(block)
}
/// SwifterSwift: Runs a block to Wrapped if not nil
///
/// let foo: String? = nil
/// foo.run { unwrappedFoo in
/// // block will never run sice foo is nill
/// print(unwrappedFoo)
/// }
///
/// let bar: String? = "bar"
/// bar.run { unwrappedBar in
/// // block will run sice bar is not nill
/// print(unwrappedBar) -> "bar"
/// }
///
/// - Parameter block: a block to run if self is not nil.
public func run(_ block: (Wrapped) -> Void) {
// http://www.russbishop.net/improving-optionals
_ = self.map(block)
}

/// SwifterSwift: Assign an optional value to a variable only if the value is not nil.
///
/// let someParameter: String? = nil
/// let parameters = [String:Any]() //Some parameters to be attached to a GET request
/// parameters[someKey] ??= someParameter //It won't be added to the parameters dict
///
/// - Parameters:
/// - lhs: Any?
/// - rhs: Any?
public static func ??= (lhs: inout Optional, rhs: Optional) {
guard let rhs = rhs else { return }
lhs = rhs
}

}

// MARK: - Methods (Collection)
public extension Optional where Wrapped: Collection {

/// SwifterSwift: Assign an optional value to a variable only if the value is not nil.
///
/// let someParameter: String? = nil
/// let parameters = [String:Any]() //Some parameters to be attached to a GET request
/// parameters[someKey] ??= someParameter //It won't be added to the parameters dict
///
/// - Parameters:
/// - lhs: Any?
/// - rhs: Any?
public static func ??= (lhs: inout Optional, rhs: Optional) {
guard let rhs = rhs else { return }
lhs = rhs
}
/// Check if optional is nil or empty collection.
public var isNilOrEmpty: Bool {
guard let collection = self else { return true }
return collection.isEmpty
}

}

Expand Down
6 changes: 2 additions & 4 deletions Sources/Extensions/SwiftStdlib/SignedIntegerExtensions.swift
Expand Up @@ -59,25 +59,23 @@ public extension SignedInteger {
// MARK: - Methods
public extension SignedInteger {

// swiftlint:disable identifier_name
// swiftlint:disable next identifier_name
/// SwifterSwift: Greatest common divisor of integer value and n.
///
/// - Parameter n: integer value to find gcd with.
/// - Returns: greatest common divisor of self and n.
public func gcd(of n: Self) -> Self {
return n == 0 ? self : n.gcd(of: self % n)
}
// swiftlint:enable identifier_name

// swiftlint:disable identifier_name
// swiftlint:disable next identifier_name
/// SwifterSwift: Least common multiple of integer and n.
///
/// - Parameter n: integer value to find lcm with.
/// - Returns: least common multiple of self and n.
public func lcm(of n: Self) -> Self {
return (self * n).abs / gcd(of: n)
}
// swiftlint:enable identifier_name

#if canImport(Foundation)
@available(iOS 9.0, macOS 10.11, *)
Expand Down

0 comments on commit 0bb4f03

Please sign in to comment.