Skip to content

Commit

Permalink
Merge pull request #52 from ciatph/dev
Browse files Browse the repository at this point in the history
v1.0.7
  • Loading branch information
ciatph authored Jan 30, 2023
2 parents 027707e + 7097425 commit 6b9a814
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Extracted municipalities are written in JSON files following the format:
}
```

Pre-compiled windows binaries are available for download in the latest [Releases](https://github.com/ciatph/ph-municipalities/releases) download page.

## Requirements

The following dependencies are used for this project. Feel free to use other dependency versions as needed.
Expand Down Expand Up @@ -167,7 +169,7 @@ const path = require('path')
const ExcelFile = require('./classes/excel')

// Use the the following if installed via npm
// const ExcelFile = require('ph-municipalities')
// const { ExcelFile } = require('ph-municipalities')

// Reads an existing excel file on /data/day1.xlsx
file = new ExcelFile({
Expand All @@ -180,12 +182,17 @@ const provinces = ['Albay','Masbate','Sorsogon']
const municipalitiesFromProvince = file.listMunicipalities(provinces)

// writeMunicipalities() writes municipalities data to a JSON file
file.writeMunicipalities({
// and returns the JSON object
const json = file.writeMunicipalities({
provinces,
fileName: path.join(__dirname, 'municipalities.json'),
prettify: true
})

// shapeJsonData() returns the output of writeMunicipalities()
// without writing to a JSON file
const json2 = file.shapeJsonData(provinces)

// JSON data of the parsed excel file will is accessible on
// file.datalist
console.log(file.datalist)
Expand All @@ -201,7 +208,7 @@ const path = require('path')
const ExcelFile = require('./classes/excel')

// Use the the following if installed via npm
// const ExcelFile = require('ph-municipalities')
// const { ExcelFile } = require('ph-municipalities')

const main = async () => {
// Excel file will be downloaded to /data/day1.xlsx
Expand Down Expand Up @@ -249,7 +256,7 @@ PHExcel.events.on(PHExcel.EVENTS.LOADED, async () => {

## Building Standalone Windows Executables

The main npm scripts can be packaged into standalone windows executables.
The main npm scripts can be packaged into standalone windows executables. Pre-compiled windows binaries are available for download in the latest [Releases](https://github.com/ciatph/ph-municipalities/releases) download page.

1. Run any of the following scripts to build the programs.
```bash
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ph-municipalities",
"version": "1.0.6",
"version": "1.0.7",
"description": "List and write the `municipalities` of Philippines provinces or regions into JSON files",
"main": "index.js",
"scripts": {
Expand Down
39 changes: 22 additions & 17 deletions src/classes/excel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,25 @@ class ExcelFile {
return this.#datalist
}

/**
* Get the requested data with other misc data
* @param {String[]} provinces - List of provinces
* @returns {Object} Formatted raw data with misc. metadata
*/
shapeJsonData (provinces) {
const url = (this.#url) ? this.#url : `local datasource cache from ${process.env.DEFAULT_EXCEL_FILE_URL}`

return {
metadata: {
source: url || '',
title: 'List of PH Municipalities By Province and Region',
description: 'This dataset generated with reference to the excel file contents from the source URL.',
date_created: new Date().toDateString()
},
data: this.listMunicipalities({ provinces })
}
}

/**
* List the municipalities of given province(s)
* @param {String[]} provinces - Array of case-sensitive province names. Starts with an upper case.
Expand Down Expand Up @@ -256,7 +275,7 @@ class ExcelFile {
* @param {String[]} provinces - Array of case-sensitive province names. Starts with an upper case.
* @param {String} fielName - Full file path to a JSON file
* @param {Bool} prettify - Write the JSON content with proper spacings and newlines
* @returns
* @returns {Object} Formatted raw data with misc. metadata
*/
writeMunicipalities ({ provinces, fileName, prettify = false }) {
if (!fileName) {
Expand All @@ -268,29 +287,15 @@ class ExcelFile {
}

try {
// List the municipalities
const municipalities = this.listMunicipalities({ provinces })

const url = (this.#url) ? this.#url : `local datasource cache from ${process.env.DEFAULT_EXCEL_FILE_URL}`

const str = {
metadata: {
source: url || '',
title: 'List of PH Municipalities By Province and Region',
description: 'This dataset generated with reference to the excel file contents from the source URL.',
date_created: new Date().toDateString()
},
data: municipalities
}
const str = this.shapeJsonData(provinces)

const json = (prettify)
? JSON.stringify(str, null, 2)
: JSON.stringify(str)

// Write results to a JSON file
fs.writeFileSync(fileName, json, 'utf-8')

return municipalities
return str
} catch (err) {
throw new Error(err.message)
}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const selectDataSource = async () => {
if (askDownload === 'Y') {
const pathToFile = path.join(process.cwd(), 'datasource.xlsx')

url = await prompt('\nEnter the download URL of a remote Excel file: ')
while (!url) {
url = await prompt('\nEnter the download URL of a remote Excel file: ')
}

console.log(`Downloading file from ${url}...`)

try {
Expand Down
1 change: 1 addition & 0 deletions src/scripts/by_province.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const selectDataSource = require('../lib/selector')
const main = async () => {
let exit = false
let ExcelHandler = null
console.log('\nWelcome! Press Ctrl+C to stop anytime.')

while (!exit) {
// Prompt to enter the download URL of a remote excel file or use the default local excel file
Expand Down
1 change: 1 addition & 0 deletions src/scripts/by_region.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const main = async () => {
let exit = false
let ExcelHandler = null
let idx
console.log('\nWelcome! Press Ctrl+C to stop anytime.')

while (!exit) {
// Prompt to enter the download URL of a remote excel file or use the default local excel file
Expand Down

0 comments on commit 6b9a814

Please sign in to comment.