Created
April 20, 2011 01:48
-
-
Save jimfleming/930167 to your computer and use it in GitHub Desktop.
returns an object describing any missing attributes nested within an object
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
_.mixin({ | |
missing: function(obj, requirements) { | |
var missing = {}; | |
_.each(requirements, function(requirement, field) { | |
if (_.isNull(obj[field]) || _.isUndefined(obj[field])) { | |
missing[field] = false; | |
return; | |
} | |
if (typeof obj[field] === 'object') { | |
potential = _.missing(obj[field], requirement); | |
if (typeof potential !== 'null' && (typeof potential == 'object' && _.size(potential) > 0)) { | |
missing[field] = potential; | |
} | |
} | |
}); | |
return missing; | |
} | |
}); | |
var context = { one: {}, two: { one: 'ONE', two: 'TWO', three: 'THREE' } }; | |
var requirements = { one: { two: true } }; | |
var missing = _.missing(context, requirements); | |
console.log(missing); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment