UNPKG

lodash-fantasy

Version:

Fantasy Land compatible types built with lodash.

1,350 lines (1,001 loc) 63 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Lodash Fantasy</title> <meta name="description" content="Fantasy Land compatible types built with lodash." /> <meta property="og:title" content=""/> <meta property="og:type" content="website"/> <meta property="og:image" content=""/> <meta property="og:url" content=""/> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <script src="scripts/jquery.min.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/bootstrap.min.css"> <link type="text/css" rel="stylesheet" href="styles/jaguar.css"> <script> var config = {"monospaceLinks":true,"cleverLinks":false,"applicationName":"lodash-fantasy","collapseSymbols":false,"default":{"outputSourceFiles":true},"linenums":true,"meta":{"description":"Fantasy Land compatible types built with lodash.","title":"Lodash Fantasy"},"openGraph":{"type":"website"}}; </script> </head> <body> <div id="wrap" class="clearfix"> <div class="navigation"> <h3 class="applicationName"><a href="index.html">lodash-fantasy</a></h3> <div class="search"> <input id="search" type="text" class="form-control input-sm" placeholder="Search Documentations"> </div> <ul class="list"> <li class="item" data-name="Either"> <span class="title"> <a href="Either.html">Either</a> </span> <ul class="members itemMembers"> <span class="subtitle">Members</span> <li data-name="Either.all"><a href="Either.html#.all">all</a></li> <li data-name="Either.any"><a href="Either.html#.any">any</a></li> <li data-name="Either.each"><a href="Either.html#.each">each</a></li> <li data-name="Either.equals"><a href="Either.html#.equals">equals</a></li> <li data-name="Either.filter"><a href="Either.html#.filter">filter</a></li> <li data-name="Either.from"><a href="Either.html#.from">from</a></li> <li data-name="Either.isEither"><a href="Either.html#.isEither">isEither</a></li> <li data-name="Either.isLeft"><a href="Either.html#.isLeft">isLeft</a></li> <li data-name="Either.isRight"><a href="Either.html#.isRight">isRight</a></li> <li data-name="Either.Left"><a href="Either.html#.Left">Left</a></li> <li data-name="Either.map"><a href="Either.html#.map">map</a></li> <li data-name="Either.mapIn"><a href="Either.html#.mapIn">mapIn</a></li> <li data-name="Either.of"><a href="Either.html#.of">of</a></li> <li data-name="Either.reduce"><a href="Either.html#.reduce">reduce</a></li> <li data-name="Either.Right"><a href="Either.html#.Right">Right</a></li> <li data-name="Either.toMaybe"><a href="Either.html#.toMaybe">toMaybe</a></li> <li data-name="Either.toPromise"><a href="Either.html#.toPromise">toPromise</a></li> <li data-name="Either.toValidation"><a href="Either.html#.toValidation">toValidation</a></li> <li data-name="Either.try"><a href="Either.html#.try">try</a></li> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li data-name="Either#ap"><a href="Either.html#ap">ap</a></li> <li data-name="Either#bimap"><a href="Either.html#bimap">bimap</a></li> <li data-name="Either#chain"><a href="Either.html#chain">chain</a></li> <li data-name="Either#equals"><a href="Either.html#equals">equals</a></li> <li data-name="Either#extend"><a href="Either.html#extend">extend</a></li> <li data-name="Either#get"><a href="Either.html#get">get</a></li> <li data-name="Either#ifLeft"><a href="Either.html#ifLeft">ifLeft</a></li> <li data-name="Either#ifRight"><a href="Either.html#ifRight">ifRight</a></li> <li data-name="Either#isLeft"><a href="Either.html#isLeft">isLeft</a></li> <li data-name="Either#isRight"><a href="Either.html#isRight">isRight</a></li> <li data-name="Either#map"><a href="Either.html#map">map</a></li> <li data-name="Either#of"><a href="Either.html#of">of</a></li> <li data-name="Either#orElse"><a href="Either.html#orElse">orElse</a></li> <li data-name="Either#orElseGet"><a href="Either.html#orElseGet">orElseGet</a></li> <li data-name="Either#orElseThrow"><a href="Either.html#orElseThrow">orElseThrow</a></li> <li data-name="Either#toMaybe"><a href="Either.html#toMaybe">toMaybe</a></li> <li data-name="Either#toPromise"><a href="Either.html#toPromise">toPromise</a></li> <li data-name="Either#toString"><a href="Either.html#toString">toString</a></li> <li data-name="Either#toValidation"><a href="Either.html#toValidation">toValidation</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="Failure"> <span class="title"> <a href="Failure.html">Failure</a> </span> <ul class="members itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li data-name="Failure.from"><a href="Failure.html#.from">from</a></li> <li data-name="Failure#ap"><a href="Failure.html#ap">ap</a></li> <li data-name="Failure#bimap"><a href="Failure.html#bimap">bimap</a></li> <li data-name="Failure#chain"><a href="Failure.html#chain">chain</a></li> <li data-name="Failure#concat"><a href="Failure.html#concat">concat</a></li> <li data-name="Failure#equals"><a href="Failure.html#equals">equals</a></li> <li data-name="Failure#extend"><a href="Failure.html#extend">extend</a></li> <li data-name="Failure#ifFailure"><a href="Failure.html#ifFailure">ifFailure</a></li> <li data-name="Failure#ifSuccess"><a href="Failure.html#ifSuccess">ifSuccess</a></li> <li data-name="Failure#isFailure"><a href="Failure.html#isFailure">isFailure</a></li> <li data-name="Failure#isSuccess"><a href="Failure.html#isSuccess">isSuccess</a></li> <li data-name="Failure#map"><a href="Failure.html#map">map</a></li> <li data-name="Failure#of"><a href="Failure.html#of">of</a></li> <li data-name="Failure#orElse"><a href="Failure.html#orElse">orElse</a></li> <li data-name="Failure#orElseGet"><a href="Failure.html#orElseGet">orElseGet</a></li> <li data-name="Failure#orElseThrow"><a href="Failure.html#orElseThrow">orElseThrow</a></li> <li data-name="Failure#toEither"><a href="Failure.html#toEither">toEither</a></li> <li data-name="Failure#toMaybe"><a href="Failure.html#toMaybe">toMaybe</a></li> <li data-name="Failure#toPromise"><a href="Failure.html#toPromise">toPromise</a></li> <li data-name="Failure#toString"><a href="Failure.html#toString">toString</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="Just"> <span class="title"> <a href="Just.html">Just</a> </span> <ul class="members itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li data-name="Just.from"><a href="Just.html#.from">from</a></li> <li data-name="Just#ap"><a href="Just.html#ap">ap</a></li> <li data-name="Just#chain"><a href="Just.html#chain">chain</a></li> <li data-name="Just#equals"><a href="Just.html#equals">equals</a></li> <li data-name="Just#extend"><a href="Just.html#extend">extend</a></li> <li data-name="Just#get"><a href="Just.html#get">get</a></li> <li data-name="Just#ifJust"><a href="Just.html#ifJust">ifJust</a></li> <li data-name="Just#ifNothing"><a href="Just.html#ifNothing">ifNothing</a></li> <li data-name="Just#isJust"><a href="Just.html#isJust">isJust</a></li> <li data-name="Just#isNothing"><a href="Just.html#isNothing">isNothing</a></li> <li data-name="Just#map"><a href="Just.html#map">map</a></li> <li data-name="Just#of"><a href="Just.html#of">of</a></li> <li data-name="Just#orElse"><a href="Just.html#orElse">orElse</a></li> <li data-name="Just#orElseGet"><a href="Just.html#orElseGet">orElseGet</a></li> <li data-name="Just#orElseThrow"><a href="Just.html#orElseThrow">orElseThrow</a></li> <li data-name="Just#toEither"><a href="Just.html#toEither">toEither</a></li> <li data-name="Just#toPromise"><a href="Just.html#toPromise">toPromise</a></li> <li data-name="Just#toString"><a href="Just.html#toString">toString</a></li> <li data-name="Just#toValidation"><a href="Just.html#toValidation">toValidation</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="Left"> <span class="title"> <a href="Left.html">Left</a> </span> <ul class="members itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li data-name="Left.from"><a href="Left.html#.from">from</a></li> <li data-name="Left#ap"><a href="Left.html#ap">ap</a></li> <li data-name="Left#bimap"><a href="Left.html#bimap">bimap</a></li> <li data-name="Left#chain"><a href="Left.html#chain">chain</a></li> <li data-name="Left#equals"><a href="Left.html#equals">equals</a></li> <li data-name="Left#extend"><a href="Left.html#extend">extend</a></li> <li data-name="Left#get"><a href="Left.html#get">get</a></li> <li data-name="Left#ifLeft"><a href="Left.html#ifLeft">ifLeft</a></li> <li data-name="Left#ifRight"><a href="Left.html#ifRight">ifRight</a></li> <li data-name="Left#isLeft"><a href="Left.html#isLeft">isLeft</a></li> <li data-name="Left#isRight"><a href="Left.html#isRight">isRight</a></li> <li data-name="Left#map"><a href="Left.html#map">map</a></li> <li data-name="Left#of"><a href="Left.html#of">of</a></li> <li data-name="Left#orElse"><a href="Left.html#orElse">orElse</a></li> <li data-name="Left#orElseGet"><a href="Left.html#orElseGet">orElseGet</a></li> <li data-name="Left#orElseThrow"><a href="Left.html#orElseThrow">orElseThrow</a></li> <li data-name="Left#toMaybe"><a href="Left.html#toMaybe">toMaybe</a></li> <li data-name="Left#toPromise"><a href="Left.html#toPromise">toPromise</a></li> <li data-name="Left#toString"><a href="Left.html#toString">toString</a></li> <li data-name="Left#toValidation"><a href="Left.html#toValidation">toValidation</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="Maybe"> <span class="title"> <a href="Maybe.html">Maybe</a> </span> <ul class="members itemMembers"> <span class="subtitle">Members</span> <li data-name="Maybe.all"><a href="Maybe.html#.all">all</a></li> <li data-name="Maybe.any"><a href="Maybe.html#.any">any</a></li> <li data-name="Maybe.each"><a href="Maybe.html#.each">each</a></li> <li data-name="Maybe.equals"><a href="Maybe.html#.equals">equals</a></li> <li data-name="Maybe.filter"><a href="Maybe.html#.filter">filter</a></li> <li data-name="Maybe.from"><a href="Maybe.html#.from">from</a></li> <li data-name="Maybe.isJust"><a href="Maybe.html#.isJust">isJust</a></li> <li data-name="Maybe.isMaybe"><a href="Maybe.html#.isMaybe">isMaybe</a></li> <li data-name="Maybe.isNothing"><a href="Maybe.html#.isNothing">isNothing</a></li> <li data-name="Maybe.Just"><a href="Maybe.html#.Just">Just</a></li> <li data-name="Maybe.map"><a href="Maybe.html#.map">map</a></li> <li data-name="Maybe.mapIn"><a href="Maybe.html#.mapIn">mapIn</a></li> <li data-name="Maybe.Nothing"><a href="Maybe.html#.Nothing">Nothing</a></li> <li data-name="Maybe.of"><a href="Maybe.html#.of">of</a></li> <li data-name="Maybe.ofNullable"><a href="Maybe.html#.ofNullable">ofNullable</a></li> <li data-name="Maybe.reduce"><a href="Maybe.html#.reduce">reduce</a></li> <li data-name="Maybe.toEither"><a href="Maybe.html#.toEither">toEither</a></li> <li data-name="Maybe.toPromise"><a href="Maybe.html#.toPromise">toPromise</a></li> <li data-name="Maybe.toValidation"><a href="Maybe.html#.toValidation">toValidation</a></li> <li data-name="Maybe.try"><a href="Maybe.html#.try">try</a></li> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li data-name="Maybe#ap"><a href="Maybe.html#ap">ap</a></li> <li data-name="Maybe#chain"><a href="Maybe.html#chain">chain</a></li> <li data-name="Maybe#equals"><a href="Maybe.html#equals">equals</a></li> <li data-name="Maybe#extend"><a href="Maybe.html#extend">extend</a></li> <li data-name="Maybe#get"><a href="Maybe.html#get">get</a></li> <li data-name="Maybe#ifJust"><a href="Maybe.html#ifJust">ifJust</a></li> <li data-name="Maybe#ifNothing"><a href="Maybe.html#ifNothing">ifNothing</a></li> <li data-name="Maybe#isJust"><a href="Maybe.html#isJust">isJust</a></li> <li data-name="Maybe#isNothing"><a href="Maybe.html#isNothing">isNothing</a></li> <li data-name="Maybe#map"><a href="Maybe.html#map">map</a></li> <li data-name="Maybe#of"><a href="Maybe.html#of">of</a></li> <li data-name="Maybe#orElse"><a href="Maybe.html#orElse">orElse</a></li> <li data-name="Maybe#orElseGet"><a href="Maybe.html#orElseGet">orElseGet</a></li> <li data-name="Maybe#orElseThrow"><a href="Maybe.html#orElseThrow">orElseThrow</a></li> <li data-name="Maybe#toEither"><a href="Maybe.html#toEither">toEither</a></li> <li data-name="Maybe#toPromise"><a href="Maybe.html#toPromise">toPromise</a></li> <li data-name="Maybe#toString"><a href="Maybe.html#toString">toString</a></li> <li data-name="Maybe#toValidation"><a href="Maybe.html#toValidation">toValidation</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="Nothing"> <span class="title"> <a href="Nothing.html">Nothing</a> </span> <ul class="members itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li data-name="Nothing.from"><a href="Nothing.html#.from">from</a></li> <li data-name="Nothing#ap"><a href="Nothing.html#ap">ap</a></li> <li data-name="Nothing#chain"><a href="Nothing.html#chain">chain</a></li> <li data-name="Nothing#equals"><a href="Nothing.html#equals">equals</a></li> <li data-name="Nothing#extend"><a href="Nothing.html#extend">extend</a></li> <li data-name="Nothing#get"><a href="Nothing.html#get">get</a></li> <li data-name="Nothing#ifJust"><a href="Nothing.html#ifJust">ifJust</a></li> <li data-name="Nothing#ifNothing"><a href="Nothing.html#ifNothing">ifNothing</a></li> <li data-name="Nothing#isJust"><a href="Nothing.html#isJust">isJust</a></li> <li data-name="Nothing#isNothing"><a href="Nothing.html#isNothing">isNothing</a></li> <li data-name="Nothing#map"><a href="Nothing.html#map">map</a></li> <li data-name="Nothing#of"><a href="Nothing.html#of">of</a></li> <li data-name="Nothing#orElse"><a href="Nothing.html#orElse">orElse</a></li> <li data-name="Nothing#orElseGet"><a href="Nothing.html#orElseGet">orElseGet</a></li> <li data-name="Nothing#orElseThrow"><a href="Nothing.html#orElseThrow">orElseThrow</a></li> <li data-name="Nothing#toEither"><a href="Nothing.html#toEither">toEither</a></li> <li data-name="Nothing#toPromise"><a href="Nothing.html#toPromise">toPromise</a></li> <li data-name="Nothing#toString"><a href="Nothing.html#toString">toString</a></li> <li data-name="Nothing#toValidation"><a href="Nothing.html#toValidation">toValidation</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="Right"> <span class="title"> <a href="Right.html">Right</a> </span> <ul class="members itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li data-name="Right.from"><a href="Right.html#.from">from</a></li> <li data-name="Right#ap"><a href="Right.html#ap">ap</a></li> <li data-name="Right#bimap"><a href="Right.html#bimap">bimap</a></li> <li data-name="Right#chain"><a href="Right.html#chain">chain</a></li> <li data-name="Right#equals"><a href="Right.html#equals">equals</a></li> <li data-name="Right#extend"><a href="Right.html#extend">extend</a></li> <li data-name="Right#get"><a href="Right.html#get">get</a></li> <li data-name="Right#ifLeft"><a href="Right.html#ifLeft">ifLeft</a></li> <li data-name="Right#ifRight"><a href="Right.html#ifRight">ifRight</a></li> <li data-name="Right#isLeft"><a href="Right.html#isLeft">isLeft</a></li> <li data-name="Right#isRight"><a href="Right.html#isRight">isRight</a></li> <li data-name="Right#map"><a href="Right.html#map">map</a></li> <li data-name="Right#of"><a href="Right.html#of">of</a></li> <li data-name="Right#orElse"><a href="Right.html#orElse">orElse</a></li> <li data-name="Right#orElseGet"><a href="Right.html#orElseGet">orElseGet</a></li> <li data-name="Right#orElseThrow"><a href="Right.html#orElseThrow">orElseThrow</a></li> <li data-name="Right#toMaybe"><a href="Right.html#toMaybe">toMaybe</a></li> <li data-name="Right#toPromise"><a href="Right.html#toPromise">toPromise</a></li> <li data-name="Right#toString"><a href="Right.html#toString">toString</a></li> <li data-name="Right#toValidation"><a href="Right.html#toValidation">toValidation</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="Success"> <span class="title"> <a href="Success.html">Success</a> </span> <ul class="members itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li data-name="Success.from"><a href="Success.html#.from">from</a></li> <li data-name="Success#ap"><a href="Success.html#ap">ap</a></li> <li data-name="Success#bimap"><a href="Success.html#bimap">bimap</a></li> <li data-name="Success#chain"><a href="Success.html#chain">chain</a></li> <li data-name="Success#concat"><a href="Success.html#concat">concat</a></li> <li data-name="Success#equals"><a href="Success.html#equals">equals</a></li> <li data-name="Success#extend"><a href="Success.html#extend">extend</a></li> <li data-name="Success#ifFailure"><a href="Success.html#ifFailure">ifFailure</a></li> <li data-name="Success#ifSuccess"><a href="Success.html#ifSuccess">ifSuccess</a></li> <li data-name="Success#isFailure"><a href="Success.html#isFailure">isFailure</a></li> <li data-name="Success#isSuccess"><a href="Success.html#isSuccess">isSuccess</a></li> <li data-name="Success#map"><a href="Success.html#map">map</a></li> <li data-name="Success#of"><a href="Success.html#of">of</a></li> <li data-name="Success#orElse"><a href="Success.html#orElse">orElse</a></li> <li data-name="Success#orElseGet"><a href="Success.html#orElseGet">orElseGet</a></li> <li data-name="Success#orElseThrow"><a href="Success.html#orElseThrow">orElseThrow</a></li> <li data-name="Success#toEither"><a href="Success.html#toEither">toEither</a></li> <li data-name="Success#toMaybe"><a href="Success.html#toMaybe">toMaybe</a></li> <li data-name="Success#toPromise"><a href="Success.html#toPromise">toPromise</a></li> <li data-name="Success#toString"><a href="Success.html#toString">toString</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="Validation"> <span class="title"> <a href="Validation.html">Validation</a> </span> <ul class="members itemMembers"> <span class="subtitle">Members</span> <li data-name="Validation.all"><a href="Validation.html#.all">all</a></li> <li data-name="Validation.any"><a href="Validation.html#.any">any</a></li> <li data-name="Validation.concat"><a href="Validation.html#.concat">concat</a></li> <li data-name="Validation.each"><a href="Validation.html#.each">each</a></li> <li data-name="Validation.empty"><a href="Validation.html#.empty">empty</a></li> <li data-name="Validation.equals"><a href="Validation.html#.equals">equals</a></li> <li data-name="Validation.Failure"><a href="Validation.html#.Failure">Failure</a></li> <li data-name="Validation.filter"><a href="Validation.html#.filter">filter</a></li> <li data-name="Validation.from"><a href="Validation.html#.from">from</a></li> <li data-name="Validation.isFailure"><a href="Validation.html#.isFailure">isFailure</a></li> <li data-name="Validation.isSuccess"><a href="Validation.html#.isSuccess">isSuccess</a></li> <li data-name="Validation.isValidation"><a href="Validation.html#.isValidation">isValidation</a></li> <li data-name="Validation.map"><a href="Validation.html#.map">map</a></li> <li data-name="Validation.mapIn"><a href="Validation.html#.mapIn">mapIn</a></li> <li data-name="Validation.of"><a href="Validation.html#.of">of</a></li> <li data-name="Validation.reduce"><a href="Validation.html#.reduce">reduce</a></li> <li data-name="Validation.Success"><a href="Validation.html#.Success">Success</a></li> <li data-name="Validation.toEither"><a href="Validation.html#.toEither">toEither</a></li> <li data-name="Validation.toMaybe"><a href="Validation.html#.toMaybe">toMaybe</a></li> <li data-name="Validation.toPromise"><a href="Validation.html#.toPromise">toPromise</a></li> <li data-name="Validation.try"><a href="Validation.html#.try">try</a></li> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li data-name="Validation#ap"><a href="Validation.html#ap">ap</a></li> <li data-name="Validation#bimap"><a href="Validation.html#bimap">bimap</a></li> <li data-name="Validation#chain"><a href="Validation.html#chain">chain</a></li> <li data-name="Validation#concat"><a href="Validation.html#concat">concat</a></li> <li data-name="Validation#equals"><a href="Validation.html#equals">equals</a></li> <li data-name="Validation#extend"><a href="Validation.html#extend">extend</a></li> <li data-name="Validation#ifFailure"><a href="Validation.html#ifFailure">ifFailure</a></li> <li data-name="Validation#ifSuccess"><a href="Validation.html#ifSuccess">ifSuccess</a></li> <li data-name="Validation#isFailure"><a href="Validation.html#isFailure">isFailure</a></li> <li data-name="Validation#isSuccess"><a href="Validation.html#isSuccess">isSuccess</a></li> <li data-name="Validation#map"><a href="Validation.html#map">map</a></li> <li data-name="Validation#of"><a href="Validation.html#of">of</a></li> <li data-name="Validation#orElse"><a href="Validation.html#orElse">orElse</a></li> <li data-name="Validation#orElseGet"><a href="Validation.html#orElseGet">orElseGet</a></li> <li data-name="Validation#orElseThrow"><a href="Validation.html#orElseThrow">orElseThrow</a></li> <li data-name="Validation#toEither"><a href="Validation.html#toEither">toEither</a></li> <li data-name="Validation#toMaybe"><a href="Validation.html#toMaybe">toMaybe</a></li> <li data-name="Validation#toPromise"><a href="Validation.html#toPromise">toPromise</a></li> <li data-name="Validation#toString"><a href="Validation.html#toString">toString</a></li> </ul> <ul class="events itemMembers"> </ul> </li> </ul> </div> <div class="main"> <h1 class="page-title" data-filename="data_Maybe.js.html">Source: data/Maybe.js</h1> <section> <article> <pre id="source-code" class="prettyprint source linenums"><code>"use strict"; // Third Party const include = require("include")(__dirname); const stream = require("lodash/fp"); // Third Party Aliases const concat = stream.concat; const curry = stream.curry; const each = stream.each; const filter = stream.filter; const find = stream.find; const flow = stream.flow; const get = stream.get; const isEqual = stream.isEqual; const isNull = stream.isNull; const isUndefined = stream.isUndefined; const map = stream.map; const negate = stream.negate; const reduce = stream.reduce; // Project const invokeIn = include("src/invokeIn"); /** * The {@link Maybe} type is intended for values that may or may not be null or undefined. It is a disjunction similar * to &lt;code>Either&lt;/code>. The key difference of the {@link Maybe} type is the focus on a value or nothing. Much like * &lt;code>Either&lt;/code>, {@link Maybe} is right-biased. * @param {*} value - Value to wrap. * @return {Maybe} {@link Maybe} wrapped &lt;code>value&lt;/code>. * @example &lt;caption>Via &lt;code>new&lt;/code>&lt;/caption> * * const v1 = new Just(value); * const v2 = new Nothing(); * * @example &lt;caption>Via function&lt;/caption> * * const v3 = Just.from(value); * const v4 = Nothing.from(); * * @example &lt;caption>Via Maybe function&lt;/caption> * * const getOr = require("lodash/fp/getOr"); * const Maybe = require("lodash-fantasy/data/Maybe"); * * function getValue(path, context) { * return getOr(Maybe.Nothing.from(), path, context); * } * * module.exports = getValue; */ class Maybe { /** * @static * @property {Just} Just - Maybe just. */ static get Just() { return Just; } /** * @static * @property {Nothing} Nothing - Maybe nothing. */ static get Nothing() { return Nothing; } /** * Returns a {@link Maybe} that resolves all of the maybes in the collection into a single Maybe. * @static * @member * @param {Maybe[]} maybes - Collection of maybes. * @return {Maybe} A {@link Maybe} representing all {@link Just} values or a singular {@link Nothing}. * @example * * const m1 = getArbitraryProperty(context1); * // => Just(context1) * * const m2 = getArbitraryProperty(context2); * // => Just(context2) * * const m3 = getArbitraryProperty(context3); * // => Nothing() * * const m4 = getArbitraryProperty(context4); * // => Nothing() * * Maybe.all([m1, m2]); * // => Just([context1, context2]) * * Maybe.all([m1, m2, m3]); * // => Nothing() * * Maybe.all([m1, m2, m3, m4]); * // => Nothing() */ static all(maybes) { return find(Maybe.isNothing, maybes) || Maybe.of(stream(maybes).map(get("value")).reduce(concat, [])); } /** * Returns the first {@link Just} in the collection or finally a {@link Nothing}. * @static * @member * @param {Maybe[]} maybes - Collection of maybes. * @return {Maybe} First {@link Just} or finally a {@link Nothing}. * @example * * const m1 = getArbitraryProperty(context1); * // => Just(context1) * * const m2 = getArbitraryProperty(context2); * // => Just(context2) * * const m3 = getArbitraryProperty(context3); * // => Nothing() * * const m4 = getArbitraryProperty(context4); * // => Nothing() * * Maybe.any([m1, m2]); * // => Just(context1) * * Maybe.any([m2, m3]); * // => Just(context2) * * Maybe.any([m3, m4]); * // => Nothing() */ static any(maybes) { return find(Maybe.isJust, maybes) || new Nothing(); } /** * Creates a new {@link Maybe} from a &lt;code>value&lt;/code>. If the &lt;code>value&lt;/code> is already a {@link Maybe} * instance, the &lt;code>value&lt;/code> is returned unchanged. Otherwise, a new {@link Just} is made with the * &lt;code>value&lt;/code>. * @static * @member * @param {*} value - Value to wrap in a {@link Maybe}. * @return {Maybe} {@link Maybe} when is the &lt;code>value&lt;/code> already wrapped or {@link Just} wrapped * &lt;code>value&lt;/code>. * * Maybe.from(); * // => Just() * * Maybe.from(true); * // => Just(true) * * Maybe.from(Just.from(value)); * // => Just(value) * * Maybe.from(Nothing.from()); * // => Nothing() */ static from(value) { return this.isMaybe(value) ? value : this.ofNullable(value); } /** * Determines whether or not the value is a {@link Just}. * @static * @member * @param {*} value - Value to check. * @return {Boolean} &lt;code>true&lt;/code> for {@link Just}; &lt;code>false&lt;/code> for {@link Nothing}. * @example * * isJust(); * // => false * * isJust(null); * // => false * * isJust(Just.from()); * // => true * * isJust(Nothing.from()); * // => false */ static isJust(value) { return value instanceof Just; } /** * Determines whether or not the value is a {@link Maybe}. * @static * @member * @param {*} value - Value to check. * @return {Boolean} &lt;code>true&lt;/code> for {@link Maybe}; &lt;code>false&lt;/code> for anything else. * @example * * isMaybe(); * // => false * * isMaybe(null); * // => false * * isMaybe(Just.from()); * // => true * * isMaybe(Nothing.from()); * // => true */ static isMaybe(value) { return value instanceof Maybe; } /** * Determines whether or not the value is a {@link Nothing}. * @static * @member * @param {*} value - Value to check. * @return {Boolean} &lt;code>true&lt;/code> for {@link Nothing}; &lt;code>false&lt;/code> for {@link Just}. * @example * * isNothing(); * // => false * * isNothing(null); * // => false * * isNothing(Nothing.from()); * // => true * * isNothing(Just.from()); * // => false */ static isNothing(value) { return value instanceof Nothing; } /** * Wraps the &lt;code>value&lt;/code> in a {@link Just}. No parts of &lt;code>value&lt;/code> are checked. * @static * @member * @param {*} value - Value to wrap. * @return {Just} {@link Just} wrapped &lt;code>value&lt;/code>. * @example * * Maybe.of(); * // => Just() * * Maybe.of(true); * // => Just(true) * * Maybe.of(Just.from(value)); * // => Just(Just(value)) * * Maybe.of(Nothing.from()); * // => Just(Nothing()) */ static of(value) { return new Just(value); } /** * Wraps the &lt;code>value&lt;/code> in a {@link Just} if the value is not &lt;code>null&lt;/code>, &lt;code>undefined&lt;/code>, or * {@link Nothing}. * @static * @member * @param {*} value - Value to wrap. * @return {Maybe} {@link Just} wrapped &lt;code>value&lt;/code> or {@link Nothing}. * @example * * Maybe.ofNullable(); * // => Nothing() * * Maybe.ofNullable(null); * // => Nothing() * * Maybe.ofNullable(true); * // => Just(true) * * Maybe.ofNullable(Just.from(value)); * // => Just(Just(value)) * * Maybe.ofNullable(Nothing.from()); * // => Nothing() */ static ofNullable(value) { return isNull(value) || isUndefined(value) || Maybe.isNothing(value) ? new Nothing() : new Just(value); } /** * Tries to invoke a &lt;code>supplier&lt;/code>. The result of the &lt;code>supplier&lt;/code> is returned in a * {@link Just}. If an exception is thrown, a {@link Nothing} is returned. The &lt;code>function&lt;/code> takes no * arguments. * @static * @member * @param {Supplier} supplier - Function to invoke. * @return {Maybe} {@link Just} wrapped supplier result or {@link Nothing} wrapped &lt;code>error&lt;/code>. * @example * * Maybe.try(normalFunction); * // => Just(returnValue) * * Maybe.try(throwableFunction); * // => Nothing() */ static try(method) { try { return Just.from(method()); } catch (error) { return Nothing.from(); } } constructor(value) { this.value = value; } /** * Applies the function contained in the instance of a {@link Just} to the value contained in the provided * {@link Just}, producing a {@link Just} containing the result. If the instance is a {@link Nothing}, the result * is the {@link Nothing} instance. If the instance is a {@link Just} and the provided {@link Maybe} is * {@link Nothing}, the result is the provided {@link Nothing}. * @abstract * @function ap * @memberof Maybe * @instance * @param {Maybe} other - Value to apply to the function wrapped in the {@link Just}. * @return {Maybe} {@link Just} wrapped applied function or {@link Nothing}. * @example &lt;caption>Just#ap&lt;/caption> * * const findPerson = curryN(3, Person.find); // Person.find(name, birthdate, address) * * Just.from(findPerson) // => Just(findPerson) * .ap(Just.ofNullable(name)) // => Just(name) * .ap(Just.ofNullable(birthdate)) // => Just(birthdate) * .ap(Just.ofNullable(address)) // => Just(address) * .ifJust(console.log); // => Log Person.find() response */ /** * Applies the provided function to the value contained for a {@link Just}. The function should return the value * wrapped in a {@link Maybe}. If the instance is a {@link Nothing}, the function is ignored and then instance is * returned unchanged. * @abstract * @function chain * @memberof Maybe * @instance * @param {Chain.&lt;Maybe>} method - The function to invoke with the value. * @return {Maybe} {@link Maybe} wrapped value returned by the provided &lt;code>method&lt;/code>. * @example &lt;caption>Just#chain&lt;/caption> * * // Using lodash/fp/curry and get * const getConfigOption = curry((path, config) => Maybe.ofNullable(get(path, config)); * * Maybe.ofNullable(config) * .chain(getConfigOption("path.to.option")) */ /** * Determines whether or not the &lt;code>other&lt;/code> is equal in value to the current (&lt;code>this&lt;/code>). This is * &lt;strong>not&lt;/strong> a reference check. * @param {*} other - Other value to check. * @return {Boolean} &lt;code>true&lt;/code> if the two Maybes are equal; &lt;code>false&lt;/code> if not equal. * @example &lt;caption>Reflexivity&lt;/caption> * * v1.equals(v1) === true; * // => true * * @example &lt;caption>Symmetry&lt;/caption> * * v1.equals(v2) === v2.equals(v1); * // => true * * @example &lt;caption>Transitivity&lt;/caption> * * (v1.equals(v2) === v2.equals(v3)) &amp;&amp; v1.equals(v3) * // => true */ equals(other) { return isEqual(this, other); } /** * Extends the Maybe. This is used for workflow continuation where the context has shifted. * @abstract * @function extend * @memberof Maybe * @instance * @param {Extend.&lt;Maybe>} - method - The function to invoke with the value. * @return {Maybe} * @example &lt;caption>Workflow continuation&lt;/caption> * * // Workflow from makeRequest.js * const makeRequest = requestOptions => requestAsPromise(requestOptions) * .then(Just.from) * .catch(Nothing.from); * * // Workflow from savePerson.js * const savePerson = curry((requestOptions, optionalPerson) => optionalPerson * .map(Person.from) * .map(person => set("body", person, requestOptions)) * .map(makeRequest) * ); * * // Workflow from processResponse.js * const processResponse = optionalResponse => optionalResponse * .ifJust(console.log); * * Maybe.ofNullable(person) * .extend(savePerson({ method: "POST" })) * .extend(processResponse); */ /** * Returns the value if the instance is a {@link Just} otherwise the &lt;code>null&lt;/code>. * @function get * @memberof Maybe * @instance * @return {*} * @example &lt;caption>Just#get&lt;/caption> * * Just.from(value).get(); * // => value * * @example &lt;caption>Nothing#get&lt;/caption> * * Nothing.from().get(); * // => null */ get() { return this.value; } /** * Applies the provided function to the value contain for a {@link Just}. Any return value from the function is * ignored. If the instance is a {@link Nothing}, the function is ignored and the instance is returned. * @abstract * @function ifJust * @memberof Maybe * @instance * @param {Consumer} method - The function to invoke with the value. * @return {Maybe} Current instance. * @example &lt;caption>Just#ifJust&lt;/caption> * * Just.from(value).ifJust(doSomething); // doSomething(value) * // => Just(value) * * @example &lt;caption>Nothing#ifJust&lt;/caption> * * Nothing.from().ifJust(doSomething); // void * // => Nothing() */ /** * Applies the provided function to the value contain for a {@link Nothing}. Any return value from the function is * ignored. If the instance is a {@link Just}, the function is ignored and the instance is returned. * @abstract * @function ifNothing * @memberof Maybe * @instance * @param {Callable} method - The function to invoke. * @return {Maybe} Current instance. * @example &lt;caption>Just#ifNothing&lt;/caption> * * Just.from(value).ifNothing(doSomething); // void * // => Just(value) * * @example &lt;caption>Nothing#ifNothing&lt;/caption> * * Nothing.from().ifNothing(doSomething); // doSomething() * // => Nothing() */ /** * Determines whether or not the instance is a {@link Nothing}. * @return {Boolean} &lt;code>true&lt;/code> if the instance is a {@link Nothing}; &lt;code>false&lt;/code> is not. * @example &lt;caption>Just#isNothing&lt;/caption> * * Just.from(value).isNothing(); * // => false * * @example &lt;caption>Nothing#isNothing&lt;/caption> * * Nothing.from().isNothing(); * // => true */ isNothing() { return this instanceof Nothing; } /** * Determines whether or not the instance is a {@link Just}. * @return {Boolean} &lt;code>true&lt;/code> if the instance is a {@link Just}; &lt;code>false&lt;/code> is not. * @example &lt;caption>Just&lt;/caption> * * Just.from(value).isNothing(); * // => true * * @example &lt;caption>Nothing#isJust&lt;/caption> * * Nothing.from().isNothing(); * // => false */ isJust() { return this instanceof Just; } /** * Applies the provided function to the value contained for a {@link Just} which is, in turn, wrapped in a * {@link Just}. If the instance is a {@link Nothing}, the function is ignored and then instance is returned * unchanged. * @abstract * @function map * @memberof Maybe * @instance * @param {Function} method - The function to invoke with the value. * @return {Maybe} {@link Maybe} wrapped value mapped with the provided &lt;code>method&lt;/code>. * @example * * // Using lodash/fp/flow and sort * Just.from([1, 3, 2]).map(flow(sort, join(", "))); * // => Just("1, 2, 3") * * Nothing.from().map(flow(sort, join(", "))); * // => Nothing() */ /** * @see Maybe.ofNullable */ of(value) { return Maybe.of(value); } /** * Returns the value if the instance is a {@link Just} otherwise returns the value supplied if the instance is a * {@link Nothing}. * @abstract * @function orElse * @memberof Maybe * @instance * @param {Consumer} method - The function to invoke with the value. * @return {*} * @example &lt;caption>Just#orElse&lt;/caption> * * Just.from(value).orElse(otherValue); * // => value * * @example &lt;caption>Nothing#orElse&lt;/caption> * * Nothing.from().orElse(otherValue); * // => otherValue */ /** * Return the value if the instance is a {@link Just} otherwise returns the value from the function provided. * @abstract * @function orElseGet * @memberof Maybe * @instance * @param {Supplier} method - The function supplying the optional value. * @return {*} * @example &lt;caption>Just#orElseGet&lt;/caption> * * Just.from(value).orElseGet(getOtherValue); * // => value * * @example &lt;caption>Nothing#orElseGet&lt;/caption> * * Nothing.from().orElseGet(getOtherValue); * // => otherValue */ /** * Returns the value if the instance is a {@link Just} otheriwse throws the &lt;code>Error&lt;/code> supplied by the * function provided. * @abstract * @function orElseThrow * @memberof Maybe * @instance * @param {Supplier} method - The function to invoke with the value. * @return {*} * @throws {Error} returned by the provided function. * @example &lt;caption>Just#orElseThrow&lt;/caption> * * Just.from(value).orElseThrow(createException); * // => value * * @example &lt;caption>Nothing#orElseThrow&lt;/caption> * * Nothing.from().orElseThrow(createException); // throw createException() */ /** * Converts the {@link Maybe} to an {@link Either}. {@link Just} becomes a {@link Right} and {@link Nothing} becomes a * {@link Left}. * @abstract * @function toEither * @memberof Maybe * @instance * @param {Either} either - Either implementation. * @return {Either} {@link Either} wrapped &lt;code>value&lt;/code>. * @example &lt;caption>Just#toEither&lt;/caption> * * const Either = require("lodash-fantasy/data/Either"); * * Just.from(value).toEither(Either); * // => Either.Right(value); * * @example &lt;caption>Nothing#toEither&lt;/caption> * * const Either = require("lodash-fantasy/data/Either"); * * Nothing.from().toEither(Either); * // => Either.Left(null); */ /** * Converts the Maybe to a &lt;code>Promise&lt;/code> using the provided &lt;code>Promise&lt;/code> implementation. * @abstract * @function toPromise * @memberof Maybe * @instance * @param {Promise} promise - Promise implementation. * @return {Promise} &lt;code>Promise&lt;/code> wrapped &lt;code>value&lt;/code>. * @example &lt;caption>Just#toPromise&lt;/caption> * * const Bluebird = require("bluebird"); * * Just.from(value).toPromise(Bluebird); * // => Promise.resolve(value); * * @example &lt;caption>Nothing#toPromise&lt;/caption> * * const