Last active
August 29, 2015 14:16
-
-
Save taiansu/4cf1afb6df98b91e98f2 to your computer and use it in GitHub Desktop.
null_value_checker
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
//var _ = require('underscore'); | |
var goodData = {a: 1, b: 2, c: 3}; | |
var badData = {a: null, b: 1, c: 2}; | |
var noNullValue = function(data, checkKeys){ | |
// check all keys unless pass an array | |
checkKeys = checkKeys || _.keys(data); | |
// will be something like [true, false, false], true means there is a null value on our checkKeys. | |
var resultAry = _.map(data, function(val, key) { return _.contains(checkKeys, key) && _.isNull(val); }); | |
// if all false, no null value on our checkKeys. | |
return _.all(resultAry, function(val) { return !val; }); | |
}; | |
console.log(noNullValue(goodData)); | |
console.log(noNullValue(badData)); | |
console.log(noNullValue(badData, ["b", "c"])); |
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
good_data = {a: 1, b: 2, c: 3} | |
bad_data = {a: nil, b: 2, c: 3} | |
def no_null_value(data, check_keys = nil) | |
check_keys ||= data.keys | |
result_ary = data.map { |key, val| check_keys.include?(key) && val == nil} | |
result_ary.all? { |val| !val; } | |
end | |
p no_null_value(good_data) | |
p no_null_value(bad_data) | |
p no_null_value(bad_data, ["b", "c"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment