1.0.13 • Published 10 months ago

psgc-reader v1.0.13

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

psgc-reader

GitHub Actions Workflow Status GitHub package.json dynamic Codecov

A package for ingesting PSGC publication files

Quickstart

import psgcReader from "psgc-reader";

const result = await psgcReader.read(filePath);
// result contains regions, provinces, cities, municipalities, subMunicipalities, and barangays that are associated with each other

// if statistical columns are not needed
const resultWithoutStatistics = await psgcReader.readWithoutStatistics(
    filePath
);

Or, just the raw data

import psgcReader, { PsgcRecord } from "psgc-reader";

const resultRaw: PsgcRecord[] = await psgcReader.readRaw(filePath);
// result is an array or records

Quickly convert the publication file into a json to inspect

import psgcReader from "psgc-reader";

await psgcReader.readToJson(filePath, jsonFilePath);

Or do it step by step

1. Import the package

import psgcReader from "psgc-reader";

2. Read

await psgcReader.readPublicationFile(filePath, sheetName);

console.log(psgcReader.locations);

2.1 Before filtering, select a builder

import psgcReader, { BasicBuilder, CompleteBuilder } from "psgc-reader";

// The BasicBuilder will omit statistical fields
psgcReader.setBuilder(new BasicBuilder());

// Default builder: includes all fields
psgcReader.setBuilder(new CompleteBuilder());

3. Filter

  • regions
  • provinces
  • cities
  • municipalities
  • subMunicipalities
  • barangays
psgcReader.filter();

console.log(psgcReader.regions);
console.log(psgcReader.provinces);
console.log(psgcReader.cities);
console.log(psgcReader.municipalities);
console.log(psgcReader.subMunicipalities);
console.log(psgcReader.barangays);

4. Associate

This will link regions, provinces, cities, municipalities, subMunicipalities, and barangays

psgcReader.associate();

5. Explore

console.log("[Regions]");
psgcReader.regions.map((region) => console.log(" >", region.name));

console.log("[SubMunicipalities under Manila]");
psgcReader.cities
    .filter((city) => city.code === "1380600000")[0]
    .subMunicipalities?.map((subMunicipality) =>
        console.log(" >", subMunicipality.name)
    );

// https://stackoverflow.com/a/66523350
const barangayCountByName = psgcReader.barangays.reduce(
    (barangay, { name }) => {
        barangay[name] = barangay[name] || 0;
        barangay[name] += 1;

        return barangay;
    },
    {}
);

// https://stackoverflow.com/a/1069840
const barangayCountByNameSorted: any[] = [];
for (let name in barangayCountByName) {
    barangayCountByNameSorted.push([name, barangayCountByName[name]]);
}
barangayCountByNameSorted.sort(function (a, b) {
    return b[1] - a[1];
});

console.log("[Top 10 barangay names]");
console.log(barangayCountByNameSorted.slice(0, 10));
1.0.13

10 months ago

1.0.12

11 months ago

1.0.11

11 months ago

1.0.10

11 months ago

1.0.9

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago