Skip to content

Commit

Permalink
Merge Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
thellimist committed Oct 28, 2015
2 parents 9d7076c + 4c1e348 commit dd2570f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 37 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ SwiftRandom is a tiny help suite for generating random data such as
```swift
func asExtension() {
Int.random(2, 77) // Random between 2-77
Int.random(2...77)
Double.random()
Float.random(3.2, 4.5) // Random between 3.2-4.5
CGFloat.random()
NSDate.random()
NSDate.randomWithinDaysBeforeToday(7)
UIColor.random()
NSURL.random()

// Array Extensions
var someArray = ["hello", "world"]
someArray[0..<someArray.count].randomItem() // Array slice
someArray.randomItem()
}
```

Expand Down Expand Up @@ -90,6 +96,17 @@ func asMethods() {
// Return random tag as string
// Example Output: "question"
Randoms.randomFakeTag()

// Return random (non-existing) gravatar as UIImage?
// The image is optional in case of network issues
Randoms.randomGravatar { (image, error) -> Void in
// Handle the image/error
}

// For consistance behaviour you can create custom Gravatar
Randoms.createGravatar(Randoms.GravatarStyle.Retro) { (image, error) -> Void in
// Handle the image/error
}
}

```
Expand Down Expand Up @@ -130,7 +147,6 @@ pod 'SwiftRandom'
- Feel free adding your own random data functions and sending pull requests.

#####Possible features:
- Random profile image (Should not include the image inside project, should load it from web when needed)
- Random wildlife pictures (Should not include the image inside project, should load it from web when needed)
- Make OSX compatiable and add here: https://github.com/AndrewSB/awesome-osx
- Random JSON
Expand All @@ -139,4 +155,4 @@ pod 'SwiftRandom'
- SwiftRandom is available under the MIT license. See the [LICENSE file](https://github.com/thellimist/SwiftRandom/blob/master/LICENSE).

##Keywords
random, swift, data, generator, faker, fake,
random, swift, data, generator, faker, fake, gravatar
71 changes: 36 additions & 35 deletions Randoms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public extension Int {
public static func random(range: Range<Int>) -> Int {
return range.startIndex + Int(arc4random_uniform(UInt32(range.endIndex - range.startIndex)))
}

/// SwiftRandom extension
public static func random(lower: Int = 0, _ upper: Int = 100) -> Int {
return lower + Int(arc4random_uniform(UInt32(upper - lower + 1)))
Expand Down Expand Up @@ -53,36 +53,36 @@ public extension NSDate {
/// SwiftRandom extension
public static func randomWithinDaysBeforeToday(days: Int) -> NSDate {
let today = NSDate()

guard let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 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(23))
let r4 = arc4random_uniform(UInt32(23))

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

guard let rndDate1 = gregorian.dateByAddingComponents(offsetComponents, toDate: today, options: []) else {
print("randoming failed")
return today
}
return rndDate1
}

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

}

public extension UIColor {
Expand Down Expand Up @@ -122,121 +122,121 @@ public extension NSURL {


public struct Randoms {

//==========================================================================================================
// MARK: - Object randoms
//==========================================================================================================

public static func randomBool() -> Bool {
return Bool.random()
}

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

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

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 randomFloat(lower: Float = 0, _ upper: Float = 100) -> Float {
return Float.random(lower, upper)
}

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

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

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

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

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

//==========================================================================================================
// MARK: - Fake random data generators
//==========================================================================================================

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()
}

public static func randomFakeGender() -> String {
return Bool.random() ? "Male" : "Female"
}

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()
}

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()
}

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

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

public static func randomFakeNameAndEnglishHonorific() -> String {
let englishHonorific = randomEnglishHonorific()
let name = randomFakeName()
return englishHonorific + " " + name
}

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()
}

public enum GravatarStyle: String {
case Standard
case MM
case Identicon
case MonsterID
case Wavatar
case Retro

static let allValues = [Standard, MM, Identicon, MonsterID, Wavatar, Retro]
}

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.lowercaseString)"
}

let request = NSURLRequest(URL: NSURL(string: url)!, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0)
let session = NSURLSession.sharedSession()

session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
dispatch_async(dispatch_get_main_queue()) {
if error == nil {
Expand All @@ -247,9 +247,10 @@ public struct Randoms {
}
}).resume()
}

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)
}
}
}

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>B537B0CA1BCA9E3E005725CE</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>

0 comments on commit dd2570f

Please sign in to comment.