Last active
October 21, 2015 11:17
-
-
Save richardvanbergen/2d8a7dc77084fcb09451 to your computer and use it in GitHub Desktop.
A comparison of using lodash vs not using it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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