Skip to content

Instantly share code, notes, and snippets.

@taiansu
Last active August 29, 2015 14:16
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 taiansu/4cf1afb6df98b91e98f2 to your computer and use it in GitHub Desktop.
Save taiansu/4cf1afb6df98b91e98f2 to your computer and use it in GitHub Desktop.
null_value_checker
//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"]));
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