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 { if (!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) } }