Skip to content
This repository was archived by the owner on Feb 2, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Codemine/Extensions/CGRect+Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should change this to reversedSize

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was reversedSize, but then this discussion happened #34 (comment). But I can change it back to reversedSize if you think it's better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed that. I guess it's fine then, if that's what Apple does. Also, why is that BezierPath thing a func and not a var?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're the native speaker @chriscombs, so if you think it should be different let's do it that way. Apple is being classic in not obeying their own rules :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right in that reversedSize implies that it returns the size and not the rect.

return CGRect(origin: origin, size: CGSize(width: height, height: width))
}
}
16 changes: 8 additions & 8 deletions Codemine/Extensions/NSURL+AssetSize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Codemine/Extensions/String+EmailValidation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions Codemine/Extensions/String+Range.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public extension String {

- returns: true if the string contains that range, false otherwise
*/
private func containsRange(_ range: Range<Index>) -> Bool {
private func contains(_ range: Range<Index>) -> Bool {
if range.lowerBound < self.startIndex || range.upperBound > self.endIndex {
return false
}
Expand All @@ -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<Index>? = nil) -> Range<Index>? {
public func range(from fromString: String, toString: String, searchType: RangeSearchType = .leftToRight, inRange: Range<Index>? = nil) -> Range<Index>? {
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 {
Expand Down
25 changes: 13 additions & 12 deletions Codemine/Extensions/UIImage+Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions Codemine/Extensions/UIView+Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(_ nibName:String) -> T? {
let view = UINib(nibName: nibName, bundle: nil).instantiate(withOwner: nil, options: nil).first as? T
public static func from<T>(nibWithName:String) -> T? {
let view = UINib(nibName: nibWithName, bundle: nil).instantiate(withOwner: nil, options: nil).first as? T
return view
}

Expand Down
20 changes: 10 additions & 10 deletions CodemineTests/CodemineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.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))


}
Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 3 additions & 3 deletions CodemineTests/UIImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

}
8 changes: 4 additions & 4 deletions CodemineTests/URLImageAssetSizeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

}
Expand All @@ -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")
}

Expand Down