diff --git a/Codemine/Extensions/CGRect+Utilities.swift b/Codemine/Extensions/CGRect+Utilities.swift index 2367438..8c11a92 100644 --- a/Codemine/Extensions/CGRect+Utilities.swift +++ b/Codemine/Extensions/CGRect+Utilities.swift @@ -35,7 +35,7 @@ public extension CGRect { - returns: a new CGRect with the width and height reversed to those of the current one */ - public func rectByReversingSize() -> CGRect { - return CGRect(origin: self.origin, size: CGSize(width: self.height, height: self.width)) + public var reversingSize: CGRect { + return CGRect(origin: origin, size: CGSize(width: height, height: width)) } } diff --git a/Codemine/Extensions/NSURL+AssetSize.swift b/Codemine/Extensions/NSURL+AssetSize.swift index 5252caf..ded1ccf 100644 --- a/Codemine/Extensions/NSURL+AssetSize.swift +++ b/Codemine/Extensions/NSURL+AssetSize.swift @@ -21,15 +21,15 @@ public extension URL { - Fit: Resizes the image to fit within the width and height boundaries without cropping or distorting the image. The resulting image is assured to match one of the constraining dimensions, while the other dimension is altered to maintain the same aspect ratio of the input image. - - Default: Default/normal image mode. No changes to the ratio. + - Standard: Default/normal image mode. No changes to the ratio. */ public enum ImageUrlMode : String { - case Resize = "resize" - case Crop = "crop" - case Fit = "fit" - case Default = "default" + case resize = "resize" + case crop = "crop" + case fit = "fit" + case `default` = "default" } - + /** Adds height, width and mode paramters to an url. To be used when fetching an image from a CDN, for example. Choose the `size` and the `mode` for the image url to define how an image will be provided from the backend. @@ -41,13 +41,13 @@ public extension URL { - widthParameterName: the name of the width paramter. Default is 'h' - Returns: `URL` as a `NSURL`. */ - public func urlByAppendingAssetSize(_ size: CGSize, mode: ImageUrlMode = .Default, heightParameterName : String = "h", widthParameterName : String = "w") -> URL? { + public func appendedAssetSize(_ size: CGSize, mode: ImageUrlMode = .default, heightParameterName : String = "h", widthParameterName : String = "w") -> URL? { guard var urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: false) else { return nil } var queryItems:[URLQueryItem] = urlComponents.queryItems ?? [] queryItems.append(URLQueryItem(name: widthParameterName, value: "\(Int(size.width * UIScreen.main.scale ))")) queryItems.append(URLQueryItem(name: heightParameterName, value: "\(Int(size.height * UIScreen.main.scale ))")) - if mode != .Default { + if mode != .default { queryItems.append(URLQueryItem(name: "mode", value: mode.rawValue)) } urlComponents.queryItems = queryItems diff --git a/Codemine/Extensions/String+EmailValidation.swift b/Codemine/Extensions/String+EmailValidation.swift index 5da3dc6..e4bf41e 100644 --- a/Codemine/Extensions/String+EmailValidation.swift +++ b/Codemine/Extensions/String+EmailValidation.swift @@ -16,7 +16,7 @@ public extension String { - returns: true is the current string is a valid email address, false otherwise */ - public func isValidEmailAddress() -> Bool { + public var isValidEmailAddress: Bool { let emailRegex = "\\A[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\\z" let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegex) diff --git a/Codemine/Extensions/String+Range.swift b/Codemine/Extensions/String+Range.swift index 81ecee0..bdb31b1 100644 --- a/Codemine/Extensions/String+Range.swift +++ b/Codemine/Extensions/String+Range.swift @@ -17,7 +17,7 @@ public extension String { - returns: true if the string contains that range, false otherwise */ - private func containsRange(_ range: Range) -> Bool { + private func contains(_ range: Range) -> Bool { if range.lowerBound < self.startIndex || range.upperBound > self.endIndex { return false } @@ -42,11 +42,11 @@ public extension String { - returns: the range between the start of the first substring and the end of the last substring */ - public func rangeFromString(_ string: String, toString: String, searchType: RangeSearchType = .leftToRight, inRange: Range? = nil) -> Range? { + public func range(from fromString: String, toString: String, searchType: RangeSearchType = .leftToRight, inRange: Range? = nil) -> Range? { let range = inRange ?? Range(uncheckedBounds: (lower: self.startIndex, upper: self.endIndex)) - if !containsRange(range) { return nil } + if !contains(range) { return nil } - guard let firstRange = self.range(of: string, options: NSString.CompareOptions(rawValue: 0), range: range, locale: nil) else { return nil } + guard let firstRange = self.range(of: fromString, options: NSString.CompareOptions(rawValue: 0), range: range, locale: nil) else { return nil } guard let secondRange = self.range(of: toString, options: NSString.CompareOptions(rawValue: 0), range: range, locale: nil) else { return nil } switch searchType { diff --git a/Codemine/Extensions/UIImage+Utilities.swift b/Codemine/Extensions/UIImage+Utilities.swift index 6cf38bb..90b6a57 100644 --- a/Codemine/Extensions/UIImage+Utilities.swift +++ b/Codemine/Extensions/UIImage+Utilities.swift @@ -24,7 +24,9 @@ public extension UIImage { - Returns: A 'UIImage' with the specified color, size and corner radius. */ - public class func imageFromColor(_ color: UIColor, size: CGSize, cornerRadius: CGFloat) -> UIImage? { + + convenience init(color: UIColor, size: CGSize, cornerRadius: CGFloat) { + self.init() /// The base rectangle of the image. let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) @@ -47,28 +49,28 @@ public extension UIImage { image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() - return image } + /** Embed an icon/image on top of a background image. - `imageOne` will be the background and `icon` is the image that will be on top of `imageOne`. - The `UIImage` that is set with the parameter `icon` will be centered on `imageOne`. + `image` will be the background and `icon` is the image that will be on top of `image`. + The `UIImage` that is set with the parameter `icon` will be centered on `image`. - Parameters: - - imageOne: The background image. - - icon: The embedded image that will be on top. + - icon: The embedded image that will be on top. + - image: The background image. - Returns: The combined image as `UIImage`. */ - public class func imageByEmbeddingIconIn(_ imageOne: UIImage, icon: UIImage) -> UIImage? { - let newSize = CGSize(width: imageOne.size.width, height: imageOne.size.height) + public class func embed(icon: UIImage, inImage image: UIImage ) -> UIImage? { + let newSize = CGSize(width: image.size.width, height: image.size.height) UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) - imageOne.draw(in: CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height)) + image.draw(in: CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height)) // Center icon - icon.draw(in: CGRect(x: imageOne.size.width/2 - icon.size.width/2, y: imageOne.size.height/2 - icon.size.height/2, width: icon.size.width, height: icon.size.height), blendMode:CGBlendMode.normal, alpha:1.0) + icon.draw(in: CGRect(x: image.size.width/2 - icon.size.width/2, y: image.size.height/2 - icon.size.height/2, width: icon.size.width, height: icon.size.height), blendMode:CGBlendMode.normal, alpha:1.0) let newImage = UIGraphicsGetImageFromCurrentImageContext() return newImage @@ -80,8 +82,7 @@ public extension UIImage { - Returns: The orientation corrected image as an `UIImage`. */ - public func rotationCorrectedImage() -> UIImage? { - // if (self.imageOrientation == UIImageOrientation.Up) { return self } + public var rotationCorrected: UIImage? { UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) self.draw(in: CGRect(origin: CGPoint.zero, size: self.size)) diff --git a/Codemine/Extensions/UIView+Utilities.swift b/Codemine/Extensions/UIView+Utilities.swift index 8361843..3aeda2e 100644 --- a/Codemine/Extensions/UIView+Utilities.swift +++ b/Codemine/Extensions/UIView+Utilities.swift @@ -14,11 +14,11 @@ public extension UIView { Assign a `nibName` to a UIView. Later on you can call this `UIView` by its `nibName`. - - Parameter nibName: The name that the UIView will get as its `nibName` assigned as a `String`. + - Parameter name: The name that the UIView will get as its `name` assigned as a `String`. - Returns: `Generics type`. */ - public static func viewWithNibNamed(_ nibName:String) -> T? { - let view = UINib(nibName: nibName, bundle: nil).instantiate(withOwner: nil, options: nil).first as? T + public static func from(nibWithName:String) -> T? { + let view = UINib(nibName: nibWithName, bundle: nil).instantiate(withOwner: nil, options: nil).first as? T return view } diff --git a/CodemineTests/CodemineTests.swift b/CodemineTests/CodemineTests.swift index 02a8fc3..175dcbc 100644 --- a/CodemineTests/CodemineTests.swift +++ b/CodemineTests/CodemineTests.swift @@ -38,29 +38,29 @@ class CodemineTests: XCTestCase { func testTrueEmailAddress() { let validEmails = ["TeSt@TesT.COM", "Test@test.com", "email@example.com", "firstname.lastname@example.com", "email@subdomain.example.com", "firstname+lastname@example.com", "1234567890@example.com", "email@example-one.com", "_______@example.com", "email@example.name", "email@example.museum", "email@example.co.jp", "firstname-lastname@example.com"] for emailAddress in validEmails { - XCTAssertTrue(emailAddress.isValidEmailAddress(), "the email address: \(emailAddress) was considered invalid, but it is not") + XCTAssertTrue(emailAddress.isValidEmailAddress, "the email address: \(emailAddress) was considered invalid, but it is not") } } func testFalseEmailAddress() { let invalidEmails = ["plainaddress", "#@%^%#$@#$@#.com", "@example.com", "Joe Smith ", "email.example.com", "email@example@example.com", ".email@example.com", "email.@example.com", "email..email@example.com", "あいうえお@example.com", "email@example.com (Joe Smith)", "email@example", "email@-example.com", "email@example..com", "Abc..123@example.com"] for emailAddress in invalidEmails { - XCTAssertFalse(emailAddress.isValidEmailAddress(), "the email address: \(emailAddress) was considered valid, but it is not") + XCTAssertFalse(emailAddress.isValidEmailAddress, "the email address: \(emailAddress) was considered valid, but it is not") } } func testRange() { let str = "Hello world!" - let range = str.rangeFromString("e", toString: " w") + let range = str.range(from: "e", toString: " w") XCTAssertTrue(range?.lowerBound == str.characters.index(str.startIndex, offsetBy: 1) && range?.upperBound == str.characters.index(str.startIndex, offsetBy: 7), "range = \(range)") - XCTAssertNil(str.rangeFromString("a", toString: "e")) - XCTAssertNil(str.rangeFromString("e", toString: "b")) - - XCTAssertNil(str.rangeFromString("l", toString: "o", searchType: .rightToLeft, inRange: range)) + XCTAssertNil(str.range(from: "a", toString: "e")) + XCTAssertNil(str.range(from: "e", toString: "b")) + + XCTAssertNil(str.range(from: "l", toString: "o", searchType: .rightToLeft, inRange: range)) let str2 = "abcdefghijklmnopqrstuvwxyz" - let range2 = str2.rangeFromString("x", toString: "z") - XCTAssertNil(str.rangeFromString("h", toString: "e", searchType: .leftToRight, inRange: range2)) + let range2 = str2.range(from: "x", toString: "z") + XCTAssertNil(str.range(from: "h", toString: "e", searchType: .leftToRight, inRange: range2)) } @@ -96,7 +96,7 @@ class CodemineTests: XCTestCase { func testReversingSize() { let rect = CGRect(x: 10, y: 10, width: 100, height: 200) - let reversedRect = rect.rectByReversingSize() + let reversedRect = rect.reversingSize XCTAssertTrue(rect.size.height == reversedRect.size.width && rect.size.width == reversedRect.size.height) } diff --git a/CodemineTests/UIImageTests.swift b/CodemineTests/UIImageTests.swift index f8ab579..be4ea93 100644 --- a/CodemineTests/UIImageTests.swift +++ b/CodemineTests/UIImageTests.swift @@ -23,15 +23,15 @@ class UIImageTests: XCTestCase { } func testUIImageFromColor() { - XCTAssertNotNil(UIImage.imageFromColor(.red, size: CGSize(width: 20, height: 20), cornerRadius: 1.0), "Failed to convert color to image") + XCTAssertNotNil(UIImage(color: .red, size: CGSize(width: 20, height: 20), cornerRadius: 1.0), "Failed to convert color to image") } func testImageImbed() { - XCTAssertNotNil(UIImage.imageByEmbeddingIconIn(testImageNamed(name: "add"), icon: testImageNamed(name: "alert")), "Failed to embed image") + XCTAssertNotNil(UIImage.embed(icon: testImageNamed(name: "alert"), inImage: testImageNamed(name: "add")), "Failed to embed image") } func testImageRotation() { - XCTAssertNotNil(testImageNamed(name: "add").rotationCorrectedImage(), "Failed to rotate image") + XCTAssertNotNil(testImageNamed(name: "add").rotationCorrected, "Failed to rotate image") } } diff --git a/CodemineTests/URLImageAssetSizeTests.swift b/CodemineTests/URLImageAssetSizeTests.swift index 057c5f5..d2cccaa 100644 --- a/CodemineTests/URLImageAssetSizeTests.swift +++ b/CodemineTests/URLImageAssetSizeTests.swift @@ -23,13 +23,13 @@ class URLImageAssetSizeTests: XCTestCase { let heightParameterName = "height" let widthParameterName = "width" - let url2 = url.urlByAppendingAssetSize(size, mode: .Default, heightParameterName: heightParameterName, widthParameterName: widthParameterName) + let url2 = url.appendedAssetSize(size, mode: .default, heightParameterName: heightParameterName, widthParameterName: widthParameterName) XCTAssertEqual(url2?.absoluteString, url.absoluteString + "?\(widthParameterName)=\(Int(size.width * UIScreen.main.scale ))&\(heightParameterName)=\(Int(size.height * UIScreen.main.scale))") - let url3 = url.urlByAppendingAssetSize(size) + let url3 = url.appendedAssetSize(size) XCTAssertEqual(url3?.absoluteString, url.absoluteString + "?w=\(Int(size.width * UIScreen.main.scale ))&h=\(Int(size.height * UIScreen.main.scale))") - let url4 = url.urlByAppendingAssetSize(size, mode: .Crop) + let url4 = url.appendedAssetSize(size, mode: .crop) XCTAssertEqual(url4?.absoluteString, url.absoluteString + "?w=\(Int(size.width * UIScreen.main.scale ))&h=\(Int(size.height * UIScreen.main.scale))&mode=crop") } @@ -39,7 +39,7 @@ class URLImageAssetSizeTests: XCTestCase { print(URLComponents(url: url, resolvingAgainstBaseURL: false)) - let newUrl = url.urlByAppendingAssetSize(CGSize(width: 10, height: 10)) + let newUrl = url.appendedAssetSize(CGSize(width: 10, height: 10)) XCTAssertNil(newUrl?.absoluteString, "Bad URL did not return nil") }