All files / generator PersonGenerator.ts

66.66% Statements 26/39
52.17% Branches 12/23
100% Functions 2/2
66.66% Lines 26/39

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 80 81 82 83 84 85 86 87 88 89 90 91          9x       109x   109x 109x 109x 109x 109x 109x 109x   109x 104x 104x 312x         104x 104x   104x 104x   104x 104x                     104x       5x 1x       1x                           4x 1x                   3x                      
import { BaseGenerator, type GeneratorOptions } from './BaseGenerator'
import type { PersonOptions } from '../schema/data'
 
export default class PersonGenerator extends BaseGenerator {
  constructor(opts: GeneratorOptions) {
    super(opts)
  }
 
  generate(options: Partial<PersonOptions> = {}) {
    const g = this.generator
 
    const sex = g.person.sex() as 'male' | 'female'
    const prefix = g.person.prefix(sex)
    const firstName = g.person.firstName(sex)
    const lastName = g.person.lastName(sex)
    const email = g.internet.email({ firstName, lastName })
    const phone = g.phone.number()
    const address = g.location.streetAddress()
 
    if (options.fields && options.fields.length > 0) {
      const result: Record<string, unknown> = {}
      for (const field of options.fields) {
        switch (field) {
        case 'prefix':
          result.prefix = prefix
          break
        case 'firstName':
          result.firstName = firstName
          break
        case 'lastName':
          result.lastName = lastName
          break
        case 'email':
          result.email = email
          break
        case 'phone':
          result.phone = phone
          break
        case 'address':
          result.address = address
          break
        default:
          break
        }
      }
      return result
    }
 
    // Single field generation
    if (options.value) {
      switch (options.value) {
      case 'prefix':
        return prefix
      case 'firstName':
        return firstName
      case 'lastName':
        return lastName
      case 'email':
        return email
      case 'phone':
        return phone
      case 'address':
        return address
      default:
        return null
      }
    }
 
    if (options.pattern) {
      return options.pattern
        .replace(/\{prefix\}/g, prefix)
        .replace(/\{firstName\}/g, firstName)
        .replace(/\{lastName\}/g, lastName)
        .replace(/\{email\}/g, email)
        .replace(/\{phone\}/g, phone)
        .replace(/\{address\}/g, address)
    }
 
    // Default: return full person object
    return {
      prefix,
      firstName,
      lastName,
      email,
      phone,
      address,
    }
 
  }
}