diff --git a/lib/run-from-file.js b/lib/run-from-file.js index b08892c..6caf5e7 100644 --- a/lib/run-from-file.js +++ b/lib/run-from-file.js @@ -1,87 +1,87 @@ -const fs = require("fs"); -const Papa = require("papaparse"); -const makeText = require("./makeText"); +import fs from 'fs'; +import { makeStop } from 'makeText.js'; +import Papa from 'papaparse'; /* CREATE TTS STOP NAME IN CSV */ (async () => { - console.log(); - console.log("* * * * * * * * * * * * * * * * * * * * * * * * * *"); - console.log("* TTS TEXT"); - const start = new Date(); - console.log("* Run started on " + start.toISOString()); + console.log(); + console.log('* * * * * * * * * * * * * * * * * * * * * * * * * *'); + console.log('* TTS TEXT'); + const start = new Date(); + console.log('* Run started on ' + start.toISOString()); - // Import stops.txt file - console.log("* Reading stops.txt file from disk..."); - const allStopsTxt = fs.readFileSync("stops.txt", { encoding: "utf8" }); - const allStopsPapa = Papa.parse(allStopsTxt, { header: true }); - const allStopsData = allStopsPapa.data; + // Import stops.txt file + console.log('* Reading stops.txt file from disk...'); + const allStopsTxt = fs.readFileSync('stops.txt', { encoding: 'utf8' }); + const allStopsPapa = Papa.parse(allStopsTxt, { header: true }); + const allStopsData = allStopsPapa.data; - // Define variable to hold results - const ttsSummary = []; - const stopsUpdated = []; - const stopsDiff = []; + // Define variable to hold results + const ttsSummary = []; + const stopsUpdated = []; + const stopsDiff = []; - // Log progress - console.log("* Preparing " + allStopsData.length + " stops..."); + // Log progress + console.log('* Preparing ' + allStopsData.length + ' stops...'); - // Iterate on each stop - for (const [index, stop] of allStopsData.entries()) { - // - process.stdout.clearLine(); - process.stdout.write( - `* Processing stop ${stop.stop_id} (${index}/${allStopsData.length})`, - ); - process.stdout.cursorTo(0); + // Iterate on each stop + for (const [index, stop] of allStopsData.entries()) { + // + process.stdout.clearLine(); + process.stdout.write( + `* Processing stop ${stop.stop_id} (${index}/${allStopsData.length})`, + ); + process.stdout.cursorTo(0); - // Assemble transfer modes - let modes = (({ - light_rail, - subway, - train, - boat, - bike_sharing, - airport, - }) => ({ light_rail, subway, train, boat, bike_sharing, airport }))(stop); + // Assemble transfer modes + let modes = (({ + airport, + bike_sharing, + boat, + light_rail, + subway, + train, + }) => ({ airport, bike_sharing, boat, light_rail, subway, train }))(stop); - const ttsStopName = makeStop(stop.stop_name, modes); + const ttsStopName = makeStop(stop.stop_name, modes); - ttsSummary.push({ - stop_id: stop.stop_id, - stop_name: stop.stop_name, - tts_stop_name: ttsStopName, - }); + ttsSummary.push({ + stop_id: stop.stop_id, + stop_name: stop.stop_name, + tts_stop_name: ttsStopName, + }); - if (stop.tts_stop_name != ttsStopName) { - stopsDiff.push(stop); - stopsDiff.push({ ...stop, tts_stop_name: ttsStopName }); - } + if (stop.tts_stop_name != ttsStopName) { + stopsDiff.push(stop); + stopsDiff.push({ ...stop, tts_stop_name: ttsStopName }); + } - stopsUpdated.push({ ...stop, tts_stop_name: ttsStopName }); + stopsUpdated.push({ ...stop, tts_stop_name: ttsStopName }); - // - } + // + } - // Save the formatted data into a CSV file - process.stdout.clearLine(); - console.log("* Saving result to CSV file..."); - const ttsSummaryCsv = Papa.unparse(ttsSummary, { - skipEmptyLines: "greedy", - }); - fs.writeFileSync("stops_tts_summary.txt", ttsSummaryCsv); - const stopsUpdatedCsv = Papa.unparse(stopsUpdated, { - skipEmptyLines: "greedy", - }); - fs.writeFileSync("stops_updated.txt", stopsUpdatedCsv); - const stopsDiffCsv = Papa.unparse(stopsDiff, { - skipEmptyLines: "greedy", - }); - fs.writeFileSync("stops_diff.txt", stopsDiffCsv); + // Save the formatted data into a CSV file + process.stdout.clearLine(); + console.log('* Saving result to CSV file...'); + const ttsSummaryCsv = Papa.unparse(ttsSummary, { + skipEmptyLines: 'greedy', + }); + fs.writeFileSync('stops_tts_summary.txt', ttsSummaryCsv); + const stopsUpdatedCsv = Papa.unparse(stopsUpdated, { + skipEmptyLines: 'greedy', + }); + fs.writeFileSync('stops_updated.txt', stopsUpdatedCsv); + const stopsDiffCsv = Papa.unparse(stopsDiff, { + skipEmptyLines: 'greedy', + }); + fs.writeFileSync('stops_diff.txt', stopsDiffCsv); - // - console.log("* Processed " + ttsSummary.length + " stops."); - const syncDuration = new Date() - start; - console.log("* Run took " + syncDuration / 1000 + " seconds."); - console.log("* * * * * * * * * * * * * * * * * * * * * * * * * *"); - console.log(); + // + console.log('* Processed ' + ttsSummary.length + ' stops.'); + const syncDuration = new Date() - start; + console.log('* Run took ' + syncDuration / 1000 + ' seconds.'); + console.log('* * * * * * * * * * * * * * * * * * * * * * * * * *'); + console.log(); })();