Skip to content

Instantly share code, notes, and snippets.

@richardvanbergen
Last active October 21, 2015 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardvanbergen/2d8a7dc77084fcb09451 to your computer and use it in GitHub Desktop.
Save richardvanbergen/2d8a7dc77084fcb09451 to your computer and use it in GitHub Desktop.
A comparison of using lodash vs not using it.
/**
* With lodash
*/
const fetchDestinationsFor = (city, excludeUncommon, sortFn) => {
const chain = _.chain(city)
.result('destinations', [])
.filter(excludeUncommon ? (x => x.common == true) : (x => true))
.map(x => fetchBySlug(x.reference))
.sortBy(sortFn || (x => true));
return chain.value();
};
/**
* Without lodash
*/
const fetchDestinationsFor = (city, excludeUncommon, sortFn) => {
// get desintations
let references = [];
if (city && city.destinations) {
references = city.destinations;
}
// exclude uncommon destinations
if (excludeUncommon) {
var newReferences = [];
for (var i = 0; i < references.length; i++) {
if (references[i].common == true) {
newReferences.push(references[i])
}
}
references = newReferences;
}
// replace list of references with the actual destination object
// this can be replaced with map() if you don't have to support IE8
var formatted = [];
for (var i = 0; i < references.length; i++) {
formatted.push(fetchBySlug(x.reference))
}
// thankfully Array.sort() is widley supported
return formatted.sort(sortFn || (x => true));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment