Skip to content

Commit 6b9a814

Browse files
authored
Merge pull request #52 from ciatph/dev
v1.0.7
2 parents 027707e + 7097425 commit 6b9a814

File tree

6 files changed

+40
-23
lines changed

6 files changed

+40
-23
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Extracted municipalities are written in JSON files following the format:
2525
}
2626
```
2727

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

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

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

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

182184
// writeMunicipalities() writes municipalities data to a JSON file
183-
file.writeMunicipalities({
185+
// and returns the JSON object
186+
const json = file.writeMunicipalities({
184187
provinces,
185188
fileName: path.join(__dirname, 'municipalities.json'),
186189
prettify: true
187190
})
188191

192+
// shapeJsonData() returns the output of writeMunicipalities()
193+
// without writing to a JSON file
194+
const json2 = file.shapeJsonData(provinces)
195+
189196
// JSON data of the parsed excel file will is accessible on
190197
// file.datalist
191198
console.log(file.datalist)
@@ -201,7 +208,7 @@ const path = require('path')
201208
const ExcelFile = require('./classes/excel')
202209

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

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

250257
## Building Standalone Windows Executables
251258

252-
The main npm scripts can be packaged into standalone windows executables.
259+
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.
253260

254261
1. Run any of the following scripts to build the programs.
255262
```bash

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ph-municipalities",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "List and write the `municipalities` of Philippines provinces or regions into JSON files",
55
"main": "index.js",
66
"scripts": {

src/classes/excel/index.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,25 @@ class ExcelFile {
213213
return this.#datalist
214214
}
215215

216+
/**
217+
* Get the requested data with other misc data
218+
* @param {String[]} provinces - List of provinces
219+
* @returns {Object} Formatted raw data with misc. metadata
220+
*/
221+
shapeJsonData (provinces) {
222+
const url = (this.#url) ? this.#url : `local datasource cache from ${process.env.DEFAULT_EXCEL_FILE_URL}`
223+
224+
return {
225+
metadata: {
226+
source: url || '',
227+
title: 'List of PH Municipalities By Province and Region',
228+
description: 'This dataset generated with reference to the excel file contents from the source URL.',
229+
date_created: new Date().toDateString()
230+
},
231+
data: this.listMunicipalities({ provinces })
232+
}
233+
}
234+
216235
/**
217236
* List the municipalities of given province(s)
218237
* @param {String[]} provinces - Array of case-sensitive province names. Starts with an upper case.
@@ -256,7 +275,7 @@ class ExcelFile {
256275
* @param {String[]} provinces - Array of case-sensitive province names. Starts with an upper case.
257276
* @param {String} fielName - Full file path to a JSON file
258277
* @param {Bool} prettify - Write the JSON content with proper spacings and newlines
259-
* @returns
278+
* @returns {Object} Formatted raw data with misc. metadata
260279
*/
261280
writeMunicipalities ({ provinces, fileName, prettify = false }) {
262281
if (!fileName) {
@@ -268,29 +287,15 @@ class ExcelFile {
268287
}
269288

270289
try {
271-
// List the municipalities
272-
const municipalities = this.listMunicipalities({ provinces })
273-
274-
const url = (this.#url) ? this.#url : `local datasource cache from ${process.env.DEFAULT_EXCEL_FILE_URL}`
275-
276-
const str = {
277-
metadata: {
278-
source: url || '',
279-
title: 'List of PH Municipalities By Province and Region',
280-
description: 'This dataset generated with reference to the excel file contents from the source URL.',
281-
date_created: new Date().toDateString()
282-
},
283-
data: municipalities
284-
}
290+
const str = this.shapeJsonData(provinces)
285291

286292
const json = (prettify)
287293
? JSON.stringify(str, null, 2)
288294
: JSON.stringify(str)
289295

290296
// Write results to a JSON file
291297
fs.writeFileSync(fileName, json, 'utf-8')
292-
293-
return municipalities
298+
return str
294299
} catch (err) {
295300
throw new Error(err.message)
296301
}

src/lib/selector.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ const selectDataSource = async () => {
1919
if (askDownload === 'Y') {
2020
const pathToFile = path.join(process.cwd(), 'datasource.xlsx')
2121

22-
url = await prompt('\nEnter the download URL of a remote Excel file: ')
22+
while (!url) {
23+
url = await prompt('\nEnter the download URL of a remote Excel file: ')
24+
}
25+
2326
console.log(`Downloading file from ${url}...`)
2427

2528
try {

src/scripts/by_province.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const selectDataSource = require('../lib/selector')
88
const main = async () => {
99
let exit = false
1010
let ExcelHandler = null
11+
console.log('\nWelcome! Press Ctrl+C to stop anytime.')
1112

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

src/scripts/by_region.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const main = async () => {
1010
let exit = false
1111
let ExcelHandler = null
1212
let idx
13+
console.log('\nWelcome! Press Ctrl+C to stop anytime.')
1314

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

0 commit comments

Comments
 (0)