| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
<head> |
| 5 |
<title>util.js</title> |
| 6 |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 7 |
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> |
| 8 |
<link rel="stylesheet" media="all" href="../docco.css" /> |
| 9 |
</head> |
| 10 |
<body> |
| 11 |
<div id="container"> |
| 12 |
<div id="background"></div> |
| 13 |
|
| 14 |
<ul id="jump_to"> |
| 15 |
<li> |
| 16 |
<a class="large" href="javascript:void(0);">Jump To …</a> |
| 17 |
<a class="small" href="javascript:void(0);">+</a> |
| 18 |
<div id="jump_wrapper"> |
| 19 |
<div id="jump_page_wrapper"> |
| 20 |
<div id="jump_page"> |
| 21 |
|
| 22 |
|
| 23 |
<a class="source" href="tuple.html"> |
| 24 |
src/tuple.js |
| 25 |
</a> |
| 26 |
|
| 27 |
|
| 28 |
<a class="source" href="universal-weak-map.html"> |
| 29 |
src/universal-weak-map.js |
| 30 |
</a> |
| 31 |
|
| 32 |
|
| 33 |
<a class="source" href="util.html"> |
| 34 |
src/util.js |
| 35 |
</a> |
| 36 |
|
| 37 |
</div> |
| 38 |
</div> |
| 39 |
</li> |
| 40 |
</ul> |
| 41 |
|
| 42 |
<ul class="sections"> |
| 43 |
|
| 44 |
<li id="title"> |
| 45 |
<div class="annotation"> |
| 46 |
<h1>util.js</h1> |
| 47 |
</div> |
| 48 |
</li> |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
<li id="section-1"> |
| 53 |
<div class="annotation"> |
| 54 |
|
| 55 |
<div class="pilwrap "> |
| 56 |
<a class="pilcrow" href="#section-1">¶</a> |
| 57 |
</div> |
| 58 |
<p>Although <code>Symbol</code> is widely supported these days, we can safely fall |
| 59 |
back to using a non-enumerable string property without violating any |
| 60 |
assumptions elsewhere in the implementation.</p> |
| 61 |
|
| 62 |
</div> |
| 63 |
|
| 64 |
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">const</span> useSymbol = <span class="hljs-keyword">typeof</span> <span class="hljs-built_in">Symbol</span> === <span class="hljs-string">"function"</span>;</pre></div></div> |
| 65 |
|
| 66 |
</li> |
| 67 |
|
| 68 |
|
| 69 |
<li id="section-2"> |
| 70 |
<div class="annotation"> |
| 71 |
|
| 72 |
<div class="pilwrap "> |
| 73 |
<a class="pilcrow" href="#section-2">¶</a> |
| 74 |
</div> |
| 75 |
<p>Used to mark <code>tuple.prototype</code> so that all objects that inherit from |
| 76 |
any <code>tuple.prototype</code> object (there could be more than one) will test |
| 77 |
positive according to <code>tuple.isTuple</code>.</p> |
| 78 |
|
| 79 |
</div> |
| 80 |
|
| 81 |
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> brand = useSymbol |
| 82 |
? <span class="hljs-built_in">Symbol</span>.for(<span class="hljs-string">"immutable-tuple"</span>) |
| 83 |
: <span class="hljs-string">"@@__IMMUTABLE_TUPLE__@@"</span>;</pre></div></div> |
| 84 |
|
| 85 |
</li> |
| 86 |
|
| 87 |
|
| 88 |
<li id="section-3"> |
| 89 |
<div class="annotation"> |
| 90 |
|
| 91 |
<div class="pilwrap "> |
| 92 |
<a class="pilcrow" href="#section-3">¶</a> |
| 93 |
</div> |
| 94 |
<p>Used to save a reference to the globally shared <code>UniversalWeakMap</code> that |
| 95 |
stores all known <code>tuple</code> objects.</p> |
| 96 |
|
| 97 |
</div> |
| 98 |
|
| 99 |
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> globalKey = useSymbol |
| 100 |
? <span class="hljs-built_in">Symbol</span>.for(<span class="hljs-string">"immutable-tuple-root"</span>) |
| 101 |
: <span class="hljs-string">"@@__IMMUTABLE_TUPLE_ROOT__@@"</span>;</pre></div></div> |
| 102 |
|
| 103 |
</li> |
| 104 |
|
| 105 |
|
| 106 |
<li id="section-4"> |
| 107 |
<div class="annotation"> |
| 108 |
|
| 109 |
<div class="pilwrap "> |
| 110 |
<a class="pilcrow" href="#section-4">¶</a> |
| 111 |
</div> |
| 112 |
<p>The <code>mustConvertThisToArray</code> value is true when the corresponding |
| 113 |
<code>Array</code> method does not attempt to modify <code>this</code>, which means we can |
| 114 |
pass a <code>tuple</code> object as <code>this</code> without first converting it to an |
| 115 |
<code>Array</code>.</p> |
| 116 |
|
| 117 |
</div> |
| 118 |
|
| 119 |
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">forEachArrayMethod</span>(<span class="hljs-params">fn</span>) </span>{ |
| 120 |
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">call</span>(<span class="hljs-params">name, mustConvertThisToArray</span>) </span>{ |
| 121 |
<span class="hljs-keyword">const</span> desc = <span class="hljs-built_in">Object</span>.getOwnPropertyDescriptor(<span class="hljs-built_in">Array</span>.prototype, name); |
| 122 |
fn(name, desc, !! mustConvertThisToArray); |
| 123 |
} |
| 124 |
|
| 125 |
call(<span class="hljs-string">"every"</span>); |
| 126 |
call(<span class="hljs-string">"filter"</span>); |
| 127 |
call(<span class="hljs-string">"find"</span>); |
| 128 |
call(<span class="hljs-string">"findIndex"</span>); |
| 129 |
call(<span class="hljs-string">"forEach"</span>); |
| 130 |
call(<span class="hljs-string">"includes"</span>); |
| 131 |
call(<span class="hljs-string">"indexOf"</span>); |
| 132 |
call(<span class="hljs-string">"join"</span>); |
| 133 |
call(<span class="hljs-string">"lastIndexOf"</span>); |
| 134 |
call(<span class="hljs-string">"map"</span>); |
| 135 |
call(<span class="hljs-string">"reduce"</span>); |
| 136 |
call(<span class="hljs-string">"reduceRight"</span>); |
| 137 |
call(<span class="hljs-string">"slice"</span>); |
| 138 |
call(<span class="hljs-string">"some"</span>); |
| 139 |
call(<span class="hljs-string">"toLocaleString"</span>); |
| 140 |
call(<span class="hljs-string">"toString"</span>);</pre></div></div> |
| 141 |
|
| 142 |
</li> |
| 143 |
|
| 144 |
|
| 145 |
<li id="section-5"> |
| 146 |
<div class="annotation"> |
| 147 |
|
| 148 |
<div class="pilwrap "> |
| 149 |
<a class="pilcrow" href="#section-5">¶</a> |
| 150 |
</div> |
| 151 |
<p>The <code>reverse</code> and <code>sort</code> methods are usually destructive, but for |
| 152 |
<code>tuple</code> objects they return a new <code>tuple</code> object that has been |
| 153 |
appropriately reversed/sorted.</p> |
| 154 |
|
| 155 |
</div> |
| 156 |
|
| 157 |
<div class="content"><div class='highlight'><pre> call(<span class="hljs-string">"reverse"</span>, <span class="hljs-literal">true</span>); |
| 158 |
call(<span class="hljs-string">"sort"</span>, <span class="hljs-literal">true</span>);</pre></div></div> |
| 159 |
|
| 160 |
</li> |
| 161 |
|
| 162 |
|
| 163 |
<li id="section-6"> |
| 164 |
<div class="annotation"> |
| 165 |
|
| 166 |
<div class="pilwrap "> |
| 167 |
<a class="pilcrow" href="#section-6">¶</a> |
| 168 |
</div> |
| 169 |
<p>Make <code>[...someTuple]</code> work.</p> |
| 170 |
|
| 171 |
</div> |
| 172 |
|
| 173 |
<div class="content"><div class='highlight'><pre> call(useSymbol && <span class="hljs-built_in">Symbol</span>.iterator || <span class="hljs-string">"@@iterator"</span>); |
| 174 |
}</pre></div></div> |
| 175 |
|
| 176 |
</li> |
| 177 |
|
| 178 |
</ul> |
| 179 |
</div> |
| 180 |
</body> |
| 181 |
</html> |