UNPKG

@italia-tools/faker

Version:

Italian-specific fake data generator based on Faker.js

192 lines (188 loc) 7.87 kB
'use strict'; var lastName_module = require('./lastName.module.cjs'); var firstName_module = require('./firstName.module.cjs'); var types = require('../types/types.cjs'); var places_module = require('./places.module.cjs'); var fiscalCode_module = require('./fiscalCode.module.cjs'); var addresses_module = require('./addresses.module.cjs'); var rxjs = require('rxjs'); var operators = require('rxjs/operators'); class PersonModule { constructor(faker) { this.faker = faker; this.lastNameModule = new lastName_module.LastNameModule(faker); this.namesModule = new firstName_module.FirstNameModule(faker); this.placesModule = new places_module.PlacesModule(faker); this.fiscalCodeModule = new fiscalCode_module.FiscalCodeModule(faker); this.addressModule = new addresses_module.AddressModule(faker); } firstName$(options) { return this.namesModule.firstName$(options); } lastName$(options) { return this.lastNameModule.lastName$(options); } fullName$(options) { return rxjs.forkJoin({ first: this.firstName$(options), last: this.lastName$() }).pipe(operators.map(({ first, last }) => `${first} ${last}`)); } prefix$(gender) { return this.namesModule.prefix$(gender); } fiscalCode$(options) { return this.fiscalCodeModule.generate$(options); } birthPlace$() { return this.placesModule.randomCity$().pipe(operators.map(city => city.name)); } province$() { return this.placesModule.province$(); } birthDate$(minAge, maxAge) { if (minAge !== undefined && maxAge !== undefined) { const currentYear = new Date().getFullYear(); const fromYear = currentYear - maxAge; const toYear = currentYear - minAge; const fromDate = new Date(`${fromYear}-01-01`); const toDate = new Date(`${toYear}-12-31`); const currentDate = new Date(); if (toYear === currentYear) { toDate.setFullYear(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()); } return rxjs.of(this.faker.date.between({ from: fromDate, to: toDate })); } return rxjs.of(this.faker.date.past()); } phone$() { return rxjs.of(this.generatePhone()); } landline$() { return rxjs.of(this.generateLandline()); } email$(firstName, lastName) { return rxjs.forkJoin({ first: firstName ? rxjs.of(firstName) : this.firstName$(), last: lastName ? rxjs.of(lastName) : this.lastName$() }).pipe(operators.map(({ first, last }) => this.generateEmail(first, last))); } pec$(firstName, lastName) { return rxjs.forkJoin({ first: firstName ? rxjs.of(firstName) : this.firstName$(), last: lastName ? rxjs.of(lastName) : this.lastName$() }).pipe(operators.map(({ first, last }) => this.generatePec(first, last))); } generatePerson$(options) { // Default values for basic personal information const gender = options?.gender || this.faker.helpers.arrayElement([types.Gender.Male, types.Gender.Female]); const birthDate = this.faker.date.birthdate({ mode: 'age', min: options?.minAge || 18, max: options?.maxAge || 80 }); // Build location options based on provided filters const locationOptions = options?.province ? { province: options.province } : options?.region ? { region: options.region } : undefined; const nameOptions = { region: options?.region, province: options?.province }; return this.placesModule.city$(locationOptions).pipe( // First stage: Generate basic personal data operators.mergeMap(birthCity => rxjs.combineLatest({ firstName: this.firstName$({ gender }), lastName: this.lastName$(nameOptions), prefix: options?.withTitle ? this.prefix$(gender) : rxjs.of(''), birthPlace: rxjs.of(birthCity) })), // Second stage: Generate documents and contact details operators.mergeMap(({ firstName, lastName, prefix, birthPlace }) => rxjs.combineLatest({ base: rxjs.of({ firstName, lastName, prefix, birthPlace }), fiscalCode: this.fiscalCodeModule.generate$({ firstName, lastName, gender, birthDate, birthPlace: { type: 'italian', city: birthPlace } }), email: this.email$(firstName, lastName), pec: this.pec$(firstName, lastName), address: this.addressModule.completeAddress$() })), // Final stage: Compose complete person profile operators.map(({ base, fiscalCode, email, pec, address }) => ({ fullName: [base.prefix, base.firstName, base.lastName].filter(Boolean).join(' '), firstName: base.firstName, lastName: base.lastName, gender, birthDate, birthPlace: { city: base.birthPlace?.name ?? '', province: base.birthPlace?.province.name ?? '', region: base.birthPlace?.region.name ?? '' }, fiscalCode, contacts: { phone: this.generatePhone(), email, pec }, address, }))); } async firstName(options) { return rxjs.lastValueFrom(this.firstName$(options)); } async lastName(options) { return rxjs.lastValueFrom(this.lastName$(options)); } async fullName(options) { return rxjs.lastValueFrom(this.fullName$(options)); } async fiscalCode(options) { return rxjs.lastValueFrom(this.fiscalCode$(options)); } async email(firstName, lastName) { return rxjs.lastValueFrom(this.email$(firstName, lastName)); } async pec(firstName, lastName) { return rxjs.lastValueFrom(this.pec$(firstName, lastName)); } async generatePerson(options) { return rxjs.lastValueFrom(this.generatePerson$(options)); } generatePhone() { const prefixes = ['320', '328', '338', '348', '350', '360', '368', '388', '389', '391', '392']; const prefix = this.faker.helpers.arrayElement(prefixes); const number = this.faker.string.numeric(7); return `${prefix}${number}`; } generateLandline() { const prefixes = ['02', '06', '010', '011', '045', '051']; const prefix = this.faker.helpers.arrayElement(prefixes); const number = this.faker.string.numeric(7); return `${prefix}${number}`; } generateEmail(firstName, lastName) { const first = firstName.toLowerCase().replace(/ /g, '.'); const last = lastName.toLowerCase().replace(/ /g, '.'); const domains = ['gmail.com', 'yahoo.it', 'libero.it', 'hotmail.it', 'outlook.it']; const domain = this.faker.helpers.arrayElement(domains); const separator = this.faker.helpers.arrayElement(['.', '_', '']); return `${first}${separator}${last}@${domain}`; } generatePec(firstName, lastName) { const first = firstName.toLowerCase().replace(/ /g, '.'); const last = lastName.toLowerCase().replace(/ /g, '.'); const domains = ['pec.it', 'legalmail.it', 'pecmail.it']; const domain = this.faker.helpers.arrayElement(domains); return `${first}.${last}@${domain}`; } parseGender(value) { const isValidGender = value === types.Gender.Male || value === types.Gender.Female; return isValidGender ? value : undefined; } } exports.PersonModule = PersonModule;