All files / src/generators PersonGenerator.ts

80% Statements 12/15
91.66% Branches 11/12
60% Functions 3/5
80% Lines 12/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                                8x 8x   8x 8x 8x 8x 8x 8x       8x                         22x     22x               1x                                    
import type { Faker } from '@faker-js/faker'
import LocationGenerator from './LocationGenerator'
 
export type PersonGeneratorOptions = {
    firstName?: string
    lastName?: string
    middleName?: string
    prefix?: string
    sex?: 'male' | 'female'
}
 
export default class PersonGenerator {
    public sex: 'male' | 'female'
    public age: number
    private generator: Faker
    private firstName: string
    private lastName: string
    private prefix: string
    private middleName: string
    private email: string
    private phone: string
    private location: LocationGenerator
 
    constructor (generator: Faker,opts: PersonGeneratorOptions) {
        this.generator = generator
        this.location = new LocationGenerator(this.generator)
 
        this.sex = opts.sex ?? this.generator.person.sex() as 'male' | 'female'
        this.prefix = opts.prefix ?? this.generator.person.prefix(this.sex)
        this.middleName = opts.middleName ?? this.generator.person.middleName(this.sex)
        this.firstName = opts.firstName ?? this.generator.person.firstName(this.sex)
        this.lastName = opts.lastName ?? this.generator.person.lastName(this.sex)
        this.email = this.generator.internet.email({
            firstName: this.firstName,
            lastName: this.lastName
        })
        this.phone = this.generator.phone.number()
    }
 
    /**
     * Generates a string representation of the person.
     * - {prefix} - the prefix of the person
     * - {firstName} - the first name of the person
     * - {lastName} - the last name of the person
     * - {middleName} - the middle name of the person
     * - {email} - the email address of the person
     * - {address} - the address of the person
     */
    public getName(pattern?: string): string {
        Iif (!pattern) {
            return `${this.prefix} ${this.firstName} ${this.middleName} ${this.lastName}`
        }
        return pattern
            .replace('{prefix}', this.prefix)
            .replace('{firstName}', this.firstName)
            .replace('{lastName}', this.lastName)
            .replace('{middleName}', this.middleName)
    }
 
    public getEmail(): string {
        return this.email
    }
 
    public getPhone(): string {
        return this.phone
    }
 
    /**
     * Generates a string representation of the person's address.
     * - {street} - the street address
     * - {city} - the city
     * - {state} - the state
     * - {zip} - the zip code
     */
    public getAddress(pattern?: string): string {
        return this.location.toString(pattern)
    }
}