Skip to content
Merged

Dev #40

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
12 changes: 11 additions & 1 deletion src/utils/data-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,20 @@ let provincesByRegion: Map<string, readonly PHProvince[]> | undefined;
let municipalitiesByProvince: Map<string, readonly PHMunicipality[]> | undefined;
let barangaysByMunicipality: Map<string, readonly PHBarangay[]> | undefined;

const regionOrder = new Map([
'0100000000', '0200000000', '0300000000', '0400000000',
'1700000000', '0500000000', '0600000000', '0700000000',
'0800000000', '0900000000', '1000000000', '1100000000',
'1200000000', '1600000000', '1300000000', '1400000000',
'1900000000', '1800000000',
].map((code, index) => [code, index] as const));

function initializeRegions() {
if (regions) return regions;

regions = sortByName(decompressAndParse<PHRegion>(regionsCompressed));
regions = decompressAndParse<PHRegion>(regionsCompressed).sort(
(a, b) => regionOrder.get(a.psgcCode)! - regionOrder.get(b.psgcCode)!,
);
regionsByCode = new Map(regions.map(region => [region.psgcCode, region]));
return regions;
}
Expand Down
12 changes: 8 additions & 4 deletions tests/region.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { getAllRegions, getRegionByCode } from "../src";
import regions from "../src/data/regions.json";

describe("Get Region/s test suite", () => {
it("should return all regions from the JSON file", () => {
const result = getAllRegions();
expect(result).toEqual(regions);
it("should return regions in administrative display order", () => {
expect(getAllRegions().map(({ psgcCode }) => psgcCode)).toEqual([
"0100000000", "0200000000", "0300000000", "0400000000",
"1700000000", "0500000000", "0600000000", "0700000000",
"0800000000", "0900000000", "1000000000", "1100000000",
"1200000000", "1600000000", "1300000000", "1400000000",
"1900000000", "1800000000",
]);
});

it("should find a region by code", () => {
Expand Down
Loading