Last active
February 15, 2018 22:13
-
-
Save lavelle/670ef6567506966ffda9ed3a90a3eed5 to your computer and use it in GitHub Desktop.
Lodash Art Gallery
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
// Author: Giles | |
const datetimeComponents = _(matchResults).tail().map(_.toNumber).value(); | |
// Better than | |
var datetimeComponents = _.map( | |
matchResults.slice(1, 6), // skip the matched result | |
function(component) { | |
return parseInt(component, 10); | |
} | |
); |
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
const sanitizeData = (response) => { | |
return _.mergeWith( | |
..._(response.offices) | |
.filter(office => _.includes(OFFICES, office.name)) | |
.map(office => _.fromPairs( | |
_(office.departments) | |
.filter(dep => _.includes(DEPARTMENTS, dep.name)) | |
.map(dep => [dep.name, dep.jobs]).value(), | |
)).value(), | |
_.ary(_.concat, 2), | |
); | |
}; |
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
// Author: Giles | |
_.mixin({ | |
objectWith: (array, iterator) => _.zipObject(array, _.map(array, iterator)), | |
}); | |
const storages = ['local', 'session']; | |
module.exports = _.objectWith(storages, (name) => new Cache(`${name}Storage`)); | |
// Better than | |
module.exports = { | |
local: new Cache('localStorage'), | |
session: new Cache('sessionStorage'), | |
}; |
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
// Author: Giles | |
const cardSegments = _(cardNumber).split('').chunk(4).invokeMap('join', '').value(); | |
// Better than | |
const cardSegments = [ | |
cardNumber.substring(0, 4), | |
cardNumber.substring(4, 8), | |
cardNumber.substring(8, 12), | |
cardNumber.substring(12), | |
]; |
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
// Author: Giles | |
const BULLET_MASK = _.times(4, _.constant(_.repeat('\u2022', 4))).join(' '); | |
// Better than | |
const BULLET_MASK = '\u2022\u2022\u2022\u2022 \u2022\u2022\u2022\u2022 \u2022\u2022\u2022\u2022 \u2022\u2022\u2022\u2022'; |
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
const path = 'picture.sizes[100]'; | |
const getPicture = _.wrap(_.get, (fn, path) => { return fn($scope.author, path) }); | |
$scope.pictureSource = getPicture(`${path}.url`, getPicture(path)); | |
// Better than | |
if (_.has($scope.author, 'picture.sizes[100]')) { | |
const picture = $scope.author.picture.sizes[100]; | |
// Some synchronizers send an object, others send a string | |
$scope.pictureSource = picture.url || picture; | |
} |
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
// Author: Tim | |
_.zipObjects( | |
['setFullYear', 'setMonth', 'setDate', 'setHours', 'setMinutes'], | |
_.map(datetimeComponents, (c, i) => i === 1 ? c - 1 : c) | |
).forEach((param, method) => modelDate[method](param)) | |
// Better than | |
modelDate.setFullYear(datetimeComponents[0]); | |
modelDate.setMonth(datetimeComponents[1] - 1); // Only this is 0-indexed | |
modelDate.setDate(datetimeComponents[2]); | |
modelDate.setHours(datetimeComponents[3]); | |
modelDate.setMinutes(datetimeComponents[4]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment