Skip to content

Commit

Permalink
Revert "Refactor for Swift 3 Release"
Browse files Browse the repository at this point in the history
This reverts commit 2c5614a.
  • Loading branch information
thellimist committed Oct 3, 2016
1 parent 7632a0a commit 832e63a
Showing 1 changed file with 58 additions and 55 deletions.
113 changes: 58 additions & 55 deletions Randoms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,88 +18,91 @@ public extension Bool {

public extension Int {
/// SwiftRandom extension
public static func random(range: Range<Int>) -> Int {
return random(range.upperBound, lower: range.lowerBound)
public static func random(_ range: Range<Int>) -> Int {
}

/// SwiftRandom extension
public static func random(_ lower: Int = 0, _ upper: Int = 100) -> Int {
public static func random(lower: Int = 0, _ upper: Int = 100) -> Int {
return lower + Int(arc4random_uniform(UInt32(upper - lower + 1)))
}
}

public extension Int32 {
/// SwiftRandom extension
public static func random(range: Range<Int>) -> Int32 {
return random(range.upperBound, lower: range.lowerBound)
public static func random(_ range: Range<Int>) -> Int32 {
}

/// SwiftRandom extension
///
/// - note: Using `Int` as parameter type as we usually just want to write `Int32.random(13, 37)` and not `Int32.random(Int32(13), Int32(37))`
public static func random(_ lower: Int = 0, _ upper: Int = 100) -> Int32 {
public static func random(lower: Int = 0, _ upper: Int = 100) -> Int32 {
let r = arc4random_uniform(UInt32(Int64(upper) - Int64(lower)))
return Int32(Int64(r) + Int64(lower))
}
}

public extension Double {
/// SwiftRandom extension
public static func random(_ lower: Double = 0, _ upper: Double = 100) -> Double {
public static func random(lower: Double = 0, _ upper: Double = 100) -> Double {
return (Double(arc4random()) / 0xFFFFFFFF) * (upper - lower) + lower
}
}

public extension Float {
/// SwiftRandom extension
public static func random(_ lower: Float = 0, _ upper: Float = 100) -> Float {
public static func random(lower: Float = 0, _ upper: Float = 100) -> Float {
return (Float(arc4random()) / 0xFFFFFFFF) * (upper - lower) + lower
}
}

public extension CGFloat {
/// SwiftRandom extension
public static func random(_ lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat {
public static func random(lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat {
return CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * (upper - lower) + lower
}
}

public extension Date {
public extension NSDate {
/// SwiftRandom extension
public static func randomWithinDaysBeforeToday(_ days: Int) -> Date {
let today = Date()
public static func randomWithinDaysBeforeToday(days: Int) -> NSDate {
let today = NSDate()

let gregorian = Calendar(identifier: Calendar.Identifier.gregorian)
guard let gregorian = Calendar(calendarIdentifier: Calendar.Identifier.gregorian) else {
print("no calendar \"NSCalendarIdentifierGregorian\" found")
return today
}

let r1 = arc4random_uniform(UInt32(days))
let r2 = arc4random_uniform(UInt32(23))
let r3 = arc4random_uniform(UInt32(59))
let r4 = arc4random_uniform(UInt32(59))

var offsetComponents = DateComponents()
let offsetComponents = NSDateComponents()
offsetComponents.day = Int(r1) * -1
offsetComponents.hour = Int(r2)
offsetComponents.minute = Int(r3)
offsetComponents.second = Int(r4)

guard let rndDate1 = gregorian.date(byAdding: offsetComponents, to: today) else {
guard let rndDate1 = gregorian.date(byAdding: offsetComponents as DateComponents, to: today as Date, options: []) else {
print("randoming failed")
return today
}
return rndDate1
}

/// SwiftRandom extension
public static func random() -> Date {
public static func random() -> NSDate {
let randomTime = TimeInterval(arc4random_uniform(UInt32.max))
return Date(timeIntervalSince1970: randomTime)
return NSDate(timeIntervalSince1970: randomTime)
}

}

public extension UIColor {
/// SwiftRandom extension
public static func random(_ randomAlpha: Bool = false) -> UIColor {
public static func random(randomAlpha: Bool = false) -> UIColor {
let randomRed = CGFloat.random()
let randomGreen = CGFloat.random()
let randomBlue = CGFloat.random()
Expand Down Expand Up @@ -132,11 +135,11 @@ public extension ArraySlice {
}
}

public extension URL {
public extension NSURL {
/// SwiftRandom extension
public static func random() -> URL {
public static func random() -> NSURL {
let urlList = ["http://www.google.com", "http://leagueoflegends.com/", "https://github.com/", "http://stackoverflow.com/", "https://medium.com/", "http://9gag.com/gag/6715049", "http://imgur.com/gallery/s9zoqs9", "https://www.youtube.com/watch?v=uelHwf8o7_U"]
return URL(string: urlList.randomItem()!)!
return NSURL(string: urlList.randomItem())!
}
}

Expand All @@ -151,52 +154,52 @@ public struct Randoms {
return Bool.random()
}

public static func randomInt(_ range: Range<Int>) -> Int {
return Int.random(range)
public static func randomInt(range: Range<Int>) -> Int {
return Int.random(range: range)
}

public static func randomInt(lower: Int = 0, _ upper: Int = 100) -> Int {
return Int.random(lower: lower, upper)
public static func randomInt(_ lower: Int = 0, _ upper: Int = 100) -> Int {
}

public static func randomInt32(_ range: Range<Int>) -> Int32 {
return Int32.random(range)
public static func randomInt32(range: Range<Int>) -> Int32 {
return Int32.random(range: range)
}

public static func randomInt32(lower: Int = 0, _ upper: Int = 100) -> Int32{
return Int32.random(lower, lower: upper)
public static func randomInt32(_ lower: Int = 0, _ upper: Int = 100) -> Int32{
}

public static func randomPercentageisOver(_ percentage: Int) -> Bool {
public static func randomPercentageisOver(percentage: Int) -> Bool {
return Int.random() > percentage
}

public static func randomDouble(_ lower: Double = 0, _ upper: Double = 100) -> Double {
return Double.random(lower, upper)
public static func randomDouble(lower: Double = 0, _ upper: Double = 100) -> Double {
return Double.random(lower, lower: upper)
}

public static func randomFloat(_ lower: Float = 0, _ upper: Float = 100) -> Float {
return Float.random(lower, upper)
public static func randomFloat(lower: Float = 0, _ upper: Float = 100) -> Float {
return Float.random(lower, lower: upper)
}

public static func randomCGFloat(_ lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat {
return CGFloat.random(lower, upper)
public static func randomCGFloat(lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat {
return CGFloat.random(lower, lower: upper)
}

public static func randomDateWithinDaysBeforeToday(_ days: Int) -> Date {
return Date.randomWithinDaysBeforeToday(days)
public static func randomDateWithinDaysBeforeToday(days: Int) -> NSDate {
return NSDate.randomWithinDaysBeforeToday(days: days)
}

public static func randomDate() -> Date {
return Date.random()
public static func randomDate() -> NSDate {
return NSDate.random()
}

public static func randomColor(_ randomAlpha: Bool = false) -> UIColor {
return UIColor.random(randomAlpha)
public static func randomColor(randomAlpha: Bool = false) -> UIColor {
return UIColor.random(randomAlpha: randomAlpha)
}

public static func randomNSURL() -> URL {
return URL.random()
public static func randomNSURL() -> NSURL {
return NSURL.random()
}

//==========================================================================================================
Expand All @@ -206,7 +209,7 @@ public struct Randoms {
public static func randomFakeName() -> String {
let firstNameList = ["Henry", "William", "Geoffrey", "Jim", "Yvonne", "Jamie", "Leticia", "Priscilla", "Sidney", "Nancy", "Edmund", "Bill", "Megan"]
let lastNameList = ["Pearson", "Adams", "Cole", "Francis", "Andrews", "Casey", "Gross", "Lane", "Thomas", "Patrick", "Strickland", "Nicolas", "Freeman"]
return firstNameList.randomItem()! + " " + lastNameList.randomItem()!
return firstNameList.randomItem() + " " + lastNameList.randomItem()
}

public static func randomFakeGender() -> String {
Expand All @@ -215,22 +218,22 @@ public struct Randoms {

public static func randomFakeConversation() -> String {
let convoList = ["You embarrassed me this evening.","You don't think that was just lemonade in your glass, do you?","Do you ever think we should just stop doing this?","Why didn't he come and talk to me himself?","Promise me you'll look after your mother.","If you get me his phone, I might reconsider.","I think the room is bugged.","No! I'm tired of doing what you say.","For some reason, I'm attracted to you."]
return convoList.randomItem()!
return convoList.randomItem()
}

public static func randomFakeTitle() -> String {
let titleList = ["CEO of Google", "CEO of Facebook", "VP of Marketing @Uber", "Business Developer at IBM", "Jungler @ Fanatic", "B2 Pilot @ USAF", "Student at Stanford", "Student at Harvard", "Mayor of Raccoon City", "CTO @ Umbrella Corporation", "Professor at Pallet Town University"]
return titleList.randomItem()!
return titleList.randomItem()
}

public static func randomFakeTag() -> String {
let tagList = ["meta", "forum", "troll", "meme", "question", "important", "like4like", "f4f"]
return tagList.randomItem()!
return tagList.randomItem()
}

fileprivate static func randomEnglishHonorific() -> String {
private static func randomEnglishHonorific() -> String {
let englishHonorificsList = ["Mr.", "Ms.", "Dr.", "Mrs.", "Mz.", "Mx.", "Prof."]
return englishHonorificsList.randomItem()!
return englishHonorificsList.randomItem()
}

public static func randomFakeNameAndEnglishHonorific() -> String {
Expand All @@ -242,13 +245,13 @@ public struct Randoms {
public static func randomFakeCity() -> String {
let cityPrefixes = ["North", "East", "West", "South", "New", "Lake", "Port"]
let citySuffixes = ["town", "ton", "land", "ville", "berg", "burgh", "borough", "bury", "view", "port", "mouth", "stad", "furt", "chester", "mouth", "fort", "haven", "side", "shire"]
return cityPrefixes.randomItem()! + citySuffixes.randomItem()!
return cityPrefixes.randomItem() + citySuffixes.randomItem()
}

public static func randomCurrency() -> String {
let currencyList = ["USD", "EUR", "GBP", "JPY", "AUD", "CAD", "ZAR", "NZD", "INR", "BRP", "CNY", "EGP", "KRW", "MXN", "SAR", "SGD",]

return currencyList.randomItem()!
return currencyList.randomItem()
}

public enum GravatarStyle: String {
Expand All @@ -262,29 +265,29 @@ public struct Randoms {
static let allValues = [Standard, MM, Identicon, MonsterID, Wavatar, Retro]
}

public static func createGravatar(_ style: Randoms.GravatarStyle = .Standard, size: Int = 80, completion: ((_ image: UIImage?, _ error: Error?) -> Void)?) {
public static func createGravatar(style: Randoms.GravatarStyle = .Standard, size: Int = 80, completion: ((image: UIImage?, error: NSError?) -> Void)?) {
var url = "https://secure.gravatar.com/avatar/thisimagewillnotbefound?s=\(size)"
if style != .Standard {
url += "&d=\(style.rawValue.lowercased())"
}

let request = URLRequest(url: URL(string: url)! as URL, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0)
let request = NSURLRequest(url: NSURL(string: url)! as URL, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0)
let session = URLSession.shared

session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in
DispatchQueue.main.async {
DispatchQueue.main.asynchronously() {
if error == nil {
completion?(UIImage(data: data!), nil)
completion?(image: UIImage(data: data!), error: nil)
} else {
completion?(nil, error)
completion?(image: nil, error: error)
}
}
}).resume()
}

public static func randomGravatar(_ size: Int = 80, completion: ((_ image: UIImage?, _ error: Error?) -> Void)?) {
public static func randomGravatar(size: Int = 80, completion: ((image: UIImage?, error: NSError?) -> Void)?) {
let options = Randoms.GravatarStyle.allValues
Randoms.createGravatar(options.randomItem()!, size: size, completion: completion)
Randoms.createGravatar(style: options.randomItem(), size: size, completion: completion)
}
}

0 comments on commit 832e63a

Please sign in to comment.