Skip to content

Instantly share code, notes, and snippets.

@julio73
Last active May 8, 2022 10:36
Show Gist options
  • Save julio73/9c15a9f209a27c58a9167cc6d7b20728 to your computer and use it in GitHub Desktop.
Save julio73/9c15a9f209a27c58a9167cc6d7b20728 to your computer and use it in GitHub Desktop.
/**
* Combines indexed entries in the parallel arrays of a given location object.
* Expects the same number of entries in each array.
*
* @param data Object with parallel values in arrays.
* @param separator String to use as a separator in the raw values.
* @param display_separator String to use as a separator in the display values.
* @param bottom_to_top Boolean to indicate if the values should be hierarchically displayed from bottom to top or top to bottom in the display values.
* @returns Object with raw and display values in parallel arrays.
* // The data is in the form:
* {
* "country": ["USA", "USA", "USA", null, "USA", "France"],
* "state": ["CA", "CA", "NY", null, "TX", null],
* "city": ["Los Angeles", "San Francisco", "New York", "Abidjan", null, "Paris"]
* }
*
* // The output should be:
* {
* "display": ["USA", "CA, USA", "Los Angeles, CA, USA", "San Francisco, CA, USA", "NY, USA", "New York, NY, USA", "Abidjan", "TX, USA", "France", "Paris, France"],
* "raw": ["USA", "USA|CA", "USA|CA|Los Angeles", "USA|CA|San Francisco", "USA|NY", "USA|NY|New York", "||Abidjan", "USA|TX", "France", "France||Paris"]
* }
*/
function buildSimpleLocationFieldOptions(data, separator = "|", display_separator = ', ', bottom_to_top = true) {
let display = [];
let raw = [];
let visitedCountries = {};
let visitedStates = {};
for (let i = 0; i < data.country.length; i++) {
const [country, state, city] = [data.country[i], (data.state ? data.state[i] : null), (data.city ? data.city[i] : null)];
// add the country if it's not been seen before
if (country && !(country in visitedCountries)) {
display.push(country);
raw.push(country);
visitedCountries[country] = 1;
}
// add the state if it's not been seen before
let stateKey = (country || '') + separator + state;
if (state && !(stateKey in visitedStates)) {
if (bottom_to_top)
display.push(country ? (state + display_separator + country) : state);
else
display.push(country ? (country + display_separator + state) : state);
raw.push(stateKey);
visitedStates[stateKey] = 1;
}
// all 3 fields at the same time
if (country && state && city) {
if (bottom_to_top)
display.push(city + display_separator + state + display_separator + country);
else
display.push(country + display_separator + state + display_separator + city);
raw.push(country + separator + state + separator + city);
}
// 2 fields at a time
else if (country && state) {
// case covered above
} else if (state && city) {
if (bottom_to_top)
display.push(state + display_separator + country);
else
display.push(country + display_separator + state);
raw.push(country + separator + state);
} else if (country && city) {
if (bottom_to_top)
display.push(city + display_separator + country);
else
display.push(country + display_separator + city);
raw.push(country + separator + separator + city);
}
// individual fields
else if (country) {
// already covered above
} else if (state) {
// also covered above
} else if (city) {
display.push(city);
raw.push(separator + separator + city);
}
}
return { "display": display, "raw": raw };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment