UNPKG

specify-assertions

Version:

Beautiful assertion library.

238 lines (177 loc) 6.02 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Specify (assertions) Source: index.js</title> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css"> <link type="text/css" rel="stylesheet" href="styles/site.cerulean.css"> </head> <body> <div class="container-fluid"> <div class="navbar navbar-fixed-top "> <div class="navbar-inner"> <a class="brand" href="index.html">Specify (assertions)</a> <ul class="nav"> <li class="dropdown"> <a href="namespaces.list.html" class="dropdown-toggle" data-toggle="dropdown">Namespaces<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="divergence.Divergence_.html">Divergence</a> </li> </ul> </li> <li class="dropdown"> <a href="modules.list.html" class="dropdown-toggle" data-toggle="dropdown">Modules<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="divergence.html">specify-assertions/lib/divergence</a> </li> <li> <a href="index_.html">specify-assertions/lib/index</a> </li> <li> <a href="validations.html">specify-assertions/lib/validations</a> </li> </ul> </li> </ul> </div> </div> <div class="row-fluid"> <div class="span12"> <div id="main"> <h1 class="page-title">Source: index.js</h1> <section> <article> <pre class="sunlight-highlight-javascript linenums">/** * Beautiful assertion library. * * @module specify-assertions/lib/index */ // -- Dependencies ----------------------------------------------------- var AssertionError = require('assertion-error') var Future = require('data.future') var identity = require('core.lambda').identity var compose = require('core.lambda').compose var curry = require('core.lambda').curry var extend = require('boo').extend // -- Helpers ---------------------------------------------------------- /** * Raises an exception. * * @private * @summary Error → Void :: partial, throws */ function raise(error) { throw error } /** * Transforms a divergence into an assertion error. * * @private * @summary Divergence → AssertionError */ function toError(divergence) { return new AssertionError( 'Expected ' + divergence.toString() , divergence ) } // -- Assertion primitives --------------------------------------------- /** * Verifies if an assertion is correct. * * @method * @summary α → (α → Validation[Divergence, Divergence]) → Divergence :: partial, throws */ exports.verify = curry(2, verify) function verify(a, checker) { return checker(a).fold( compose(raise, toError) , identity) } /** * Verifies if an assertion is correct asynchronously, for Promises/A+. * * @method * @summary Promise[Error, α] → (α → Validation[Divergence, Divergence]) → Promise[AssertionError, Divergence] */ exports.verifyPromise = curry(2, verifyPromise) function verifyPromise(promise, checker) { return promise.then(function(a) { return verify(a, checker) }) } /** * Verifies if an assertion is correct asynchronously, for fantasy-land monads. * * @method * @summary (m:Monad[_]) => m[α] → (α → Validation[Divergence, Divergence]) → m[Validation[AssertionError, Divergence]] */ exports.verifyMonad = curry(2, verifyMonad) function verifyMonad(monad, checker) { return monad.map(function(a) { return checker(a).bimap(toError, identity) }) } /** * Verifies if an assertion is correct asynchronously, for monadic Futures. * * @method * @summary Future[α, β] → (α → Validation[Divergence, Divergence]) → Future[AssertionError, Divergence] */ exports.verifyFuture = curry(2, verifyFuture) function verifyFuture(future, checker) { return verifyMonad(future, checker) .chain(function(a){ return new Future(propagate(a)) }) function propagate(a){ return function(reject, resolve) { return a.fold(reject, resolve) }} } // -- Other exports ---------------------------------------------------- extend(exports, require('./validations')) exports.divergence = require('./divergence')</pre> </article> </section> </div> <div class="clearfix"></div> <footer> <span class="copyright"> © 2014 Origami Tower </span> <br /> <span class="jsdoc-message"> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha9</a> on Thu Dec 25 2014 14:27:25 GMT-0200 (BRST) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>. </span> </footer> </div> <br clear="both"> </div> </div> <script src="scripts/sunlight.js"></script> <script src="scripts/sunlight.javascript.js"></script> <script src="scripts/sunlight-plugin.doclinks.js"></script> <script src="scripts/sunlight-plugin.linenumbers.js"></script> <script src="scripts/sunlight-plugin.menu.js"></script> <script src="scripts/jquery.min.js"></script> <script src="scripts/jquery.scrollTo.js"></script> <script src="scripts/jquery.localScroll.js"></script> <script src="scripts/bootstrap-dropdown.js"></script> <script src="scripts/toc.js"></script> <script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script> <script> $( function () { $( "#toc" ).toc( { anchorName : function(i, heading, prefix) { return $(heading).attr("id") || ( prefix + i ); }, selectors : "h1,h2,h3,h4", showAndHide : false, scrollTo : 60 } ); $( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" ); $( "#main span[id^='toc']" ).addClass( "toc-shim" ); } ); </script> </body> </html>