UNPKG

nochoices

Version:

Full featured implementation of options into typescript.

122 lines 182 kB
<!DOCTYPE html><html class="default" lang="en"><head><meta charSet="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>Option | nochoices</title><meta name="description" content="Documentation for nochoices"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os"</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><div class="table-cell" id="tsd-search" data-base=".."><div class="field"><label for="tsd-search-field" class="tsd-widget tsd-toolbar-icon search no-caption"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><use href="#icon-search"></use></svg></label><input type="text" id="tsd-search-field" aria-label="Search"/></div><div class="field"><div id="tsd-toolbar-links"></div></div><ul class="results"><li class="state loading">Preparing search index...</li><li class="state failure">The search index is not available</li></ul><a href="../index.html" class="title">nochoices</a></div><div class="table-cell" id="tsd-widgets"><a href="#" class="tsd-widget tsd-toolbar-icon menu no-caption" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><use href="#icon-menu"></use></svg></a></div></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb"><li><a href="../modules.html">nochoices</a></li><li><a href="Option.html">Option</a></li></ul><h1>Class Option&lt;T&gt;</h1></div><section class="tsd-panel tsd-comment"><div class="tsd-comment tsd-typography"><p>An Option<T> represents a value of type T that can be present ot not. Values inside options cannot be used directly, which ensures a safe data consumption.</p> <p>There are several ways to create an optional value:</p> </div><div class="tsd-comment tsd-typography"><h4>Example</h4><pre><code class="language-ts"><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">none</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">()</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">some</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-9">&#39;foo&#39;</span><span class="hl-2">)</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">nullable</span><span class="hl-2">: </span><span class="hl-7">string</span><span class="hl-2"> | </span><span class="hl-7">null</span><span class="hl-2"> = </span><span class="hl-9">&#39;bar&#39;</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">maybe</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">fromNullable</span><span class="hl-2">(</span><span class="hl-4">nullable</span><span class="hl-2">)</span> </code><button>Copy</button></pre> <p>An optional can also be created combining other optionals:</p> <h4>Example</h4><pre><code class="language-ts"><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">opt1</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">()</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">opt2</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-9">&#39;foo&#39;</span><span class="hl-2">)</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">opt3</span><span class="hl-2"> = </span><span class="hl-4">opt1</span><span class="hl-2">.</span><span class="hl-5">or</span><span class="hl-2">(</span><span class="hl-4">opt2</span><span class="hl-2">)</span> </code><button>Copy</button></pre> <p>Optional values can also perform operations</p> <h4>Example</h4><pre><code class="language-ts"><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">opt</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-9">&#39;foo&#39;</span><span class="hl-2">)</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">opt2</span><span class="hl-2"> = </span><span class="hl-4">opt</span><span class="hl-2">.</span><span class="hl-5">map</span><span class="hl-2">(</span><span class="hl-4">v</span><span class="hl-2"> </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-4">v</span><span class="hl-2"> + </span><span class="hl-9">&#39;bar&#39;</span><span class="hl-2">) </span><span class="hl-0">// === Some(&#39;foobar&#39;)</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">opt3</span><span class="hl-2"> = </span><span class="hl-4">opt</span><span class="hl-2">.</span><span class="hl-5">filter</span><span class="hl-2">(</span><span class="hl-4">v</span><span class="hl-2"> </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-4">v</span><span class="hl-2">.</span><span class="hl-4">length</span><span class="hl-2"> === </span><span class="hl-6">0</span><span class="hl-2">) </span><span class="hl-0">// === None</span> </code><button>Copy</button></pre> </div></section> <section class="tsd-panel"><h4>Type Parameters</h4><ul class="tsd-type-parameter-list"><li><h4><span class="tsd-kind-type-parameter">T</span></h4><div class="tsd-comment tsd-typography"><p>The type of the object wrapped by the optional</p> </div><div class="tsd-comment tsd-typography"></div></li></ul></section> <aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/hojarasca/nochoices/blob/6b4e920/src/option.ts#L52">option.ts:52</a></li></ul></aside><section class="tsd-panel-group tsd-index-group"><section class="tsd-panel tsd-index-panel"><details class="tsd-index-content tsd-index-accordion" open><summary class="tsd-accordion-summary tsd-index-summary"><h5 class="tsd-index-heading uppercase" role="button" aria-expanded="false" tabIndex="0"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><use href="#icon-chevronSmall"></use></svg> Index</h5></summary><div class="tsd-accordion-details"><section class="tsd-index-section"><h3 class="tsd-index-heading">Methods</h3><div class="tsd-index-list"><a href="Option.html#and" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>and</span></a> <a href="Option.html#andThen" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>and<wbr/>Then</span></a> <a href="Option.html#equals" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>equals</span></a> <a href="Option.html#equalsWith" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>equals<wbr/>With</span></a> <a href="Option.html#expect" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>expect</span></a> <a href="Option.html#filter" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>filter</span></a> <a href="Option.html#flatten" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>flatten</span></a> <a href="Option.html#getOrInsert" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>get<wbr/>Or<wbr/>Insert</span></a> <a href="Option.html#getOrInsertWith" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>get<wbr/>Or<wbr/>Insert<wbr/>With</span></a> <a href="Option.html#ifNone" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>if<wbr/>None</span></a> <a href="Option.html#ifSome" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>if<wbr/>Some</span></a> <a href="Option.html#insert" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>insert</span></a> <a href="Option.html#inspectContent" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>inspect<wbr/>Content</span></a> <a href="Option.html#isNone" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>is<wbr/>None</span></a> <a href="Option.html#isSome" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>is<wbr/>Some</span></a> <a href="Option.html#isSomeAnd" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>is<wbr/>Some<wbr/>And</span></a> <a href="Option.html#isSomeBut" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>is<wbr/>Some<wbr/>But</span></a> <a href="Option.html#map" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>map</span></a> <a href="Option.html#mapOr" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>map<wbr/>Or</span></a> <a href="Option.html#mapOrElse" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>map<wbr/>Or<wbr/>Else</span></a> <a href="Option.html#or" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>or</span></a> <a href="Option.html#orElse" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>or<wbr/>Else</span></a> <a href="Option.html#replace" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>replace</span></a> <a href="Option.html#take" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>take</span></a> <a href="Option.html#takeIf" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>take<wbr/>If</span></a> <a href="Option.html#toArray" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>to<wbr/>Array</span></a> <a href="Option.html#unwrap" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>unwrap</span></a> <a href="Option.html#unwrapOr" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>unwrap<wbr/>Or</span></a> <a href="Option.html#unwrapOrElse" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>unwrap<wbr/>Or<wbr/>Else</span></a> <a href="Option.html#xor" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>xor</span></a> <a href="Option.html#zip" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>zip</span></a> <a href="Option.html#zipWith" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>zip<wbr/>With</span></a> <a href="Option.html#None" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>None</span></a> <a href="Option.html#Some" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>Some</span></a> <a href="Option.html#fromNullable" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>from<wbr/>Nullable</span></a> </div></section></div></details></section></section><section class="tsd-panel-group tsd-member-group"><h2>Methods</h2><section class="tsd-panel tsd-member"><a id="and" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><span>and</span><a href="#and" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="and.and-1" class="tsd-anchor"></a><span class="tsd-kind-call-signature">and</span><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">V</span><span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">another</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="Option.html" class="tsd-signature-type tsd-kind-class">Option</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">V</span><span class="tsd-signature-symbol">&gt;</span><a href="#and.and-1" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-comment tsd-typography"><p>Returns a new option that is only present if both values (this, and the argument) are present. The value returned is the value contained in the option received as argument.</p> <p>This method behaves similar to the <code>&amp;&amp;</code> operator, but translated to optional values instead of booleans.</p> </div><section class="tsd-panel"><h4>Type Parameters</h4><ul class="tsd-type-parameter-list"><li><h4><span class="tsd-kind-type-parameter">V</span></h4><div class="tsd-comment tsd-typography"><p>The type of the value that the other Option may contain.</p> </div><div class="tsd-comment tsd-typography"></div></li></ul></section><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><h5><span class="tsd-kind-parameter">another</span>: <a href="Option.html" class="tsd-signature-type tsd-kind-class">Option</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">V</span><span class="tsd-signature-symbol">&gt;</span></h5><div class="tsd-comment tsd-typography"><p>Another optional value.</p> </div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <a href="Option.html" class="tsd-signature-type tsd-kind-class">Option</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">V</span><span class="tsd-signature-symbol">&gt;</span></h4><p>The result of applying an <code>and</code> operation between the 2 optionals.</p> <div class="tsd-comment tsd-typography"><h4>Example</h4><pre><code class="language-ts"><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">some</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-9">&#39;foo&#39;</span><span class="hl-2">)</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">another</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-6">5</span><span class="hl-2">)</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">result</span><span class="hl-2"> = </span><span class="hl-4">some</span><span class="hl-2">.</span><span class="hl-5">and</span><span class="hl-2">(</span><span class="hl-4">another</span><span class="hl-2">) </span><span class="hl-0">// Some(5)</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">none</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">()</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">result2</span><span class="hl-2"> = </span><span class="hl-4">none</span><span class="hl-2">.</span><span class="hl-5">and</span><span class="hl-2">(</span><span class="hl-4">another</span><span class="hl-2">) </span><span class="hl-0">// None</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">result3</span><span class="hl-2"> = </span><span class="hl-4">some</span><span class="hl-2">.</span><span class="hl-5">and</span><span class="hl-2">(</span><span class="hl-4">none</span><span class="hl-2">) </span><span class="hl-0">// None</span> </code><button>Copy</button></pre> </div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/hojarasca/nochoices/blob/6b4e920/src/option.ts#L439">option.ts:439</a></li></ul></aside></li></ul></section><section class="tsd-panel tsd-member"><a id="andThen" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><span>and<wbr/>Then</span><a href="#andThen" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="andThen.andThen-1" class="tsd-anchor"></a><span class="tsd-kind-call-signature">and<wbr/>Then</span><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">U</span><span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">fn</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="Option.html" class="tsd-signature-type tsd-kind-class">Option</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">U</span><span class="tsd-signature-symbol">&gt;</span><a href="#andThen.andThen-1" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-comment tsd-typography"><p>Similar to <a href="Option.html#and" class="tsd-kind-method"><code>and</code></a>, but allowing the second optional to be generated lazily. The provided fn is not executed if <code>this</code> is None.</p> </div><section class="tsd-panel"><h4>Type Parameters</h4><ul class="tsd-type-parameter-list"><li><h4><span class="tsd-kind-type-parameter">U</span></h4><div class="tsd-comment tsd-typography"><p>The type returned by the given function.</p> </div><div class="tsd-comment tsd-typography"></div></li></ul></section><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><h5><span class="tsd-kind-parameter">fn</span>: <a href="../types/TransformToOption.html" class="tsd-signature-type tsd-kind-type-alias">TransformToOption</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">, </span><span class="tsd-signature-type tsd-kind-type-parameter">U</span><span class="tsd-signature-symbol">&gt;</span></h5><div class="tsd-comment tsd-typography"><p>Function to generate the second option. It takes the content of the current instance as argument.</p> </div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <a href="Option.html" class="tsd-signature-type tsd-kind-class">Option</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">U</span><span class="tsd-signature-symbol">&gt;</span></h4><p>A new option only present if this is Some and the result of the function is Some. The value is the one returned by the function.</p> <div class="tsd-comment tsd-typography"><h4>Example</h4><pre><code class="language-ts"><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">some</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-6">5</span><span class="hl-2">)</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">newSome</span><span class="hl-2"> = </span><span class="hl-4">some</span><span class="hl-2">.</span><span class="hl-5">andThen</span><span class="hl-2">(</span><span class="hl-4">value</span><span class="hl-2"> </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-4">value</span><span class="hl-2"> * </span><span class="hl-6">2</span><span class="hl-2">)) </span><span class="hl-0">// Some(10)</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">none</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">&lt;</span><span class="hl-7">number</span><span class="hl-2">&gt;()</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">newNone</span><span class="hl-2"> = </span><span class="hl-4">none</span><span class="hl-2">.</span><span class="hl-5">andThen</span><span class="hl-2">(</span><span class="hl-4">value</span><span class="hl-2"> </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-4">value</span><span class="hl-2"> * </span><span class="hl-6">2</span><span class="hl-2">)) </span><span class="hl-0">// None</span> </code><button>Copy</button></pre> </div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/hojarasca/nochoices/blob/6b4e920/src/option.ts#L514">option.ts:514</a></li></ul></aside></li></ul></section><section class="tsd-panel tsd-member"><a id="equals" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><span>equals</span><a href="#equals" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="equals.equals-1" class="tsd-anchor"></a><span class="tsd-kind-call-signature">equals</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">another</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">boolean</span><a href="#equals.equals-1" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-comment tsd-typography"><p>Returns true if and only of both optionals are some and both have the same value. Comparison is done using <code>===</code>.</p> </div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><h5><span class="tsd-kind-parameter">another</span>: <a href="Option.html" class="tsd-signature-type tsd-kind-class">Option</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">&gt;</span></h5><div class="tsd-comment tsd-typography"><p>Another optional to compare with this</p> </div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">boolean</span></h4><div class="tsd-comment tsd-typography"><h4>Example</h4><pre><code class="language-ts"><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">().</span><span class="hl-5">equals</span><span class="hl-2">(</span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">()) </span><span class="hl-0">// true</span><br/><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-6">10</span><span class="hl-2">).</span><span class="hl-5">equals</span><span class="hl-2">(</span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">()) </span><span class="hl-0">// false</span><br/><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">().</span><span class="hl-5">equals</span><span class="hl-2">(</span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-9">&#39;foo&#39;</span><span class="hl-2">)) </span><span class="hl-0">// false</span><br/><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-9">&#39;bar&#39;</span><span class="hl-2">).</span><span class="hl-5">equals</span><span class="hl-2">(</span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-9">&#39;bar&#39;</span><span class="hl-2">)) </span><span class="hl-0">// true</span> </code><button>Copy</button></pre> </div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/hojarasca/nochoices/blob/6b4e920/src/option.ts#L864">option.ts:864</a></li></ul></aside></li></ul></section><section class="tsd-panel tsd-member"><a id="equalsWith" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><span>equals<wbr/>With</span><a href="#equalsWith" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="equalsWith.equalsWith-1" class="tsd-anchor"></a><span class="tsd-kind-call-signature">equals<wbr/>With</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">another</span>, <span class="tsd-kind-parameter">equality</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">boolean</span><a href="#equalsWith.equalsWith-1" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-comment tsd-typography"><p>Returns true if and only of both optionals are some and the provided equality check returns true.</p> </div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><h5><span class="tsd-kind-parameter">another</span>: <a href="Option.html" class="tsd-signature-type tsd-kind-class">Option</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">&gt;</span></h5><div class="tsd-comment tsd-typography"><p>Another optional to compare with this</p> </div><div class="tsd-comment tsd-typography"></div></li><li><h5><span class="tsd-kind-parameter">equality</span>: <a href="../types/AreEqual.html" class="tsd-signature-type tsd-kind-type-alias">AreEqual</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">&gt;</span></h5><div class="tsd-comment tsd-typography"><p>Function to compare content of both.</p> </div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">boolean</span></h4><div class="tsd-comment tsd-typography"><h4>Example</h4><pre><code class="language-ts"><span class="hl-0">// If both are None, the equality function is not even called</span><br/><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">().</span><span class="hl-5">equalsWith</span><span class="hl-2">(</span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">(), () </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-1">false</span><span class="hl-2">) </span><span class="hl-0">// true</span><br/><br/><span class="hl-0">// If one is some and the other is none, the equality function is not even called</span><br/><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-6">10</span><span class="hl-2">).</span><span class="hl-5">equalsWith</span><span class="hl-2">(</span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">(), () </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-1">true</span><span class="hl-2">) </span><span class="hl-0">// false</span><br/><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">().</span><span class="hl-5">equalsWith</span><span class="hl-2">(</span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-9">&#39;foo&#39;</span><span class="hl-2">), () </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-1">true</span><span class="hl-2">) </span><span class="hl-0">// false</span><br/><br/><span class="hl-0">// If both are some the equality function is called</span><br/><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-6">10</span><span class="hl-2">).</span><span class="hl-5">equalsWith</span><span class="hl-2">(</span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-6">15</span><span class="hl-2">), (</span><span class="hl-4">a</span><span class="hl-2">, </span><span class="hl-4">b</span><span class="hl-2">) </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-4">a</span><span class="hl-2"> % </span><span class="hl-6">5</span><span class="hl-2"> === </span><span class="hl-4">b</span><span class="hl-2"> % </span><span class="hl-6">5</span><span class="hl-2">) </span><span class="hl-0">// true</span><br/><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-6">7</span><span class="hl-2">).</span><span class="hl-5">equalsWith</span><span class="hl-2">(</span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-6">15</span><span class="hl-2">), (</span><span class="hl-4">a</span><span class="hl-2">, </span><span class="hl-4">b</span><span class="hl-2">) </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-4">a</span><span class="hl-2"> % </span><span class="hl-6">5</span><span class="hl-2"> === </span><span class="hl-4">b</span><span class="hl-2"> % </span><span class="hl-6">5</span><span class="hl-2">) </span><span class="hl-0">// false</span> </code><button>Copy</button></pre> </div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/hojarasca/nochoices/blob/6b4e920/src/option.ts#L889">option.ts:889</a></li></ul></aside></li></ul></section><section class="tsd-panel tsd-member"><a id="expect" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><span>expect</span><a href="#expect" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="expect.expect-1" class="tsd-anchor"></a><span class="tsd-kind-call-signature">expect</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">err</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><a href="#expect.expect-1" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-comment tsd-typography"><p>If there is a value present returns it, otherwise throws the error specified as argument.</p> <p>This is the right method to use when you an error should be raised if the optional is empty.</p> </div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><h5><span class="tsd-kind-parameter">err</span>: <span class="tsd-signature-type ">Error</span></h5><div class="tsd-comment tsd-typography"><p>The error to throw if the Option instance does not contain a value.</p> </div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type tsd-kind-type-parameter">T</span></h4><p>The value contained in the Option instance.</p> <div class="tsd-comment tsd-typography"><h4>Throws</h4><p>The provided error if the Option instance does not contain a value.</p> <h4>Example</h4><pre><code class="language-ts"><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">some</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-9">&#39;foo&#39;</span><span class="hl-2">)</span><br/><span class="hl-4">some</span><span class="hl-2">.</span><span class="hl-5">expect</span><span class="hl-2">(</span><span class="hl-1">new</span><span class="hl-2"> </span><span class="hl-5">Error</span><span class="hl-2">(</span><span class="hl-9">&#39;No value&#39;</span><span class="hl-2">)) </span><span class="hl-0">// &#39;foo&#39;</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">none</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">()</span><br/><span class="hl-4">none</span><span class="hl-2">.</span><span class="hl-5">expect</span><span class="hl-2">(</span><span class="hl-1">new</span><span class="hl-2"> </span><span class="hl-5">Error</span><span class="hl-2">(</span><span class="hl-9">&#39;No value&#39;</span><span class="hl-2">)) </span><span class="hl-0">// throws Error: &#39;No value&#39;</span> </code><button>Copy</button></pre> </div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/hojarasca/nochoices/blob/6b4e920/src/option.ts#L299">option.ts:299</a></li></ul></aside></li></ul></section><section class="tsd-panel tsd-member"><a id="filter" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><span>filter</span><a href="#filter" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="filter.filter-1" class="tsd-anchor"></a><span class="tsd-kind-call-signature">filter</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">fn</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="Option.html" class="tsd-signature-type tsd-kind-class">Option</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">&gt;</span><a href="#filter.filter-1" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-comment tsd-typography"><p>Filters the value contained in the Option instance using the provided predicate function.</p> <ul> <li>If the Option instance does not contain a value (None), it returns a new Option without a value.</li> <li>If the Option instance contains a value (Some) and the predicate function returns true when applied to the value, it returns a new Option with the same value.</li> <li>If the Option instance contains a value (Some) and the predicate function returns false when applied to the value, it returns a new Option without a value (None).</li> </ul> </div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><h5><span class="tsd-kind-parameter">fn</span>: <a href="../types/Predicate.html" class="tsd-signature-type tsd-kind-type-alias">Predicate</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">&gt;</span></h5><div class="tsd-comment tsd-typography"><p>The predicate function used to filter.</p> </div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <a href="Option.html" class="tsd-signature-type tsd-kind-class">Option</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">&gt;</span></h4><p>A new Option with the value if the predicate function returns true, otherwise an Option without a value.</p> <div class="tsd-comment tsd-typography"><h4>Example</h4><pre><code class="language-ts"><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">opt1</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-6">5</span><span class="hl-2">)</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">newOpt1</span><span class="hl-2"> = </span><span class="hl-4">opt1</span><span class="hl-2">.</span><span class="hl-5">filter</span><span class="hl-2">(</span><span class="hl-4">value</span><span class="hl-2"> </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-4">value</span><span class="hl-2"> &gt; </span><span class="hl-6">3</span><span class="hl-2">) </span><span class="hl-0">// Some(5)</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">opt2</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">&lt;</span><span class="hl-7">number</span><span class="hl-2">&gt;()</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">newOpt2</span><span class="hl-2"> = </span><span class="hl-4">opt2</span><span class="hl-2">.</span><span class="hl-5">filter</span><span class="hl-2">(</span><span class="hl-4">value</span><span class="hl-2"> </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-4">value</span><span class="hl-2"> &gt; </span><span class="hl-6">3</span><span class="hl-2">) </span><span class="hl-0">// None</span> </code><button>Copy</button></pre> </div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/hojarasca/nochoices/blob/6b4e920/src/option.ts#L278">option.ts:278</a></li></ul></aside></li></ul></section><section class="tsd-panel tsd-member"><a id="flatten" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><span>flatten</span><a href="#flatten" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="flatten.flatten-1" class="tsd-anchor"></a><span class="tsd-kind-call-signature">flatten</span><span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="Option.html" class="tsd-signature-type tsd-kind-class">Option</a><span class="tsd-signature-symbol">&lt;</span><a href="../types/FlattenOption.html" class="tsd-signature-type tsd-kind-type-alias">FlattenOption</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol">&gt;</span><a href="#flatten.flatten-1" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-comment tsd-typography"><p>Flattens nested options. An <code>Option&lt;Option&lt;T&gt;&gt;</code> returns an <code>Option&lt;T&gt;</code> with the same value inside (or no value in case of None).</p> <p>In case the option is not nested, it returns the same option.</p> <p>There is a type safer alternative to this method as an exported function <a href="../functions/flatten.html" class="tsd-kind-function">flatten</a></p> </div><h4 class="tsd-returns-title">Returns <a href="Option.html" class="tsd-signature-type tsd-kind-class">Option</a><span class="tsd-signature-symbol">&lt;</span><a href="../types/FlattenOption.html" class="tsd-signature-type tsd-kind-type-alias">FlattenOption</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol">&gt;</span></h4><p>Flatterned version of the option.</p> <div class="tsd-comment tsd-typography"><h4>Example</h4><pre><code class="language-ts"><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">some</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-9">&#39;foo&#39;</span><span class="hl-2">))</span><br/><span class="hl-4">some</span><span class="hl-2">.</span><span class="hl-5">flatten</span><span class="hl-2">() </span><span class="hl-0">// Some(&#39;foo&#39;)</span><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">none</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">()</span><br/><span class="hl-4">none</span><span class="hl-2">.</span><span class="hl-5">flatten</span><span class="hl-2">() </span><span class="hl-0">// None</span> </code><button>Copy</button></pre> </div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/hojarasca/nochoices/blob/6b4e920/src/option.ts#L321">option.ts:321</a></li></ul></aside></li></ul></section><section class="tsd-panel tsd-member"><a id="getOrInsert" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><span>get<wbr/>Or<wbr/>Insert</span><a href="#getOrInsert" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="getOrInsert.getOrInsert-1" class="tsd-anchor"></a><span class="tsd-kind-call-signature">get<wbr/>Or<wbr/>Insert</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">value</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><a href="#getOrInsert.getOrInsert-1" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-comment tsd-typography"><p>If the instance contains a value returns that value. If the instance is empty it inserts the value provided by argument, and returns the value.</p> <p>Notice that if the instance already had a value this method does not replace it and the argument is ignored.</p> </div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><h5><span class="tsd-kind-parameter">value</span>: <span class="tsd-signature-type tsd-kind-type-parameter">T</span></h5><div class="tsd-comment tsd-typography"><p>The value to be inserted and returned if the instance is None.</p> </div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type tsd-kind-type-parameter">T</span></h4><p>The value contained in the Option instance after the operation.</p> <div class="tsd-comment tsd-typography"><h4>Example</h4><pre><code class="language-ts"><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">opt1</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">()</span><br/><span class="hl-4">opt1</span><span class="hl-2">.</span><span class="hl-5">getOrInsert</span><span class="hl-2">(</span><span class="hl-9">&#39;foo&#39;</span><span class="hl-2">) </span><span class="hl-0">// &#39;foo&#39;</span><br/><span class="hl-4">opt1</span><span class="hl-2">.</span><span class="hl-5">isPresent</span><span class="hl-2">() </span><span class="hl-0">// true</span><br/><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">opt2</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-9">&#39;bar&#39;</span><span class="hl-2">)</span><br/><span class="hl-4">opt2</span><span class="hl-2">.</span><span class="hl-5">getOrInsert</span><span class="hl-2">(</span><span class="hl-9">&#39;foo&#39;</span><span class="hl-2">) </span><span class="hl-0">// &#39;bar&#39;</span> </code><button>Copy</button></pre> </div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/hojarasca/nochoices/blob/6b4e920/src/option.ts#L578">option.ts:578</a></li></ul></aside></li></ul></section><section class="tsd-panel tsd-member"><a id="getOrInsertWith" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><span>get<wbr/>Or<wbr/>Insert<wbr/>With</span><a href="#getOrInsertWith" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="getOrInsertWith.getOrInsertWith-1" class="tsd-anchor"></a><span class="tsd-kind-call-signature">get<wbr/>Or<wbr/>Insert<wbr/>With</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">fn</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><a href="#getOrInsertWith.getOrInsertWith-1" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-comment tsd-typography"><p>Similar to <a href="Option.html#insert" class="tsd-kind-method"><code>insert</code></a> but allowing the inserted value to be calculated lazily.</p> <p>If the instance is Some the value inside the option is present and the generator function is ignored.</p> <p>If the instance is None, the provided function is called, and the result is inserted into the instance and returned.</p> </div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><h5><span class="tsd-kind-parameter">fn</span>: <a href="../types/Generator.html" class="tsd-signature-type tsd-kind-type-alias">Generator</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">&gt;</span></h5><div class="tsd-comment tsd-typography"><p>Function to generate the value to insert and return in case of none.</p> </div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type tsd-kind-type-parameter">T</span></h4><p>The value contained in the Option instance after the operation.</p> <div class="tsd-comment tsd-typography"><h4>Example</h4><pre><code class="language-ts"><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">opt1</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">None</span><span class="hl-2">()</span><br/><span class="hl-4">opt1</span><span class="hl-2">.</span><span class="hl-5">getOrInsertWith</span><span class="hl-2">(() </span><span class="hl-1">=&gt;</span><span class="hl-2"> </span><span class="hl-9">&#39;foo&#39;</span><span class="hl-2">) </span><span class="hl-0">// &#39;foo&#39;</span><br/><span class="hl-4">opt1</span><span class="hl-2">.</span><span class="hl-5">isPresent</span><span class="hl-2">() </span><span class="hl-0">// true</span><br/><br/><span class="hl-1">const</span><span class="hl-2"> </span><span class="hl-3">opt2</span><span class="hl-2"> = </span><span class="hl-4">Option</span><span class="hl-2">.</span><span class="hl-5">Some</span><span class="hl-2">(</span><span class="hl-9">&#39;bar&#39;</span><span class="hl-2">)</span><br/><span class="hl-4">opt2</span><span