Skip to content

Commit

Permalink
Merge pull request #4 from apradanas/develop
Browse files Browse the repository at this point in the history
Add more test and mark
  • Loading branch information
apradanas committed May 29, 2015
2 parents 8303f1d + 0d608b7 commit 7adff89
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Example/SwiftArmyExampleTests/SwiftArmyExampleTimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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() {
Expand Down
50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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?`
Expand All @@ -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`
Expand Down
19 changes: 13 additions & 6 deletions swift-army/ColorUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
20 changes: 20 additions & 0 deletions swift-army/DoubleUtil.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
20 changes: 20 additions & 0 deletions swift-army/FloatUtil.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
38 changes: 38 additions & 0 deletions swift-army/IntUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Foundation

public extension Int {

// MARK: - Properties

var years: NSTimeInterval {
return 365 * self.days
}
Expand Down Expand Up @@ -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)
}
}
18 changes: 16 additions & 2 deletions swift-army/StringUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
44 changes: 42 additions & 2 deletions swift-army/TimeUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

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

0 comments on commit 7adff89

Please sign in to comment.