Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
82 lines (79 sloc) 20.3 KB
<!DOCTYPE html> <html> <head> <title>contract.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> contract.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <h2><a href="https://github.com/Raynos/contract">Github Repo</a></h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>A method that generates a function which will wrap the function <code>f</code> and
call the pre condition with (arg1, arg2, ...)</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">constructPre</span> <span class="o">=</span> <span class="kd">function</span> <span class="nx">_constructPre</span><span class="p">(</span><span class="nx">f</span><span class="p">,</span> <span class="nx">pre</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="kd">function</span> <span class="nx">_wrapped</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">pre</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">);</span>
<span class="k">return</span> <span class="nx">f</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">);</span>
<span class="p">};</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>A method that generates a function which will wrap the function and
call the post condition with (ret, arg1, arg2, ...)</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">constructPost</span> <span class="o">=</span> <span class="kd">function</span> <span class="nx">_constructPost</span><span class="p">(</span><span class="nx">f</span><span class="p">,</span> <span class="nx">post</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="kd">function</span> <span class="nx">_wrapped</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">arr</span> <span class="o">=</span> <span class="p">[</span><span class="nx">f</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">)];</span>
<span class="nx">post</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">arr</span><span class="p">.</span><span class="nx">concat</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">arr</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">));</span>
<span class="k">return</span> <span class="nx">arr</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
<span class="p">};</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>A method that generates a function which will wrap the function <code>f</code> and
call the invariant with (before, arg1, arg2, ...)</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">constructInvariant</span> <span class="o">=</span> <span class="kd">function</span> <span class="nx">_constructInvariant</span><span class="p">(</span><span class="nx">f</span><span class="p">,</span> <span class="nx">invariant</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="kd">function</span> <span class="nx">_wrapped</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">before</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="k">this</span><span class="p">));</span>
<span class="kd">var</span> <span class="nx">ret</span> <span class="o">=</span> <span class="nx">f</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">arr</span> <span class="o">=</span> <span class="p">[</span><span class="nx">before</span><span class="p">];</span>
<span class="nx">invariant</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">arr</span><span class="p">.</span><span class="nx">concat</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">arr</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">));</span>
<span class="k">return</span> <span class="nx">ret</span><span class="p">;</span>
<span class="p">};</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Handle multiple APIs. Either this was wrapped <code>construct(f).pre(pre)</code> or
This was called from the function prototype <code>f.pre(pre)</code> or this was
called from the Construct object <code>Construct.pre(pre, f)</code> </p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">handleMultipleAPIs</span> <span class="o">=</span> <span class="kd">function</span> <span class="nx">_handleMultipleAPIs</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">method</span><span class="p">,</span> <span class="nx">f</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">f</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span><span class="p">;</span>
<span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span> <span class="o">=</span> <span class="nx">method</span><span class="p">(</span><span class="nx">f</span><span class="p">,</span> <span class="nx">obj</span><span class="p">);</span>
<span class="k">return</span> <span class="k">this</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">f</span> <span class="o">===</span> <span class="kc">undefined</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">f</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
<span class="k">return</span> <span class="nx">method</span><span class="p">(</span><span class="nx">f</span><span class="p">,</span> <span class="nx">obj</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">method</span><span class="p">(</span><span class="nx">f</span><span class="p">,</span> <span class="nx">obj</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>The contract object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">Contract</span> <span class="o">=</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>handle multiple APIs. Pass the function &amp; pre conditions into constructPre</p> </td> <td class="code"> <div class="highlight"><pre> <span class="s2">&quot;pre&quot;</span><span class="o">:</span> <span class="kd">function</span> <span class="nx">_pre</span><span class="p">(</span><span class="nx">pre</span><span class="p">,</span> <span class="nx">f</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">handleMultipleAPIs</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">pre</span><span class="p">,</span> <span class="nx">constructPre</span><span class="p">,</span> <span class="nx">f</span><span class="p">);</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>handle multiple APIs. Pass the function &amp; post condition into constructPost</p> </td> <td class="code"> <div class="highlight"><pre> <span class="s2">&quot;post&quot;</span><span class="o">:</span> <span class="kd">function</span> <span class="nx">_post</span><span class="p">(</span><span class="nx">post</span><span class="p">,</span> <span class="nx">f</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">handleMultipleAPIs</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">post</span><span class="p">,</span> <span class="nx">constructPost</span><span class="p">,</span> <span class="nx">f</span><span class="p">);</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>handle multiple APIs. Pass the function &amp; invariant condition into constructInvariant</p> </td> <td class="code"> <div class="highlight"><pre> <span class="s2">&quot;invariant&quot;</span><span class="o">:</span> <span class="kd">function</span> <span class="nx">_invariant</span><span class="p">(</span><span class="nx">invariant</span><span class="p">,</span> <span class="nx">f</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">handleMultipleAPIs</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">invariant</span><span class="p">,</span> <span class="nx">constructInvariant</span><span class="p">,</span> <span class="nx">f</span><span class="p">);</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>unwrap the Constract object</p> </td> <td class="code"> <div class="highlight"><pre> <span class="s2">&quot;valueOf&quot;</span><span class="o">:</span> <span class="kd">function</span> <span class="nx">_valueOf</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>defaults object. By default inject into Function.prototype</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">defaults</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">natives</span><span class="o">:</span> <span class="kc">true</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>ContractFactory generates a new Contract based on the passed function <code>f</code></p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">ContractFactory</span> <span class="o">=</span> <span class="kd">function</span> <span class="nx">_create</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">c</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span><span class="nx">Contract</span><span class="p">);</span>
<span class="nx">c</span><span class="p">.</span><span class="nx">_wrapped</span> <span class="o">=</span> <span class="nx">f</span><span class="p">;</span>
<span class="k">return</span> <span class="nx">c</span><span class="p">;</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>contract = require("constract");
Sets up the contract object basedon the options.
If <code>natives === true</code> inject the methods into Function.prototype.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">defaults</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">options</span> <span class="o">&amp;&amp;</span> <span class="nx">options</span><span class="p">[</span><span class="nx">key</span><span class="p">])</span> <span class="p">{</span>
<span class="nx">defaults</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">options</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">defaults</span><span class="p">.</span><span class="nx">natives</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">defineProperties</span><span class="p">(</span><span class="nb">Function</span><span class="p">.</span><span class="nx">prototype</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">ContractFactory</span><span class="p">;</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p>define the properties on <code>ContractFactory</code></p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">defineProperties</span><span class="p">(</span><span class="nx">ContractFactory</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>Define the pre, post &amp; invariant objects onto an object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">defineProperties</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperties</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="p">{</span>
<span class="s2">&quot;pre&quot;</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="nx">Contract</span><span class="p">.</span><span class="nx">pre</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">true</span>
<span class="p">},</span>
<span class="s2">&quot;post&quot;</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="nx">Contract</span><span class="p">.</span><span class="nx">post</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">true</span>
<span class="p">},</span>
<span class="s2">&quot;invariant&quot;</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="nx">Contract</span><span class="p">.</span><span class="nx">invariant</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">true</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">}</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>