Skip to content

Commit

Permalink
functions docs
Browse files Browse the repository at this point in the history
  • Loading branch information
icrowley committed Dec 23, 2014
1 parent cf0942e commit 6b1fdd8
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 20 deletions.
9 changes: 9 additions & 0 deletions addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package fake

import "strconv"

// Continent generates random continent
func Continent() string {
return lookup(lang, "continents", true)
}

// Country generates random country
func Country() string {
return lookup(lang, "countries", true)
}

// City generates random city
func City() string {
city := lookup(lang, "cities", true)
switch r.Intn(5) {
Expand All @@ -30,19 +33,23 @@ func citySuffix() string {
return lookup(lang, "city_suffixes", false)
}

// State generates random state
func State() string {
return lookup(lang, "states", false)
}

// StateAbbrev generates random state abbreviation
func StateAbbrev() string {
return lookup(lang, "state_abbrevs", false)
}

// Street generates random street name
func Street() string {
street := lookup(lang, "streets", true)
return join(street, streetSuffix())
}

// StreetAddress generates random street name along with building number
func StreetAddress() string {
return join(Street(), strconv.Itoa(r.Intn(100)))
}
Expand All @@ -51,10 +58,12 @@ func streetSuffix() string {
return lookup(lang, "street_suffixes", true)
}

// Zip generates random zip code using one of the formats specifies in zip_format file
func Zip() string {
return generate(lang, "zips", true)
}

// Phone generates random phone number using one of the formats format specified in phone_format file
func Phone() string {
return generate(lang, "phones", true)
}
5 changes: 4 additions & 1 deletion credit_cards.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var creditCards = map[string]creditCard{
"discover": {"Discover", 16, []int{6011}},
}

// CreditCardType returns one of the following credit values:
// VISA, MasterCard, American Express and Discover
func CreditCardType() string {
n := len(creditCards)
var vendors []string
Expand All @@ -29,12 +31,13 @@ func CreditCardType() string {
return vendors[r.Intn(n)]
}

// CreditCardNum generated credit card number according to the card number rules
func CreditCardNum(vendor string) string {
if vendor != "" {
vendor = strings.ToLower(vendor)
} else {
var vendors []string
for v, _ := range creditCards {
for v := range creditCards {
vendors = append(vendors, v)
}
vendor = vendors[r.Intn(len(vendors))]
Expand Down
2 changes: 2 additions & 0 deletions currencies.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package fake

// Currency generates currency name
func Currency() string {
return lookup(lang, "currencies", true)
}

// CurrencyCode generates currency code
func CurrencyCode() string {
return lookup(lang, "currency_codes", true)
}
18 changes: 11 additions & 7 deletions dates.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
package fake

import (
"time"
)

// Day generates day of the month
func Day() int {
return r.Intn(31) + 1
}

// WeekDay generates name ot the week day
func WeekDay() string {
return lookup(lang, "weekdays", true)
}

// WeekDayShort generates abbreviated name of the week day
func WeekDayShort() string {
return lookup(lang, "weekdays_short", true)
}

// WeekdayNum generates number of the day of the week
func WeekdayNum() int {
return r.Intn(7) + 1
}

// Month generates month name
func Month() string {
return lookup(lang, "months", true)
}

// MonthShort generates abbreviated month name
func MonthShort() string {
return lookup(lang, "months_short", true)
}

// MonthNum generates month number (from 1 to 12)
func MonthNum() int {
return r.Intn(12) + 1
}

func Year(fromOffset, toOffset int) int {
n := r.Intn(fromOffset+toOffset) + 1
return (time.Now().Year() - fromOffset) + n
// Year generates year using the given boundaries
func Year(from, to int) int {
n := r.Intn(to-from) + 1
return from + n
}
13 changes: 10 additions & 3 deletions fake.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package fake is the fake data generatror for go (Golang), heavily inspired by forgery and ffaker Ruby gems
package fake

import (
Expand All @@ -20,11 +21,13 @@ var enFallback = true
var availLangs = GetLangs()

var (
ErrInsufficientParams = fmt.Errorf("Insufficient params to lookup the samples")
ErrNoLanguageFn = func(lang string) error { return fmt.Errorf("The language passed (%s) is not available", lang) }
ErrNoSamplesFn = func(lang string) error { return fmt.Errorf("No samples found for language: %s", lang) }
// ErrNoLanguageFn is the error that indicates that given language is not available
ErrNoLanguageFn = func(lang string) error { return fmt.Errorf("The language passed (%s) is not available", lang) }
// ErrNoSamplesFn is the error that indicates that there are no samples for the given language
ErrNoSamplesFn = func(lang string) error { return fmt.Errorf("No samples found for language: %s", lang) }
)

// GetLangs returns a slice of available languages
func GetLangs() []string {
var langs []string
for k, v := range data {
Expand All @@ -35,6 +38,8 @@ func GetLangs() []string {
return langs
}

// SetLang sets the language in which the data should be generated
// returns error if passed language is not available
func SetLang(newLang string) error {
found := false
for _, l := range availLangs {
Expand All @@ -50,10 +55,12 @@ func SetLang(newLang string) error {
return nil
}

// UseExternalData sets the flag that allows using of external files as data providers (fake uses embedded ones by default)
func UseExternalData(flag bool) {
useExternalData = flag
}

// EnFallback sets the flag that allows fake to fallback to englsh samples if the ones for the used languaged are not available
func EnFallback(flag bool) {
enFallback = flag
}
Expand Down
11 changes: 10 additions & 1 deletion general.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,24 @@ func text(atLeast, atMost int, allowLower, allowUpper, allowNumeric, allowSpecia
return string(result)
}

// Password generates password with the length from atLeast to atMOst charachers,
// allow* parameters specify whether corresponding symbols can be used
func Password(atLeast, atMost int, allowUpper, allowNumeric, allowSpecial bool) string {
return text(atLeast, atMost, true, allowUpper, allowNumeric, allowSpecial)
}

// SimplePassword is a wrapper around Password,
// it generates password with the length from 6 to 12 symbols, with upper characters and numeric symbols allowed
func SimplePassword() string {
return Password(6, 12, true, true, false)
}

// Color generates color name
func Color() string {
return lookup(lang, "colors", true)
}

// DigitsN returns n digits as a string
func DigitsN(n int) string {
digits := make([]rune, n)
for i := 0; i < n; i++ {
Expand All @@ -49,6 +55,7 @@ func DigitsN(n int) string {
return string(digits)
}

// Digits returns from 1 to 5 digits as a string
func Digits() string {
return DigitsN(r.Intn(5) + 1)
}
Expand All @@ -61,10 +68,12 @@ func hexDigitsStr(n int) string {
return string(num)
}

// HexColor generates hex color name
func HexColor() string {
return hexDigitsStr(6)
}

func ShortHexColor() string {
// HexColorShort generates short hex color name
func HexColorShort() string {
return hexDigitsStr(3)
}
10 changes: 10 additions & 0 deletions geo.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
package fake

// Latitute generates latitude
func Latitute() float32 {
return r.Float32() * 180 / 90
}

// LatitudeDegress generates latitude degrees (from -180 to 180)
func LatitudeDegress() int {
return r.Intn(360) - 180
}

// LatitudeMinutes generates latitude minutes (from 0 to 60)
func LatitudeMinutes() int {
return r.Intn(60)
}

// LatitudeSeconds generates latitude seconds (from 0 to 60)
func LatitudeSeconds() int {
return r.Intn(60)
}

// LatitudeDirection generates latitude direction (N(orth) o S(outh))
func LatitudeDirection() string {
if r.Intn(2) == 0 {
return "N"
}
return "S"
}

// Longitude generates longitude
func Longitude() float32 {
return r.Float32()*360 - 180
}

// LongitudeDegrees generates longitude degrees (from -180 to 180)
func LongitudeDegrees() int {
return r.Intn(360) - 180
}

// LongitudeMinutes generates (from 0 to 60)
func LongitudeMinutes() int {
return r.Intn(60)
}

// LongitudeSeconds generates (from 0 to 60)
func LongitudeSeconds() int {
return r.Intn(60)
}

// LongitudeDirection generates (W(est) or E(ast))
func LongitudeDirection() string {
if r.Intn(2) == 0 {
return "W"
Expand Down
11 changes: 10 additions & 1 deletion internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"strings"
)

// UserName generates user name in one of the following forms
// first name + last name, letter + last names or concatenation of from 1 to 3 lowercased words
func UserName() string {
gender := randGender()
switch r.Intn(2) {
switch r.Intn(3) {
case 0:
return lookup("en", gender+"_first_names", false) + lookup(lang, gender+"_last_names", false)
case 1:
Expand All @@ -17,30 +19,37 @@ func UserName() string {
}
}

// TopLevelDomain generates random top level domain
func TopLevelDomain() string {
return lookup(lang, "top_level_domains", true)
}

// DomainName generates random domain name
func DomainName() string {
return Company() + "." + TopLevelDomain()
}

// EmailAddress generates email address
func EmailAddress() string {
return UserName() + "@" + DomainName()
}

// EmailSubject generates random email subject
func EmailSubject() string {
return Sentence()
}

// EmailBody generates random email body
func EmailBody() string {
return Paragraphs()
}

// DomainZone generates random domain zone
func DomainZone() string {
return lookup(lang, "domain_zones", true)
}

// IPv4 generates IPv4 address
func IPv4() string {
ip := make([]string, 4)
for i := 0; i < 4; i++ {
Expand Down
3 changes: 3 additions & 0 deletions jobs.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package fake

// Company generates company name
func Company() string {
return lookup(lang, "companies", true)
}

// JobTitle generates job title
func JobTitle() string {
job := lookup(lang, "jobs", true)
return join(job, jobTitleSuffix())
Expand All @@ -13,6 +15,7 @@ func jobTitleSuffix() string {
return lookup(lang, "jobs_suffixes", false)
}

// Industry generates industry name
func Industry() string {
return lookup(lang, "industries", true)
}
Loading

0 comments on commit 6b1fdd8

Please sign in to comment.