@italia-tools/faker
Version:
Italian-specific fake data generator based on Faker.js
217 lines (212 loc) • 8.68 kB
JavaScript
'use strict';
var cityAdapter = require('../utils/cityAdapter.cjs');
var weightedRandom = require('../utils/weightedRandom.cjs');
var rxjs = require('rxjs');
var operators = require('rxjs/operators');
var countryAdapter = require('../utils/countryAdapter.cjs');
function _interopNamespaceDefaultOnly (e) { return Object.freeze({ __proto__: null, default: e }); }
class PlacesModule {
constructor(faker) {
this.faker = faker;
this.dataSubject = new rxjs.BehaviorSubject(null);
this.countrySubject = new rxjs.BehaviorSubject(null);
}
loadCityData() {
if (this.dataSubject.getValue()) {
return rxjs.of(undefined);
}
return rxjs.from(Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefaultOnly(require('../data/cities.json.cjs')); })).pipe(operators.map(module => {
const citiesData = module.default;
const citySelector = new weightedRandom.WeightedRandomSelector(cityAdapter.CityAdapter.toWeightedItems(citiesData));
this.dataSubject.next({
citySelector,
citiesData
});
}), operators.catchError(error => {
console.error('Error loading city data:', error);
throw error;
}));
}
loadCountryData() {
// Check if data is already loaded
const existingData = this.countrySubject.getValue();
if (existingData) {
return rxjs.of(undefined);
}
return rxjs.from(Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefaultOnly(require('../data/countries.json.cjs')); })).pipe(operators.map(module => {
const countries = module.default.map((country) => countryAdapter.CountryAdapter.toEnglish(country));
this.countrySubject.next(countries);
}), operators.catchError(error => {
console.error('Error loading countries:', error);
throw new Error(`Failed to load country data: ${error.message}`);
}));
}
randomCity$() {
return this.loadCityData().pipe(operators.map(() => this.selectItalianCity()));
}
randomCities$(count) {
return this.loadCityData().pipe(operators.switchMap(() => {
const uniqueCities = new Set();
return new rxjs.Observable(observer => {
while (uniqueCities.size < count) {
uniqueCities.add(this.selectItalianCity());
}
observer.next(Array.from(uniqueCities));
observer.complete();
});
}));
}
province$() {
return this.randomCity$().pipe(operators.map(randomCity => ({
name: randomCity.province.name,
code: randomCity.code
})));
}
city$(options) {
return this.loadCityData().pipe(operators.map(() => {
const data = this.dataSubject.getValue();
if (!data) {
throw new Error('City data not initialized');
}
// Search by belfioreCode (exact match)
if (options?.belfioreCode) {
const city = data.citiesData.find(city => city.codiceCatastale.toLowerCase() === options.belfioreCode?.toLowerCase());
return city ? cityAdapter.CityAdapter.toEnglish(city) : null;
}
// Search by name (exact match or case-insensitive)
if (options?.cityName) {
const city = data.citiesData.find(city => city.nome.toLowerCase() === options.cityName?.toLowerCase());
return city ? cityAdapter.CityAdapter.toEnglish(city) : null;
}
if (options?.province) {
const filteredCities = data.citiesData.filter(city => city.provincia.nome.toLowerCase() === options.province?.toLowerCase());
if (filteredCities.length > 0) {
return cityAdapter.CityAdapter.toEnglish(this.faker.helpers.arrayElement(filteredCities));
}
}
if (options?.region) {
const filteredCities = data.citiesData.filter(city => city.regione.nome.toLowerCase() === options.region?.toLowerCase());
if (filteredCities.length > 0) {
return cityAdapter.CityAdapter.toEnglish(this.faker.helpers.arrayElement(filteredCities));
}
}
return this.selectItalianCity();
}));
}
allCities$() {
return this.loadCityData().pipe(operators.map(() => {
const data = this.dataSubject.getValue();
if (!data) {
throw new Error('City data not initialized');
}
return data.citiesData.map(city => cityAdapter.CityAdapter.toEnglish(city));
}));
}
mostPopulatedCities$(x) {
return this.loadCityData().pipe(operators.map(() => {
const data = this.dataSubject.getValue();
if (!data) {
throw new Error('City data not initialized');
}
const cities = data.citiesData.map(city => cityAdapter.CityAdapter.toEnglish(city));
return cities.sort((a, b) => b.population - a.population).slice(0, x);
}));
}
;
getBirthPlace$() {
return this.randomCity$().pipe(operators.map(randomCity => ({
name: randomCity.name,
belfioreCode: randomCity.belfioreCode,
province: randomCity.province.name,
region: randomCity.region.name,
provinceCode: randomCity.provinceCode
})));
}
region$() {
return this.randomCity$().pipe(operators.map(randomCity => randomCity.region.name));
}
preloadData$() {
return this.loadCityData();
}
randomCountry$() {
return this.loadCountryData().pipe(operators.map(() => {
const countries = this.countrySubject.getValue();
if (!countries)
throw new Error('Country data not initialized');
return this.faker.helpers.arrayElement(countries);
}));
}
getCountryByName$(name) {
if (!name || typeof name !== 'string') {
return rxjs.of(null);
}
return this.loadCountryData().pipe(operators.map(() => {
const countries = this.countrySubject.getValue();
if (!countries || !Array.isArray(countries)) {
throw new Error('Country data not properly initialized');
}
return countries.find(country => country.nameIt.toLowerCase() === name.toLowerCase() ||
country.nameEn.toLowerCase() === name.toLowerCase()) || null;
}), operators.catchError(error => {
console.error('Error in getCountryByName$:', error);
return rxjs.of(null);
}));
}
getAllCountries$() {
return this.loadCountryData().pipe(operators.map(() => {
const countries = this.countrySubject.getValue();
if (!countries)
throw new Error('Country data not initialized');
return countries;
}));
}
async randomCity() {
return rxjs.lastValueFrom(this.randomCity$());
}
async randomCities(count) {
return rxjs.lastValueFrom(this.randomCities$(count));
}
async province() {
return rxjs.lastValueFrom(this.province$());
}
async city(options) {
return rxjs.lastValueFrom(this.city$(options));
}
async allCities() {
return rxjs.lastValueFrom(this.allCities$());
}
async mostPopulatedCities(x) {
return rxjs.lastValueFrom(this.mostPopulatedCities$(x));
}
async getBirthPlace() {
return rxjs.lastValueFrom(this.getBirthPlace$());
}
async region() {
return rxjs.lastValueFrom(this.region$());
}
async preloadData() {
return rxjs.lastValueFrom(this.preloadData$());
}
async randomCountry() {
return rxjs.lastValueFrom(this.randomCountry$());
}
async getCountryByName(name) {
return rxjs.lastValueFrom(this.getCountryByName$(name));
}
async getAllCountries() {
return rxjs.lastValueFrom(this.getAllCountries$());
}
clearCache() {
this.dataSubject.next(null);
this.countrySubject.next(null);
}
selectItalianCity() {
const data = this.dataSubject.getValue();
if (!data) {
throw new Error('City data not initialized');
}
const randomCity = data.citySelector.select();
return cityAdapter.CityAdapter.toEnglish(randomCity);
}
}
exports.PlacesModule = PlacesModule;