diff --git a/.swiftlint.yml b/.swiftlint.yml index 965d20a52..6ca479806 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -1,5 +1,3 @@ disabled_rules: - line_length - file_length -- legacy_constructor -- xctfail_message diff --git a/CHANGELOG.md b/CHANGELOG.md index d789a3451..203bcfe43 100644 --- a/CHANGELOG.md +++ b/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 --- diff --git a/Sources/Extensions/CoreGraphics/CGPointExtensions.swift b/Sources/Extensions/CoreGraphics/CGPointExtensions.swift index d5784504d..7dceb5c63 100644 --- a/Sources/Extensions/CoreGraphics/CGPointExtensions.swift +++ b/Sources/Extensions/CoreGraphics/CGPointExtensions.swift @@ -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. @@ -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 @@ -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 diff --git a/Sources/Extensions/Shared/ColorExtensions.swift b/Sources/Extensions/Shared/ColorExtensions.swift index c91c6be66..99b805a9d 100644 --- a/Sources/Extensions/Shared/ColorExtensions.swift +++ b/Sources/Extensions/Shared/ColorExtensions.swift @@ -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 @@ -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 @@ -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 @@ -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 { @@ -357,7 +354,7 @@ public extension Color { } -// swiftlint:disable type_body_length +// swiftlint:disable next type_body_length // MARK: - Social public extension Color { @@ -1805,4 +1802,3 @@ public extension Color { } #endif -// swiftlint:enable type_body_length diff --git a/Sources/Extensions/SwiftStdlib/Deprecated/SwiftStdlibDeprecated.swift b/Sources/Extensions/SwiftStdlib/Deprecated/SwiftStdlibDeprecated.swift index dec402bc4..86cd98d8e 100644 --- a/Sources/Extensions/SwiftStdlib/Deprecated/SwiftStdlibDeprecated.swift +++ b/Sources/Extensions/SwiftStdlib/Deprecated/SwiftStdlibDeprecated.swift @@ -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" @@ -62,7 +62,6 @@ extension String { } return self[safe: i.. Double { return pow(lhs, rhs) } -// swiftlint:disable identifier_name +// swiftlint:disable next identifier_name prefix operator √ /// SwifterSwift: Square root of double. /// @@ -57,4 +57,3 @@ public prefix func √ (double: Double) -> Double { // http://nshipster.com/swift-operators/ return sqrt(double) } -// swiftlint:enable identifier_name diff --git a/Sources/Extensions/SwiftStdlib/FloatExtensions.swift b/Sources/Extensions/SwiftStdlib/FloatExtensions.swift index e72f1ee99..6f6ccef0d 100755 --- a/Sources/Extensions/SwiftStdlib/FloatExtensions.swift +++ b/Sources/Extensions/SwiftStdlib/FloatExtensions.swift @@ -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. /// @@ -57,4 +57,3 @@ public prefix func √ (float: Float) -> Float { // http://nshipster.com/swift-operators/ return sqrt(float) } -// swiftlint:enable identifier_name diff --git a/Sources/Extensions/SwiftStdlib/FloatingPointExtensions.swift b/Sources/Extensions/SwiftStdlib/FloatingPointExtensions.swift index 7ce0807bd..833ddc870 100644 --- a/Sources/Extensions/SwiftStdlib/FloatingPointExtensions.swift +++ b/Sources/Extensions/SwiftStdlib/FloatingPointExtensions.swift @@ -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. /// @@ -112,9 +112,8 @@ public func ± (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. /// @@ -124,4 +123,3 @@ public prefix func ± (number: T) -> (T, T) { // http://nshipster.com/swift-operators/ return 0 ± number } -// swiftlint:enable identifier_name diff --git a/Sources/Extensions/SwiftStdlib/IntExtensions.swift b/Sources/Extensions/SwiftStdlib/IntExtensions.swift index b41bcf249..89e44e38c 100755 --- a/Sources/Extensions/SwiftStdlib/IntExtensions.swift +++ b/Sources/Extensions/SwiftStdlib/IntExtensions.swift @@ -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 } @@ -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. /// @@ -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. /// @@ -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. /// @@ -246,4 +243,3 @@ public prefix func ± (int: Int) -> (Int, Int) { // http://nshipster.com/swift-operators/ return 0 ± int } -// swiftlint:enable identifier_name diff --git a/Sources/Extensions/SwiftStdlib/OptionalExtensions.swift b/Sources/Extensions/SwiftStdlib/OptionalExtensions.swift index f396229b9..3ed386197 100644 --- a/Sources/Extensions/SwiftStdlib/OptionalExtensions.swift +++ b/Sources/Extensions/SwiftStdlib/OptionalExtensions.swift @@ -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 @@ -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 + } } diff --git a/Sources/Extensions/SwiftStdlib/SignedIntegerExtensions.swift b/Sources/Extensions/SwiftStdlib/SignedIntegerExtensions.swift index cfa760fb1..eff9bfe8d 100644 --- a/Sources/Extensions/SwiftStdlib/SignedIntegerExtensions.swift +++ b/Sources/Extensions/SwiftStdlib/SignedIntegerExtensions.swift @@ -59,7 +59,7 @@ 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. @@ -67,9 +67,8 @@ public extension SignedInteger { 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. @@ -77,7 +76,6 @@ public extension SignedInteger { 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, *) diff --git a/Sources/Extensions/SwiftStdlib/SignedNumericExtensions.swift b/Sources/Extensions/SwiftStdlib/SignedNumericExtensions.swift index b3b3e6d7a..a9b1c1e6b 100644 --- a/Sources/Extensions/SwiftStdlib/SignedNumericExtensions.swift +++ b/Sources/Extensions/SwiftStdlib/SignedNumericExtensions.swift @@ -19,9 +19,8 @@ public extension SignedNumeric { let formatter = NumberFormatter() formatter.numberStyle = .currency formatter.locale = Locale.current - // swiftlint:disable force_cast + // swiftlint:disable next force_cast return formatter.string(from: self as! NSNumber) - // swiftlint:enable force_cast } } diff --git a/Sources/Extensions/SwiftStdlib/StringExtensions.swift b/Sources/Extensions/SwiftStdlib/StringExtensions.swift index 520ab8111..787d351d1 100755 --- a/Sources/Extensions/SwiftStdlib/StringExtensions.swift +++ b/Sources/Extensions/SwiftStdlib/StringExtensions.swift @@ -563,7 +563,7 @@ public extension String { } #endif - // swiftlint:disable identifier_name + // swiftlint:disable next identifier_name /// SwifterSwift: Safely subscript string with index. /// /// "Hello World!"[3] -> "l" @@ -574,7 +574,6 @@ public extension String { guard i >= 0 && i < count else { return nil } return self[index(startIndex, offsetBy: i)] } - // swiftlint:enable identifier_name /// SwifterSwift: Safely subscript string within a half-open range. /// @@ -736,7 +735,7 @@ public extension String { self = String(chars) } - // swiftlint:disable identifier_name + // swiftlint:disable next identifier_name /// SwifterSwift: Sliced string from a start index with length. /// /// "Hello World".slicing(from: 6, length: 5) -> "World" @@ -753,9 +752,8 @@ public extension String { guard length > 0 else { return "" } return self[safe: i..