Skip to content

Instantly share code, notes, and snippets.

@sameoldmadness
Created September 17, 2014 19:15
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 sameoldmadness/2b96beb3c4530bdb5506 to your computer and use it in GitHub Desktop.
Save sameoldmadness/2b96beb3c4530bdb5506 to your computer and use it in GitHub Desktop.
Poor man's option type
/*
Usage example
option(document.findElementById('header'))
.map(function (header) {
header.textContent = 'Uh oh';
})
.orElse(function () {
document.body.insertAdjacentHTML('afterbegin', '<h1 id="header">Uh oh</h1>');
});
*/
var option = function (obj) {
return {
map: function (fn, thisArg) {
if (obj) {
fn.call(thisArg, obj);
}
return {
orElse: function (fn, thisArg) {
if (!obj) {
fn.call(thisArg);
}
}
}
}
}
}
// https://igor.io/2014/01/10/functional-library-null.html
// https://github.com/schmittjoh/php-option
// https://github.com/pr1001/option.js
// https://github.com/commuterjoy/js-scala-option
// https://github.com/shinypb/option.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment