yui-array.js.html revision eb9b6f817df5c660be9f00fc5d27fe33479fdc98
<html>
<head>
</head>
<body id="yahoo-com">
<div id="doc3" class="yui-t2">
<div id="hd">
<h3>yui <span class="subtitle">3.00PR1</span></h3>
<p>
> yui-array.js (source view)
</p>
</div>
<div id="bd">
<div id="yui-main">
<div class="yui-b">
<form name="yui-classopts-form">
<span id="classopts"><input type="checkbox" name="showprivate" id="showprivate" /> Show Private</span>
<span id="classopts"><input type="checkbox" name="showprotected" id="showprotected" /> Show Protected</span>
<span id="classopts"><input type="checkbox" name="showdeprecated" id="showdeprecated" /> Show Deprecated</span>
</form>
<div id="srcout">
<style>
#doc3 #classopts { display:none; }
</style>
<div class="highlight" ><pre><span class="c">/**</span>
<span class="c"> * YUI core</span>
<span class="c"> * @module yui</span>
<span class="c"> */</span>
<span class="nx">YUI</span><span class="o">.</span><span class="nx">add</span><span class="o">(</span><span class="s2">"array"</span><span class="o">,</span> <span class="k">function</span><span class="o">(</span><span class="nx">Y</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">L</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Lang</span><span class="o">,</span> <span class="nx">Native</span> <span class="o">=</span> <span class="nb">Array</span><span class="o">.</span><span class="nx">prototype</span><span class="o">;</span>
<span class="c">/** </span>
<span class="c"> * - Arrays are return unmodified unless the start position is specified.</span>
<span class="c"> * - "Array-like" collections (@see Array.test) are converted to arrays</span>
<span class="c"> * - For everything else, a new array is created with the input as the sole item</span>
<span class="c"> * - The start position is used if the input is or is like an array to return</span>
<span class="c"> * a subset of the collection.</span>
<span class="c"> *</span>
<span class="c"> * @TODO this will not automatically convert elements that are also collections</span>
<span class="c"> * such as forms and selects. Passing true as the third param will</span>
<span class="c"> * force a conversion.</span>
<span class="c"> *</span>
<span class="c"> * @class Array</span>
<span class="c"> * @static</span>
<span class="c"> * @constructor</span>
<span class="c"> * @param o the item to arrayify</span>
<span class="c"> * @param i {int} if an array or array-like, this is the start index</span>
<span class="c"> * @param al {boolean} if true, it forces the array-like fork. This</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">al</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">t</span> <span class="o">=</span> <span class="o">(</span><span class="nx">al</span><span class="o">)</span> <span class="o">?</span> <span class="m">2</span> <span class="o">:</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">.</span><span class="nx">test</span><span class="o">(</span><span class="nx">o</span><span class="o">);</span>
<span class="nx">switch</span> <span class="o">(</span><span class="nx">t</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">case</span> <span class="m">1</span><span class="o">:</span>
<span class="k">return</span> <span class="o">(</span><span class="nx">i</span><span class="o">)</span> <span class="o">?</span> <span class="nx">o</span><span class="o">.</span><span class="nx">slice</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">i</span><span class="o">)</span> <span class="o">:</span> <span class="nx">o</span><span class="o">;</span>
<span class="nx">case</span> <span class="m">2</span><span class="o">:</span>
<span class="k">return</span> <span class="nx">Native</span><span class="o">.</span><span class="nx">slice</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">i</span> <span class="o">||</span> <span class="m">0</span><span class="o">);</span>
<span class="nx">default</span><span class="o">:</span>
<span class="k">return</span> <span class="o">[</span><span class="nx">o</span><span class="o">];</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="k">var</span> <span class="nx">A</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">;</span>
<span class="c">/** </span>
<span class="c"> * Evaluates the input to determine if it is an array, array-like, or </span>
<span class="c"> * something else. This is used to handle the arguments collection </span>
<span class="c"> * available within functions, and HTMLElement collections</span>
<span class="c"> *</span>
<span class="c"> * @todo current implementation (intenionally) will not implicitly </span>
<span class="c"> * handle html elements that are array-like (forms, selects, etc). </span>
<span class="c"> *</span>
<span class="c"> * @return {int} a number indicating the results:</span>
<span class="c"> * 0: Not an array or an array-like collection</span>
<span class="c"> * 1: A real array. </span>
<span class="c"> * 2: array-like collection.</span>
<span class="c"> */</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">test</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">r</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">L</span><span class="o">.</span><span class="nx">isObject</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="kc">true</span><span class="o">))</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">L</span><span class="o">.</span><span class="nx">isArray</span><span class="o">(</span><span class="nx">o</span><span class="o">))</span> <span class="o">{</span>
<span class="nx">r</span> <span class="o">=</span> <span class="m">1</span><span class="o">;</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="k">try</span> <span class="o">{</span>
<span class="c">// indexed, but no tagName (element) or alert (window)</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="s2">"length"</span> <span class="k">in</span> <span class="nx">o</span> <span class="o">&&</span> <span class="o">!(</span><span class="s2">"tagName"</span> <span class="k">in</span> <span class="nx">o</span><span class="o">)</span> <span class="o">&&</span> <span class="o">!(</span><span class="s2">"alert"</span> <span class="k">in</span> <span class="nx">o</span><span class="o">))</span> <span class="o">{</span>
<span class="nx">r</span> <span class="o">=</span> <span class="m">2</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span> <span class="k">catch</span><span class="o">(</span><span class="nx">ex</span><span class="o">)</span> <span class="o">{}</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">r</span><span class="o">;</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Executes the supplied function on each item in the array.</span>
<span class="c"> * @method each</span>
<span class="c"> * @return {YUI} the YUI instance</span>
<span class="c"> */</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">each</span> <span class="o">=</span> <span class="o">(</span><span class="nx">Native</span><span class="o">.</span><span class="nx">forEach</span><span class="o">)</span> <span class="o">?</span>
<span class="k">function</span> <span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">Native</span><span class="o">.</span><span class="nx">forEach</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span> <span class="o">||</span> <span class="nx">Y</span><span class="o">);</span>
<span class="k">return</span> <span class="nx">Y</span><span class="o">;</span>
<span class="o">}</span> <span class="o">:</span>
<span class="k">function</span> <span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">a</span><span class="o">.</span><span class="nx">length</span><span class="o">,</span> <span class="nx">i</span><span class="o">;</span>
<span class="k">for</span> <span class="o">(</span><span class="nx">i</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="nx">l</span><span class="o">;</span> <span class="nx">i</span><span class="o">=</span><span class="nx">i</span><span class="o">+</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">f</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span> <span class="o">||</span> <span class="nx">Y</span><span class="o">,</span> <span class="nx">a</span><span class="o">[</span><span class="nx">i</span><span class="o">],</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">Y</span><span class="o">;</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Returns an object using the first array as keys, and</span>
<span class="c"> * the second as values. If the second array is not</span>
<span class="c"> * provided the value is set to true for each.</span>
<span class="c"> * @method hash</span>
<span class="c"> * @param k {Array} keyset</span>
<span class="c"> * @param v {Array} optional valueset</span>
<span class="c"> * @return {object} the hash</span>
<span class="c"> */</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">hash</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">k</span><span class="o">,</span> <span class="nx">v</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">o</span> <span class="o">=</span> <span class="o">{},</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">k</span><span class="o">.</span><span class="nx">length</span><span class="o">,</span> <span class="nx">vl</span> <span class="o">=</span> <span class="nx">v</span> <span class="o">&&</span> <span class="nx">v</span><span class="o">.</span><span class="nx">length</span><span class="o">,</span> <span class="nx">i</span><span class="o">;</span>
<span class="k">for</span> <span class="o">(</span><span class="nx">i</span><span class="o">=</span><span class="m">0</span><span class="o">;</span> <span class="nx">i</span><span class="o"><</span><span class="nx">l</span><span class="o">;</span> <span class="nx">i</span><span class="o">=</span><span class="nx">i</span><span class="o">+</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">o</span><span class="o">[</span><span class="nx">k</span><span class="o">[</span><span class="nx">i</span><span class="o">]]</span> <span class="o">=</span> <span class="o">(</span><span class="nx">vl</span> <span class="o">&&</span> <span class="nx">vl</span> <span class="o">></span> <span class="nx">i</span><span class="o">)</span> <span class="o">?</span> <span class="nx">v</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">:</span> <span class="kc">true</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">o</span><span class="o">;</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Returns the index of the first item in the array</span>
<span class="c"> * that contains the specified value, -1 if the</span>
<span class="c"> * value isn't found.</span>
<span class="c"> * @TODO use native method if avail</span>
<span class="c"> * @method indexOf</span>
<span class="c"> * @param a {Array} the array to search</span>
<span class="c"> * @param val the value to search for</span>
<span class="c"> * @return {int} the index of the item that contains the value or -1</span>
<span class="c"> */</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">indexOf</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">val</span><span class="o">)</span> <span class="o">{</span>
<span class="k">for</span> <span class="o">(</span><span class="k">var</span> <span class="nx">i</span><span class="o">=</span><span class="m">0</span><span class="o">;</span> <span class="nx">i</span><span class="o"><</span><span class="nx">a</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span><span class="o">=</span><span class="nx">i</span><span class="o">+</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">a</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">===</span> <span class="nx">val</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="nx">i</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">return</span> <span class="o">-</span><span class="m">1</span><span class="o">;</span>
<span class="o">};</span>
<span class="o">},</span> <span class="s2">"@VERSION@"</span><span class="o">);</span>
</pre></div>
</div>
</div>
</div>
<div class="yui-b">
<div class="nav">
<div class="module">
<h4>Modules</h4>
<ul class="content">
</ul>
</div>
<div class="module">
<h4>Classes</h4>
<ul class="content">
</ul>
</div>
<div class="module">
<h4>Files</h4>
<ul class="content">
</ul>
</div>
</div>
</div>
</div>
<div id="ft">
<hr />
Copyright © 2008 Yahoo! Inc. All rights reserved.
</div>
</div>
</body>
</html>