Skip to content

Instantly share code, notes, and snippets.

@vorandrew
Created February 25, 2018 21:08
Show Gist options
  • Save vorandrew/57e7fd4f1d2ccc185a512bb1afa0251c to your computer and use it in GitHub Desktop.
Save vorandrew/57e7fd4f1d2ccc185a512bb1afa0251c to your computer and use it in GitHub Desktop.
Group by flow
let data = _.flow(
_.groupBy('country_id'),
_.map(
_.flow( // city group by level
_.groupBy('city_id'),
_.map(
_.flow( // person group by level
_.groupBy('person_id'),
_.map(vals => {
return {
children: _.flow(
_.head,
_.get('child_name')
, vals),
brothers: _.flow(
_.head,
_.get('brother_name')
, vals),
sisters: _.flow(
_.head,
_.get('sister_name')
, vals),
}
}),
_.object({
city_id,
city_field_1,
city_field_2,
people, // from person group by level
})
),
_.object({
country_id,
country_field_1,
country_field_2,
people, // from city group by level
})
)
)
)
return getCountries()
.then(countries => {
return Promise.all(countries.map(country => {
return getCities(country)
.then(cities => {
return Promise.all(cities.map(city => {
return getPeople(city)
.then(people => {
return map.people(async person => {
return {
...person,
children: await getChildren(person),
brothers: await getBrothers(person),
sisters: await getSisters(person),
}
})
})
}))
}).then(cities_processed => {
country.cities = cities_processed
return country
})
})).then(countries_processed => {
return {
data: {
countries: countries_processed
}
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment