Last active
February 17, 2017 23:38
-
-
Save jsonberry/71d10c27cc35266645a44ce75556987b to your computer and use it in GitHub Desktop.
Data latch utility for JSON using Lodash
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
/* | |
Sometimes I pass data into a transformation layer | |
I always expect the data to be a JSON object | |
None of the properties of the object should be undefined, null, empty objects, or empty arrays | |
If any of them are, then eject out of the transformation so the app doesn't have a chance to explode | |
*/ | |
// How to implement | |
function transformData(data) { | |
if (hasEmptryProps(data)) { | |
return | |
} | |
// Do stuff with data here | |
} | |
function hasEmptyProps(data) { | |
if (!_.isPlainObject(data) || _.isEmpty(data)) { | |
return true; | |
} | |
return _.chain(data) | |
.map() | |
.filter(_.isEmpty) | |
.reject(_.isNumber) | |
.value() | |
.length > 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment