Skip to content

Instantly share code, notes, and snippets.

@timcharper
Created May 16, 2012 18:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timcharper/2712916 to your computer and use it in GitHub Desktop.
Save timcharper/2712916 to your computer and use it in GitHub Desktop.
function type(obj) {
var obj_type = typeof obj;
return({ is : function(type) { return obj_type === type; },
is_object : function() { return obj_type === "object"; },
is_func : function() { return obj_type === "function"; },
is_string : function() { return obj_type === "string"; }});
};
function maybe(item) {
return({
result: function(accessor, __default) {
if (!item)
return type(accessor).is_func() ? __default : accessor;
else
return type(accessor).is_func() ? accessor(item) : item;
},
select: function(accessor) {
if (!item)
return maybe(null);
else if (type(accessor).is_string())
return maybe(item[accessor]);
else
return maybe(accessor(item));
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment