Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/rwanda.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/rwanda.umd.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions dist/src/rwanda.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export interface District {
export interface Sector {
[cell: string]: string[];
}
/**
* Clears the cache
*/
export declare const clearCache: () => void;
export declare const getCountry: () => string;
export declare const getProvinces: () => string[];
export declare const getDistricts: () => string[];
Expand Down
88 changes: 79 additions & 9 deletions dist/src/rwanda.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,121 @@
import rwandaData from '../rwanda.json';
// data import
var data = rwandaData;
// caching
var cache = {
provinces: null,
districts: null,
districtsByProvince: new Map(),
sectors: null,
sectorsByDistrict: new Map(),
cells: null,
cellsBySector: new Map(),
villages: null,
villagesByCell: new Map(),
};
// TODO: find a better way to get the size of the cache
// Method bellow does not work
// export const getCacheSize = (): number => {
// return new TextEncoder().encode(JSON.stringify(cache)).length;
// }
/**
* Clears the cache
*/
export var clearCache = function () {
cache.provinces = null;
cache.districts = null;
cache.districtsByProvince = new Map();
cache.sectors = null;
cache.sectorsByDistrict = new Map();
cache.cells = null;
cache.cellsBySector = new Map();
cache.villages = null;
cache.villagesByCell = new Map();
};
// functions
export var getCountry = function () {
return 'Rwanda';
};
export var getProvinces = function () {
return Object.keys(data.rwanda);
if (cache.provinces)
return cache.provinces;
var res = Object.keys(data.rwanda);
cache.provinces = res;
return res;
};
export var getDistricts = function () {
return Object.values(data.rwanda).flatMap(function (province) { return Object.keys(province); });
if (cache.districts)
return cache.districts;
var res = Object.values(data.rwanda).flatMap(function (province) { return Object.keys(province); });
cache.districts = res;
return res;
};
export var getDistrictsByProvince = function (province) {
return Object.keys(data.rwanda[province] || {});
if (cache.districtsByProvince.has(province))
return cache.districtsByProvince.get(province);
var res = Object.keys(data.rwanda[province] || {});
cache.districtsByProvince.set(province, res);
return res;
};
export var getSectors = function () {
return Object.values(data.rwanda).flatMap(function (province) {
if (cache.sectors)
return cache.sectors;
var res = Object.values(data.rwanda).flatMap(function (province) {
return Object.values(province).flatMap(function (district) { return Object.keys(district); });
});
cache.sectors = res;
return res;
};
export var getSectorsByDistrict = function (province, district) {
var _a;
return Object.keys(((_a = data.rwanda[province]) === null || _a === void 0 ? void 0 : _a[district]) || {});
var key = "".concat(province, "|").concat(district);
if (cache.sectorsByDistrict.has(key))
return cache.sectorsByDistrict.get(key);
var res = Object.keys(((_a = data.rwanda[province]) === null || _a === void 0 ? void 0 : _a[district]) || {});
cache.sectorsByDistrict.set(key, res);
return res;
};
export var getCells = function () {
return Object.values(data.rwanda).flatMap(function (province) {
if (cache.cells)
return cache.cells;
var res = Object.values(data.rwanda).flatMap(function (province) {
return Object.values(province).flatMap(function (district) {
return Object.values(district).flatMap(function (sector) { return Object.keys(sector); });
});
});
cache.cells = res;
return res;
};
export var getCellsBySector = function (province, district, sector) {
var _a, _b;
return Object.keys(((_b = (_a = data.rwanda[province]) === null || _a === void 0 ? void 0 : _a[district]) === null || _b === void 0 ? void 0 : _b[sector]) || {});
var key = "".concat(province, "|").concat(district, "|").concat(sector);
if (cache.cellsBySector.has(key))
return cache.cellsBySector.get(key);
var res = Object.keys(((_b = (_a = data.rwanda[province]) === null || _a === void 0 ? void 0 : _a[district]) === null || _b === void 0 ? void 0 : _b[sector]) || {});
cache.cellsBySector.set(key, res);
return res;
};
export var getVillages = function () {
return Object.values(data.rwanda).flatMap(function (province) {
if (cache.villages)
return cache.villages;
var res = Object.values(data.rwanda).flatMap(function (province) {
return Object.values(province).flatMap(function (district) {
return Object.values(district).flatMap(function (sector) {
return Object.values(sector).flatMap(function (cell) { return cell; });
});
});
});
cache.villages = res;
return res;
};
export var getVillagesByCell = function (province, district, sector, cell) {
var _a, _b, _c;
return ((_c = (_b = (_a = data.rwanda[province]) === null || _a === void 0 ? void 0 : _a[district]) === null || _b === void 0 ? void 0 : _b[sector]) === null || _c === void 0 ? void 0 : _c[cell]) || [];
var key = "".concat(province, "|").concat(district, "|").concat(sector, "|").concat(cell);
if (cache.villagesByCell.has(key))
return cache.villagesByCell.get(key);
var res = ((_c = (_b = (_a = data.rwanda[province]) === null || _a === void 0 ? void 0 : _a[district]) === null || _b === void 0 ? void 0 : _b[sector]) === null || _c === void 0 ? void 0 : _c[cell]) || [];
cache.villagesByCell.set(key, res);
return res;
};
export var getRandomLocation = function () {
var provinces = Object.keys(data.rwanda);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/rwanda.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export interface District {
export interface Sector {
[cell: string]: string[];
}
/**
* Clears the cache
*/
export declare const clearCache: () => void;
export declare const getCountry: () => string;
export declare const getProvinces: () => string[];
export declare const getDistricts: () => string[];
Expand Down
92 changes: 83 additions & 9 deletions src/rwanda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,132 @@ export interface Sector {
// data import
const data: RwandaData = rwandaData;

// caching
const cache: {
provinces: string[] | null,
districts: string[] | null,
districtsByProvince: Map<string, string[]>,
sectors: string[] | null
sectorsByDistrict: Map<string, string[]>,
cells: string[] | null,
cellsBySector: Map<string, string[]>,
villages: string[] | null,
villagesByCell: Map<string, string[]>
} = {
provinces: null,
districts: null,
districtsByProvince: new Map<string, string[]>(),
sectors: null,
sectorsByDistrict: new Map<string, string[]>(),
cells: null,
cellsBySector: new Map<string, string[]>(),
villages: null,
villagesByCell: new Map<string, string[]>(),
};

// TODO: find a better way to get the size of the cache
// Method bellow does not work
// export const getCacheSize = (): number => {
// return new TextEncoder().encode(JSON.stringify(cache)).length;
// }

/**
* Clears the cache
*/
export const clearCache = (): void => {
cache.provinces = null;
cache.districts = null;
cache.districtsByProvince = new Map<string, string[]>();
cache.sectors = null;
cache.sectorsByDistrict = new Map<string, string[]>();
cache.cells = null;
cache.cellsBySector = new Map<string, string[]>();
cache.villages = null;
cache.villagesByCell = new Map<string, string[]>();
};

// functions

export const getCountry = (): string => {
return 'Rwanda';
};

export const getProvinces = (): string[] => {
return Object.keys(data.rwanda);
if (cache.provinces) return cache.provinces;
const res = Object.keys(data.rwanda);
cache.provinces = res;
return res;
};

export const getDistricts = (): string[] => {
return Object.values(data.rwanda).flatMap(province => Object.keys(province));
if (cache.districts) return cache.districts;
const res = Object.values(data.rwanda).flatMap(province => Object.keys(province));
cache.districts = res;
return res;
};

export const getDistrictsByProvince = (province: string): string[] => {
return Object.keys(data.rwanda[province] || {});
if (cache.districtsByProvince.has(province)) return cache.districtsByProvince.get(province)!;
const res = Object.keys(data.rwanda[province] || {});
cache.districtsByProvince.set(province, res);
return res;
};

export const getSectors = (): string[] => {
return Object.values(data.rwanda).flatMap(province =>
if (cache.sectors) return cache.sectors;
const res = Object.values(data.rwanda).flatMap(province =>
Object.values(province).flatMap(district => Object.keys(district))
);
cache.sectors = res;
return res;
};

export const getSectorsByDistrict = (province: string, district: string): string[] => {
return Object.keys(data.rwanda[province]?.[district] || {});
const key = `${province}|${district}`;
if (cache.sectorsByDistrict.has(key)) return cache.sectorsByDistrict.get(key)!;
const res = Object.keys(data.rwanda[province]?.[district] || {});
cache.sectorsByDistrict.set(key, res);
return res;
};

export const getCells = (): string[] => {
return Object.values(data.rwanda).flatMap(province =>
if (cache.cells) return cache.cells;
const res = Object.values(data.rwanda).flatMap(province =>
Object.values(province).flatMap(district =>
Object.values(district).flatMap(sector => Object.keys(sector))
)
);
cache.cells = res;
return res;
};

export const getCellsBySector = (province: string, district: string, sector: string): string[] => {
return Object.keys(data.rwanda[province]?.[district]?.[sector] || {});
const key = `${province}|${district}|${sector}`;
if (cache.cellsBySector.has(key)) return cache.cellsBySector.get(key)!;
const res = Object.keys(data.rwanda[province]?.[district]?.[sector] || {});
cache.cellsBySector.set(key, res);
return res;
};

export const getVillages = (): string[] => {
return Object.values(data.rwanda).flatMap(province =>
if (cache.villages) return cache.villages;
const res = Object.values(data.rwanda).flatMap(province =>
Object.values(province).flatMap(district =>
Object.values(district).flatMap(sector =>
Object.values(sector).flatMap(cell => cell)
)
)
);
cache.villages = res;
return res;
};

export const getVillagesByCell = (province: string, district: string, sector: string, cell: string): string[] => {
return data.rwanda[province]?.[district]?.[sector]?.[cell] || [];
const key = `${province}|${district}|${sector}|${cell}`;
if (cache.villagesByCell.has(key)) return cache.villagesByCell.get(key)!;
const res = data.rwanda[province]?.[district]?.[sector]?.[cell] || [];
cache.villagesByCell.set(key, res);
return res;
};

export const getRandomLocation = () => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"include": [
"src"
],
}
}