UNPKG

wherebj

Version:

Une librairie pour accéder aux données des villes et quartiers du Bénin

47 lines (46 loc) 1.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const path_1 = require("path"); class WhereBJ { constructor() { const filePath = (0, path_1.join)(__dirname, 'data', 'locations.json'); this.data = JSON.parse((0, fs_1.readFileSync)(filePath, 'utf8')); } getAllDepartments() { return this.data.departments; } getDepartment(id) { return this.data.departments.find(dept => dept.id === id); } getAllCities() { return this.data.departments.flatMap(dept => dept.cities); } getCity(id) { return this.getAllCities().find(city => city.id === id); } getCityQuarters(cityId) { const city = this.getCity(cityId); return city ? city.quarters : []; } searchQuarters(query) { const results = []; const normalizedQuery = query.toLowerCase(); this.getAllCities().forEach(city => { city.quarters.forEach(quarter => { if (quarter.name.toLowerCase().includes(normalizedQuery)) { results.push({ city: city.name, quarter }); } }); }); return results; } getMetadata() { return this.data.metadata; } getCitiesByDepartment(departmentId) { const department = this.getDepartment(departmentId); return department ? department.cities : []; } } exports.default = WhereBJ;