Skip to content

Commit e4c26be

Browse files
committed
person - added age
1 parent dcf08ae commit e4c26be

File tree

3 files changed

+96
-42
lines changed

3 files changed

+96
-42
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ FirstName() string
427427
MiddleName() string
428428
LastName() string
429429
Gender() string
430+
Age() int
430431
SSN() string
431432
EIN() string
432433
Hobby() string

person.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type PersonInfo struct {
1111
FirstName string `json:"first_name" xml:"first_name"`
1212
LastName string `json:"last_name" xml:"last_name"`
1313
Gender string `json:"gender" xml:"gender"`
14+
Age int `json:"age" xml:"age"`
1415
SSN string `json:"ssn" xml:"ssn"`
1516
Hobby string `json:"hobby" xml:"hobby"`
1617
Job *JobInfo `json:"job" xml:"job"`
@@ -30,6 +31,7 @@ func person(f *Faker) *PersonInfo {
3031
FirstName: firstName(f),
3132
LastName: lastName(f),
3233
Gender: gender(f),
34+
Age: age(f),
3335
SSN: ssn(f),
3436
Hobby: hobby(f),
3537
Job: job(f),
@@ -89,6 +91,14 @@ func (f *Faker) NameSuffix() string { return nameSuffix(f) }
8991

9092
func nameSuffix(f *Faker) string { return getRandValue(f, []string{"person", "suffix"}) }
9193

94+
// Age will generate a random age between 0 and 100
95+
func Age() int { return age(GlobalFaker) }
96+
97+
// Age will generate a random age between 0 and 100
98+
func (f *Faker) Age() int { return age(f) }
99+
100+
func age(f *Faker) int { return randIntRange(f, 0, 100) }
101+
92102
// SSN will generate a random Social Security Number
93103
func SSN() string { return ssn(GlobalFaker) }
94104

@@ -270,6 +280,7 @@ func addPersonLookup() {
270280
"first_name": "Markus",
271281
"last_name": "Moen",
272282
"gender": "male",
283+
"age": 30,
273284
"ssn": "275413589",
274285
"image": "https://picsum.photos/208/500",
275286
"hobby": "Lacrosse",
@@ -450,7 +461,7 @@ func addPersonLookup() {
450461
Generate: func(f *Faker, m *MapParams, info *Info) (any, error) { return lastName(f), nil },
451462
})
452463

453-
// gender (keep terms neutral and search-friendly)
464+
// gender
454465
AddFuncLookup("gender", Info{
455466
Display: "Gender",
456467
Category: "person",
@@ -471,7 +482,25 @@ func addPersonLookup() {
471482
Generate: func(f *Faker, m *MapParams, info *Info) (any, error) { return gender(f), nil },
472483
})
473484

474-
// ssn (us)
485+
// age
486+
AddFuncLookup("age", Info{
487+
Display: "Age",
488+
Category: "person",
489+
Description: "The number of years a person has lived",
490+
Example: "40",
491+
Output: "int",
492+
Aliases: []string{
493+
"age of person", "person age", "age of individual", "years old", "years of age",
494+
},
495+
Keywords: []string{
496+
"years", "old", "birthday", "birthdate", "birth-date",
497+
"lifespan", "maturity", "elderly", "young", "adult", "teenager", "child",
498+
"senior", "juvenile", "minor", "majority", "minority", "generation",
499+
},
500+
Generate: func(f *Faker, m *MapParams, info *Info) (any, error) { return age(f), nil },
501+
})
502+
503+
// ssn
475504
AddFuncLookup("ssn", Info{
476505
Display: "SSN",
477506
Category: "person",

person_test.go

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,26 @@ func BenchmarkGender(b *testing.B) {
209209
}
210210
}
211211

212+
func ExampleAge() {
213+
Seed(11)
214+
fmt.Println(Age())
215+
216+
// Output: 90
217+
}
218+
219+
func ExampleFaker_Age() {
220+
f := New(11)
221+
fmt.Println(f.Age())
222+
223+
// Output: 90
224+
}
225+
226+
func BenchmarkAge(b *testing.B) {
227+
for i := 0; i < b.N; i++ {
228+
Age()
229+
}
230+
}
231+
212232
func ExampleHobby() {
213233
Seed(11)
214234
fmt.Println(Hobby())
@@ -322,6 +342,7 @@ func ExamplePerson() {
322342
fmt.Println(person.FirstName)
323343
fmt.Println(person.LastName)
324344
fmt.Println(person.Gender)
345+
fmt.Println(person.Age)
325346
fmt.Println(person.SSN)
326347
fmt.Println(person.Hobby)
327348

@@ -350,26 +371,27 @@ func ExamplePerson() {
350371
// Output: Sonny
351372
// Stiedemann
352373
// male
353-
// 280254464
354-
// Sailing
355-
// DataLogix
356-
// Planner
357-
// Human
358-
// Usability
359-
// 679 Underpassborough, Omaha, Massachusetts 37930
360-
// 679 Underpassborough
361-
// Omaha
362-
// Massachusetts
363-
// 37930
364-
// Antigua and Barbuda
365-
// 41.632289
366-
// -1.458116
367-
// 1026894059
368-
// beaulahmayert@kautzer.net
369-
// UnionPay
370-
// 627700709387573
371-
// 02/27
372-
// 162
374+
// 20
375+
// 575624882
376+
// Photography
377+
// Qado Energy, Inc.
378+
// Facilitator
379+
// Regional
380+
// Quality
381+
// 79993 Lanebury, Lincoln, Kansas 93050
382+
// 79993 Lanebury
383+
// Lincoln
384+
// Kansas
385+
// 93050
386+
// India
387+
// -26.936948
388+
// -28.374174
389+
// 2689405915
390+
// hopeprohaska@metz.io
391+
// American Express
392+
// 4570938757201747
393+
// 11/28
394+
// 205
373395
}
374396

375397
func ExampleFaker_Person() {
@@ -383,6 +405,7 @@ func ExampleFaker_Person() {
383405
fmt.Println(person.FirstName)
384406
fmt.Println(person.LastName)
385407
fmt.Println(person.Gender)
408+
fmt.Println(person.Age)
386409
fmt.Println(person.SSN)
387410
fmt.Println(person.Hobby)
388411

@@ -411,26 +434,27 @@ func ExampleFaker_Person() {
411434
// Output: Sonny
412435
// Stiedemann
413436
// male
414-
// 280254464
415-
// Sailing
416-
// DataLogix
417-
// Planner
418-
// Human
419-
// Usability
420-
// 679 Underpassborough, Omaha, Massachusetts 37930
421-
// 679 Underpassborough
422-
// Omaha
423-
// Massachusetts
424-
// 37930
425-
// Antigua and Barbuda
426-
// 41.632289
427-
// -1.458116
428-
// 1026894059
429-
// beaulahmayert@kautzer.net
430-
// UnionPay
431-
// 627700709387573
432-
// 02/27
433-
// 162
437+
// 20
438+
// 575624882
439+
// Photography
440+
// Qado Energy, Inc.
441+
// Facilitator
442+
// Regional
443+
// Quality
444+
// 79993 Lanebury, Lincoln, Kansas 93050
445+
// 79993 Lanebury
446+
// Lincoln
447+
// Kansas
448+
// 93050
449+
// India
450+
// -26.936948
451+
// -28.374174
452+
// 2689405915
453+
// hopeprohaska@metz.io
454+
// American Express
455+
// 4570938757201747
456+
// 11/28
457+
// 205
434458
}
435459

436460
func BenchmarkPerson(b *testing.B) {

0 commit comments

Comments
 (0)