• Jump To … +
    Either.js List.js Maybe.js Set.js Stream.js Thunk.js Tuple.js boolean.js char.js base.js extra.js fold.js map.js zipWith.js collection.js control.js curried.js eq.js floating.js fractional.js function.js integral.js match.js num.js numeric.js object.js ord.js realfrac.js register.js string.js type.js nodash.js
  • Maybe.js

  • ¶
    /* vim: set et sw=2 ts=2: */
    'use strict';
    
    module.exports = [ 'idf', 'is', 'compose', 'compose2', 'filter', 'map', 'List', 'singleton', 'emptyList',
      function (idf, is, compose, compose2, filter, map, List, singleton, emptyList) {
    
      function isJust(thing) {
        return thing !== null && thing !== undefined;
      }
    
      return {
    
        maybe: function (def, fun, value) {
          if (value === null || value === undefined) {
            return def;
          }
          return fun(value);
        },
    
        isJust: isJust,
    
        isNothing: function (thing) {
          return !isJust(thing);
        },
    
        fromMaybe: function (def, maybe) {
          if (isJust(maybe)) {
            return maybe;
          }
          return def;
        },
    
        listToMaybe: function (xs) {
          if (is(List, xs)) {
            return xs.isEmpty() ? null : xs.head();
          }
          throw new TypeError();
        },
    
        maybeToList: function (thing) {
          if (isJust(thing)) {
            return singleton(thing);
          }
          return emptyList();
        },
    
        catMaybes: filter(isJust),
    
        mapMaybe: compose2(filter(isJust), map)
          
      };
      
    }];