UNPKG

monad-ts

Version:

Monad-ts is a small library implements some of key monads and way to chain them in a pipe (flow) in JavaScript and TypeScript. Angular 2+ compatible.

187 lines (175 loc) 9.22 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../../"> <title data-ice="title">src/services/equality.js | API Document</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> </head> <body class="layout-container" data-ice="rootContainer"> <header> <a href="./">Home</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <a data-ice="repoURL" href="https://github.com/sash-ua/monad-ts" class="repo-url-github">Repository</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> </header> <nav class="navigation" data-ice="nav"><div> <ul> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/asyncFlow.js~AsyncFlow.html">AsyncFlow</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/either.js~Either.html">Either</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/error.js~ErrorM.html">ErrorM</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/flow.js~Flow.html">Flow</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/identity.js~Identity.html">Identity</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/list.js~List.html">List</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/maybe.js~Maybe.html">Maybe</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/monad.js~Monad.html">Monad</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/state.js~State.html">State</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">interfaces</div><span data-ice="kind" class="kind-interface">I</span><span data-ice="name"><span><a href="class/src/interfaces/m.js~M.html">M</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">services</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-cast">cast</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-clone">clone</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-debounceTime">debounceTime</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-equality">equality</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-hash">hash</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-wait">wait</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">types</div><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-D&lt;T&gt;">D&lt;T&gt;</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-MF&lt;T,U&gt;">MF&lt;T,U&gt;</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-Pr&lt;U&gt;">Pr&lt;U&gt;</a></span></span></li> </ul> </div> </nav> <div class="content" data-ice="content"><h1 data-ice="title">src/services/equality.js</h1> <pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">/** * It checks equality of given arguments, arguments must be statically analyzable, therefore there are some constraints, * look at examples to find them. * * @example &lt;caption&gt;1)&lt;/caption&gt; * Functions compare by the structure, not by values of variables or other elements it consists of. * * Equal: * let d = 20; * equality( * ()=&gt;{return ()=&gt; {return {&apos;g&apos;: d}};}, * function(){return function() {return{&apos;g&apos;: d}};} * ) * * Not Equal: * let d = 20; * let d2 = 20; * equality( * ()=&gt;{return ()=&gt; {return {&apos;g&apos;: d}};}, * function(){return function() {return{&apos;g&apos;: d2}};} * ) * * @example &lt;caption&gt;2)&lt;/caption&gt; * Do not use the creation of some objects by object creation, they will be compared wrong. * Never use this constructions in compared objects: * new Boolean(*); * new Number(*); * Error(*); * new Error(*); * new Date(*); * new RegExp(*); * * Equal: * equality(new Boolean(true), new Boolean(false)); // Wrong * equality(Error(&apos;true&apos;), Error(&apos;false&apos;)); // Wrong * equality(new Number(1), new Number(11)); // Wrong * equality(new Date(1995, 11, 17), new Date(&apos;1995-12-17T03:24:00&apos;)) // Wrong * * Not Equal (the exception of `new` option in some cases can solve the issue): * equality(Boolean(true), Boolean(false)); // Right * equality(Number(1), Number(11)); // Right * * @example &lt;caption&gt;3)&lt;/caption&gt; * Instances of a user-defined object type that has a constructor function are compared as objects by `key: value`. * * class Test{ * constructor(private arg: any){ } * } * class Test2{ * constructor(private arg: any){ } * } * Equal: * new Test(true) and new Test(true); * * Not Equal: * new Test(true) and new Test2(true); * new Test(true) and new Test(false); * * @method equality * @param {any} x - argument 1, can include null, NaN etc. * @param {any} y - argument 2, can include null, NaN etc. * @returns {boolean} */ export function equality(x, y) { const isXO = new Object(x); const isYO = new Object(y); if (isXO !== x &amp;&amp; isYO !== y) { return Object.is(x, y); } if (isXO === x &amp;&amp; isYO === y) { const xN = x.constructor; const yN = y.constructor; if (xN === yN) { if (xN === Array &amp;&amp; yN === Array) { return x.length !== y.length ? false : _arrayIterator(x, y); } else if (xN === Function &amp;&amp; yN === Function) { return x.toString() === y.toString(); } else if (equality(Object.getOwnPropertyNames(x), Object.getOwnPropertyNames(y))) { for (let key in x) { if (x.hasOwnProperty(key)) { if (!equality(x[key], y[key])) return false; } } return true; } } else { return false; } } else { return false; } } /** * Tterate array controller. * @method _arrayIterator * @param {Array&lt;any&gt;} x * @param {Array&lt;any&gt;} y * @return {boolean} * @private */ function _arrayIterator(x, y) { return !x.some((v, i) =&gt; { return Array.isArray(v) ? _arrayIterator(v, y[i]) : (typeof v === &apos;object&apos; || typeof v === &quot;function&quot;) ? !equality(v, y[i]) : !Object.is(v, y[i]); }); } //Copyright (c) 2017 Alex Tranchenko. All rights reserved. </code></pre> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.5.2)</span><img src="./image/esdoc-logo-mini-black.png"></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>