Just to get started
I have gone back to our helper methods.
In your code you import nano
Likewise you could import helper functions in a similar way
// helpers
// could be import { fetchUrl, getJson } from './fetchHelpers.js'
const fetchUrl = (url) => fetch(url)
const getJson = (response) => (
(Array.isArray(response))
? Promise.all(response.map(getJson))
: response.json()
)
// likewise import { getFirstSplit, getLastSplit, splitOnce } from './stringHelpers.js'
const getFirstSplit = (term, strg) => strg.split(term)[0]
const getLastSplit = (term, strg) => strg.split(term).pop()
const splitOnce = (term, strg) => strg.substring(strg.indexOf(term) + 1)
Using these helpers makes your code a bit more readable down the line
A look at
combineURLData(filteredURLs, options) {
var finalURLs = [];
var urlData = Object.entries(filteredURLs);
urlData.forEach(function(data, index) {
for(let i=0;i<urlData.length;i++) {
if(data[0].toLowerCase() === options[i].split("&")[0]) {
finalURLs.push(data[1]+"&"+options[i].substring(options[i].indexOf("&")+1));
}
};
});
return finalURLs;
}
The above is a bit confusing. We are using forEach to loop through urlData, then inside of that we loop through it again. One loop too many?
Instead of this pattern
var result = []
arr.forEach(value => { result.push(value * 2) })
I tend to prefer arrayās map, filter or reduce. It saves having the outside factor that we mutate, in other words its self-contained.
In your case there is a conditional as to whether we push a new value or not, so reduce seem the right candidate. Itās worth looking at reduce!
So a first refactor
combineURLData (filteredURLs, options) {
const urlData = Object.entries(filteredURLs)
// Note: we need better names than dataFirst, dataSecond. What are they?
// They need readable labels.
return urlData.reduce(
(finalUrls /* <-- accumulator */, [dataFirst, dataSecond], i) => {
// options[i] is used a few times, so let's give it an identifier
const currentOption = options[i]
// using a helper function instead of options[i].split("&")[0]
if (dataFirst.toLowerCase() === getFirstSplit('&', currentOption)) {
// refactoring currentOption.substring(currentOption.indexOf('&') + 1)
// using a function I have called splitOnce instead (still your code:))
// all of this can be concatenated using a template string
// `${javascript here}&${more javascript here}`
finalUrls.push(`${dataSecond}'&'${splitOnce('&', currentOption)}`)
}
return finalUrls
},
[] // <-- this is finalUrls initial value. we will push values into this.
)
}
combineURLData without comment noise
combineURLData (filteredURLs, options) {
const urlData = Object.entries(filteredURLs)
return urlData.reduce(
(finalUrls, [dataFirst, dataSecond], i) => {
const currentOption = options[i]
if (dataFirst.toLowerCase() === getFirstSplit('&', currentOption)) {
finalUrls.push(`${dataSecond}'&'${splitOnce('&', currentOption)}`)
}
return finalUrls
},
[]
)
}
I have to get this out of the way. I am not saying my refactoring is the way to go. As with many of us, still on the learning path. Hopefully there is something to be taken from it though:)
To be continuedā¦