Skip to content

Instantly share code, notes, and snippets.

@jsonberry
Last active February 17, 2017 23:38
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 jsonberry/71d10c27cc35266645a44ce75556987b to your computer and use it in GitHub Desktop.
Save jsonberry/71d10c27cc35266645a44ce75556987b to your computer and use it in GitHub Desktop.
Data latch utility for JSON using Lodash
/*
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