Created
May 16, 2012 18:41
-
-
Save timcharper/2712916 to your computer and use it in GitHub Desktop.
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
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