diff --git a/Example/SwiftArmyExampleTests/SwiftArmyExampleTimeTests.swift b/Example/SwiftArmyExampleTests/SwiftArmyExampleTimeTests.swift index 46aa09e..189b2a4 100644 --- a/Example/SwiftArmyExampleTests/SwiftArmyExampleTimeTests.swift +++ b/Example/SwiftArmyExampleTests/SwiftArmyExampleTimeTests.swift @@ -224,7 +224,7 @@ class SwiftArmyExampleTimeTests: XCTestCase { XCTAssertFalse(self.startDate!.isSameDayAs(expectedDate!), "Is same day method error") } - /*func testTimeIsSameWeek() { + func testTimeIsSameWeek() { var expectedDate = self.dateFormatter.dateFromString("2015-01-01 00:00:00") XCTAssertTrue(self.startDate!.isSameWeekAs(expectedDate!), "Is same week method error") @@ -248,7 +248,7 @@ class SwiftArmyExampleTimeTests: XCTestCase { expectedDate = self.dateFormatter.dateFromString("2015-10-10 12:00:00") XCTAssertFalse(self.startDate!.isSameWeekAs(expectedDate!), "Is same week method error") - }*/ + } func testPerformanceExample() { self.measureBlock() { diff --git a/README.md b/README.md index 588fc6b..3df9aff 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ A collection of Swift extensions ### Manual -Just clone and add all *.swift files in ```swift-army``` to your project. +Just clone and add all *.swift files in ```swift-army``` folder to your project. ### CocoaPods @@ -30,8 +30,22 @@ Just clone and add all *.swift files in ```swift-army``` to your project. Name | Signature ---- | --------- -**init** | `init(rgba: String)` -**init** | `init(rgb: Int, alpha: CGFloat)` +**init** | `init(rgbaString rgba: String)` +**init** | `init(rgbaUInt rgba: UInt)` + +### Double + +Name | Signature +---- | --------- +**ceil** | `ceil() -> Double` +**floor** | `floor() -> Double` + +### Float + +Name | Signature +---- | --------- +**ceil** | `ceil() -> Float` +**floor** | `floor() -> Float` ### Int @@ -50,13 +64,25 @@ Name | `seconds: NSTimeInterval` | `second: NSTimeInterval` | +#### Instance Methods + +Name | Signature +---- | --------- +**isEven** | `isEven() -> Bool` +**isOdd** | `isOdd() -> Bool` +**isPositive** | `isPositive() -> Bool` +**isNegative** | `isNegative() -> Bool` +**isZero** | `isZero() -> Bool` +**split** | `split() -> [Int]` +**abs** | `abs() -> Int` + ### String #### Properties Name | ---- | -`length` | +`length: Int` | #### Instance Methods @@ -68,6 +94,7 @@ Name | Signature **trim** | `trim() -> String` **trimLeft** | `trimLeft(set: NSCharacterSet) -> String` **trimRight** | `trimRight(set: NSCharacterSet) -> String` +**isValidEmail** | `isValidEmail() -> Bool` **toDouble** | `toDouble() -> Double?` **toFloat** | `toFloat() -> Float?` **toUInt** | `toUInt() -> UInt?` @@ -79,6 +106,21 @@ Name | Signature ### Time (NSDate) +#### Properties + +Name | +---- | +`seconds: Int` | +`minutes: Int` | +`hours: Int` | +`days: Int` | +`weekDay: Int` | +`weekMonth: Int` | +`month: Int` | +`year: Int` | + +#### Instance Methods + Name | Signature ---- | --------- **add** | `add(seconds: Int, minutes: Int, hours: Int, days: Int, weeks: Int, months: Int, years: Int) -> NSDate` diff --git a/swift-army/ColorUtil.swift b/swift-army/ColorUtil.swift index 926c0a9..e615664 100644 --- a/swift-army/ColorUtil.swift +++ b/swift-army/ColorUtil.swift @@ -10,7 +10,7 @@ import Foundation public extension UIColor { - convenience init(rgba: String) { + convenience init(rgbaString rgba: String) { var hex: String = rgba var red: CGFloat = 0.0 var green: CGFloat = 0.0 @@ -54,11 +54,18 @@ public extension UIColor { self.init(red:red, green:green, blue:blue, alpha:alpha) } - convenience init(rgb: Int, alpha: CGFloat = 1.0) { - let red = CGFloat((rgb & 0xFF0000) >> 16) / 255.0 - let green = CGFloat((rgb & 0x00FF00) >> 8) / 255.0 - let blue = CGFloat(rgb & 0x0000FF) / 255.0 + convenience init(rgbaUInt rgba: UInt) { + var colorStr = String(format: "%0X", rgba) + if rgba <= 0xFFF { + colorStr = String(format: "%03X", rgba) + } else if rgba <= 0xFFFF { + colorStr = String(format: "%04X", rgba) + } else if rgba <= 0xFFFFFF { + colorStr = String(format: "%06X", rgba) + } else if rgba <= 0xFFFFFFFF { + colorStr = String(format: "%08X", rgba) + } - self.init(red:red, green:green, blue:blue, alpha:alpha) + self.init(rgbaString: colorStr) } } \ No newline at end of file diff --git a/swift-army/DoubleUtil.swift b/swift-army/DoubleUtil.swift new file mode 100644 index 0000000..12c40ef --- /dev/null +++ b/swift-army/DoubleUtil.swift @@ -0,0 +1,20 @@ +// +// DoubleUtil.swift +// swift-army +// +// Created by Aditya Pradana S. on 5/28/15. +// Copyright (c) 2015 @apradanas. All rights reserved. +// + +import Foundation + +public extension Double { + + func ceil() -> Double { + return Foundation.ceil(self) + } + + func floor() -> Double { + return Foundation.floor(self) + } +} \ No newline at end of file diff --git a/swift-army/FloatUtil.swift b/swift-army/FloatUtil.swift new file mode 100644 index 0000000..176b61e --- /dev/null +++ b/swift-army/FloatUtil.swift @@ -0,0 +1,20 @@ +// +// FloatUtil.swift +// Pods +// +// Created by Aditya Pradana S. on 5/28/15. +// Copyright (c) 2015 @apradanas. All rights reserved. +// + +import Foundation + +public extension Float { + + func ceil() -> Float { + return ceilf(self) + } + + func floor() -> Float { + return floorf(self) + } +} \ No newline at end of file diff --git a/swift-army/IntUtil.swift b/swift-army/IntUtil.swift index 74ea0ed..305e388 100644 --- a/swift-army/IntUtil.swift +++ b/swift-army/IntUtil.swift @@ -10,6 +10,8 @@ import Foundation public extension Int { + // MARK: - Properties + var years: NSTimeInterval { return 365 * self.days } @@ -49,4 +51,40 @@ public extension Int { var second: NSTimeInterval { return self.seconds } + + // MARK: - Validation + + func isEven() -> Bool { + return (self % 2) == 0 + } + + func isOdd() -> Bool { + return !isEven() + } + + func isPositive() -> Bool { + return self > 0 + } + + func isNegative() -> Bool { + return !isPositive() + } + + // MARK: - Instance Methods + + func split() -> [Int] { + var result = [Int]() + + for char in String(self) { + let str = String(char) + if let int = str.toInt() { + result.append(int) + } + } + return result + } + + func abs() -> Int { + return Swift.abs(self) + } } \ No newline at end of file diff --git a/swift-army/StringUtil.swift b/swift-army/StringUtil.swift index 1d2460b..163c52f 100644 --- a/swift-army/StringUtil.swift +++ b/swift-army/StringUtil.swift @@ -10,8 +10,12 @@ import Foundation public extension String { + // MARK: - Properties + var length: Int { return count(self) } + // MARK: - Instance Methods + func equals(to: String) -> Bool { return self == to } @@ -47,6 +51,16 @@ public extension String { return trimLeft().trimRight() } + // MARK: - Validation + + func isValidEmail() -> Bool { + let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" + var emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegEx) + return emailTest.evaluateWithObject(self) + } + + // MARK: - Conversion + func toDouble() -> Double? { let scanner = NSScanner(string: self) var double: Double = 0 @@ -103,7 +117,7 @@ public extension String { let dateFormat = NSDateFormatter() dateFormat.dateFormat = from if let date = dateFormat.dateFromString(self.trim()) { - return date.toString(to) + return date.toString(format: to) } return nil } @@ -112,7 +126,7 @@ public extension String { let dateFormat = NSDateFormatter() dateFormat.dateFormat = from if let date = dateFormat.dateFromString(self.trim()) { - return date.toString(to, locale: locale) + return date.toStringWithLocale(format: to, locale: locale) } return nil } diff --git a/swift-army/TimeUtil.swift b/swift-army/TimeUtil.swift index 9b564f1..73383eb 100644 --- a/swift-army/TimeUtil.swift +++ b/swift-army/TimeUtil.swift @@ -10,6 +10,42 @@ import Foundation public extension NSDate { + // MARK: - Properties + + var seconds: Int { + return NSCalendar.currentCalendar().component(.CalendarUnitSecond, fromDate: self) + } + + var minutes: Int { + return NSCalendar.currentCalendar().component(.CalendarUnitMinute, fromDate: self) + } + + var hours: Int { + return NSCalendar.currentCalendar().component(.CalendarUnitHour, fromDate: self) + } + + var days: Int { + return NSCalendar.currentCalendar().component(.CalendarUnitDay, fromDate: self) + } + + var weekDay: Int { + return NSCalendar.currentCalendar().component(.CalendarUnitWeekday, fromDate: self) + } + + var weekMonth: Int { + return NSCalendar.currentCalendar().component(.CalendarUnitWeekOfMonth, fromDate: self) + } + + var month: Int { + return NSCalendar.currentCalendar().component(.CalendarUnitMonth, fromDate: self) + } + + var year: Int { + return NSCalendar.currentCalendar().component(.CalendarUnitYear, fromDate: self) + } + + // MARK: - Instance Methods + func add(seconds: Int = 0, minutes: Int = 0, hours: Int = 0, days: Int = 0, weeks: Int = 0, months: Int = 0, years: Int = 0) -> NSDate { var calendar = NSCalendar.currentCalendar() @@ -140,6 +176,8 @@ public extension NSDate { return NSCalendar.currentCalendar().components(.CalendarUnitYear, fromDate: self, toDate: date, options: nil).year } + // MARK: - Validation + func isAfter(date: NSDate) -> Bool { return (self.compare(date) == NSComparisonResult.OrderedDescending) } @@ -169,6 +207,8 @@ public extension NSDate { return lhs.yearForWeekOfYear == rhs.yearForWeekOfYear && lhs.weekOfYear == rhs.weekOfYear } + // MARK: - Conversion + func toLocalTime() -> NSDate { let seconds = NSTimeZone.localTimeZone().secondsFromGMTForDate(self) return self.addSeconds(seconds) @@ -179,13 +219,13 @@ public extension NSDate { return self.addSeconds(seconds) } - func toString(format: String) -> String { + func toString(format: String = "yyyy-MM-dd HH:mm:ss") -> String { let dateFormat = NSDateFormatter() dateFormat.dateFormat = format return dateFormat.stringFromDate(self) } - func toString(format: String, locale: String) -> String { + func toStringWithLocale(format: String = "yyyy-MM-dd HH:mm:ss", locale: String = "en_US") -> String { let templateDateFormat = NSDateFormatter.dateFormatFromTemplate(format, options: 0, locale: NSLocale(localeIdentifier: locale)