Skip to content

Instantly share code, notes, and snippets.

@jgngo
Last active January 23, 2020 07:16
Show Gist options
  • Save jgngo/01aaaaf9c4c392aeb8cc6b65a804d724 to your computer and use it in GitHub Desktop.
Save jgngo/01aaaaf9c4c392aeb8cc6b65a804d724 to your computer and use it in GitHub Desktop.
filter store list
const fs = require("fs");
const axios = require("axios");
const INPUT_URL =
"https://s3-ap-southeast-1.amazonaws.com/s3.philseven.com/public/cbs_stores.json";
const INPUT_FILE = "cbs_stores.json";
const OUTPUT_FILE = "solr_import.json";
// Get from URL
axios
.get(INPUT_URL)
.then(function(stores) {
const solrStores = processStores(stores.data);
writeJson(solrStores)
})
.catch(function(error) {
// handle error
console.log(error);
})
.then(function() {
// always executed
});
// Get from File
/*
let rawdata = fs.readFileSync(INPUT_FILE);
let stores = JSON.parse(rawdata);
processStores(stores);
// console.log(`INPUT FILE ${INPUT_FILE} rows = ${stores.length}`);
// console.log(`OUTPUT FILE ${OUTPUT_FILE} rows = ${solrStores.length}`);
*/
function writeJson(stores) {
fs.writeFile(OUTPUT_FILE, JSON.stringify(stores), function(err) {
if (err) throw err;
console.log(`${OUTPUT_FILE} saved.`);
});
}
function processStores(stores) {
// Filter out null values in latlon
const ecmsStores = stores.filter(store => {
return store.latlon !== null;
});
const solrStores = ecmsStores.map(store => {
let cleanStore = store;
// Convert number strings to numbers
cleanStore.storeLatitude = parseFloat(store.storeLatitude);
cleanStore.storeLongitude = parseFloat(store.storeLongitude);
// Replace null values with dummy strings
if (store.storeName === null) {
cleanStore.storeName = "Store Name";
}
if (store.storeAddress === null) {
cleanStore.storeAddress = "Store Address";
}
if (store.storeRegion === null) {
cleanStore.storeRegion = "Region";
}
if (store.storeProvince === null) {
cleanStore.storeProvince = "Province";
}
if (store.storeCityMuni === null) {
cleanStore.storeCityMuni = "City";
}
// Convert boolean strings to Boolean type
if (typeof store.storeCommEnabled != "boolean") {
cleanStore.storeCommEnabled = store.storeCommEnabled === "true";
}
return cleanStore;
});
return solrStores;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment