Monet Maybe

HTML

<script src="https://cdn.rawgit.com/cwmyers/monet.js/master/src/main/javascript/monet.js"></script>
<body style="font-family: arial; background: white;">
<h1>Monet</h1>
<p>Javascript library with monad implementations.</p>
<p><a target="_blank" href="https://cwmyers.github.io/monet.js/">https://cwmyers.github.io/monet.js/</a></p>
<textarea id="output" style="width:100%; height:100em;"></textarea>
</body>

JavaScript

var output = "";

(function(couldBeNull) {

  try {

  // Main use
  // Check a value if it's "something".
	var unknown = Maybe.fromNull(couldBeNull);
  if (unknown.isSome()) {
    // Ok!
  } else {
    // KO!
  }

  // Some monad
  output += "\n" + Some(1).some();
  output += "\n" + None().isSome(); // False
  output += "\n";

  // All true
  output += "\n" + Maybe.fromNull(0).isSome();
  output += "\n" + Maybe.fromNull(null).isNone();
  output += "\n" + Maybe.fromNull(undefined).isNone();
  output += "\n";

  try {
    // Some illegal if none
    None().some();
  } catch (exception) {
    output += "\n" + exception;
    output += "\n";
  }

  // None or some
  output += "\n" + None().orSome("some");
  output += "\n";

  // Map to 'ab'
  output += "\n" + Some("a").map(function(value) { return value + "b"; }).some();
  output += "\n";

  } catch (exception) {
    output = exception;
  }

})("stuff");

// Print
document.getElementById("output").innerText = output;