daggy

Library for creating tagged constructors.

npm install daggy
3 downloads in the last week
9 downloads in the last month
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta name="generator" content="pandoc" />
  <title></title>
  <style type="text/css">code{white-space: pre;}</style>
  <style type="text/css">
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #40a070; }
code > span.fl { color: #40a070; }
code > span.ch { color: #4070a0; }
code > span.st { color: #4070a0; }
code > span.co { color: #60a0b0; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #06287e; }
code > span.er { color: #ff0000; font-weight: bold; }
  </style>
</head>
<body>
<h1 id="daggy">Daggy</h1>
<p>Library for creating tagged constructors.</p>
<h2 id="daggy.getinstanceself-constructor"><code>daggy.getInstance(self, constructor)</code></h2>
<p>Returns <code>self</code> if it's an <code>instanceof constructor</code>, otherwise creates a new object with <code>constructor</code>'s prototype.</p>
<p>Allows creating constructors that can be used with or without the new keyword but always have the correct prototype.</p>
<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">WrappedArray</span>() {
    <span class="kw">var</span> self = <span class="ot">daggy</span>.<span class="fu">getInstance</span>(<span class="kw">this</span>, WrappedArray);
    <span class="ot">self</span>.<span class="fu">_array</span> = [].<span class="ot">slice</span>.<span class="fu">apply</span>(arguments);
    <span class="kw">return</span> self;
}
<span class="kw">new</span> <span class="fu">WrappedArray</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>) <span class="kw">instanceof</span> WrappedArray; <span class="co">// true</span>
<span class="fu">WrappedArray</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>) <span class="kw">instanceof</span> WrappedArray; <span class="co">// true</span></code></pre>
<h2 id="daggy.taggedarguments"><code>daggy.tagged(arguments)</code></h2>
<p>Creates a new constructor with the given field names as arguments and properties. Allows <code>instanceof</code> checks with returned constructor.</p>
<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Tuple3 = <span class="ot">daggy</span>.<span class="fu">tagged</span>(<span class="st">&#39;x&#39;</span>, <span class="st">&#39;y&#39;</span>, <span class="st">&#39;z&#39;</span>);

<span class="kw">var</span> _123 = <span class="fu">Tuple3</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>); <span class="co">// optional new keyword</span>
<span class="ot">_123</span>.<span class="fu">x</span> == <span class="dv">1</span> &amp;&amp; <span class="ot">_123</span>.<span class="fu">y</span> == <span class="dv">2</span> &amp;&amp; <span class="ot">_123</span>.<span class="fu">z</span> == <span class="dv">3</span>; <span class="co">// true</span>
_123 <span class="kw">instanceof</span> Tuple3; <span class="co">// true</span></code></pre>
<h2 id="daggy.taggedsumconstructors"><code>daggy.taggedSum(constructors)</code></h2>
<p>Creates a constructor for each key in <code>constructors</code>. Returns a function with each constructor as a property. Allows <code>instanceof</code> checks for each constructor and the returned function.</p>
<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Option = <span class="ot">daggy</span>.<span class="fu">taggedSum</span>({
    <span class="dt">Some</span>: [<span class="st">&#39;x&#39;</span>],
    <span class="dt">None</span>: []
});

<span class="ot">Option</span>.<span class="fu">Some</span>(<span class="dv">1</span>) <span class="kw">instanceof</span> <span class="ot">Option</span>.<span class="fu">Some</span>; <span class="co">// true</span>
<span class="ot">Option</span>.<span class="fu">Some</span>(<span class="dv">1</span>) <span class="kw">instanceof</span> Option; <span class="co">// true</span>
<span class="ot">Option</span>.<span class="fu">None</span> <span class="fu">instanceof</span> <span class="fu">Option</span>; <span class="co">// true</span>

<span class="kw">function</span> <span class="fu">incOrZero</span>(o) {
    <span class="kw">return</span> <span class="ot">o</span>.<span class="fu">cata</span>({
        <span class="dt">Some</span>: <span class="kw">function</span>(x) {
            <span class="kw">return</span> x + <span class="dv">1</span>;
        },
        <span class="dt">None</span>: <span class="kw">function</span>() {
            <span class="kw">return</span> <span class="dv">0</span>;
        }
    });
}
<span class="fu">incOrZero</span>(<span class="ot">Option</span>.<span class="fu">Some</span>(<span class="dv">1</span>)); <span class="co">// 2</span>
<span class="fu">incOrZero</span>(<span class="ot">Option</span>.<span class="fu">None</span>); <span class="co">// 0</span></code></pre>
</body>
</html>
npm loves you