ArrayList Class
Generic ArrayList class for managing lists of items and iterating operations over them. The targeted use for this class is for augmentation onto a class that is responsible for managing multiple instances of another class (e.g. NodeList for Nodes). The recommended use is to augment your class with ArrayList, then use ArrayList.addMethod to mirror the API of the constituent items on the list's API.
The default implementation creates immutable lists, but mutability can be provided via the arraylist-add submodule or by implementing mutation methods directly on the augmented class's prototype.
Constructor
ArrayList
-
items
Parameters:
-
items
Arrayarray of items this list will be responsible for
Index
Methods
_item
-
i
Protected method for optimizations that may be appropriate for API mirroring. Similar in functionality to <code>item</code>, but is used by methods added with <code>ArrayList.addMethod()</code>.
Parameters:
-
i
Integer<p><p><p><p>Index of item to fetch</p></p></p></p>
Returns:
<p><p><p><p>The item appropriate for pass through API methods</p></p></p></p>
add
-
item
-
index
Add a single item to the ArrayList. Does not prevent duplicates.
Parameters:
-
item
Mixed<p><p><p><p>Item presumably of the same type as others in the ArrayList.</p></p></p></p>
-
index
Number<p><p><p><p>(Optional.) Number representing the position at which the item should be inserted.</p></p></p></p>
Returns:
<p><p><p><p>the instance.</p></p></p></p>
addMethod
-
dest
-
name
<p>Adds a pass through method to dest (typically the prototype of a list class) that calls the named method on each item in the list with whatever parameters are passed in. Allows for API indirection via list instances.</p>
<p>Accepts a single string name or an array of string names.</p>
<pre><code>list.each( function ( item ) { item.methodName( 1, 2, 3 ); } ); // becomes list.methodName( 1, 2, 3 );</code></pre>
<p>Additionally, the pass through methods use the item retrieved by the <code>_item</code> method in case there is any special behavior that is appropriate for API mirroring.</p>
<p>If the iterated method returns a value, the return value from the added method will be an array of values with each value being at the corresponding index for that item. If the iterated method does not return a value, the added method will be chainable.
Parameters:
-
dest
ObjectObject or prototype to receive the iterator method
-
name
String | StringName of method of methods to create
each
-
fn
-
context
<p>Execute a function on each item of the list, optionally providing a custom execution context. Default context is the item.</p>
<p>The callback signature is <code>callback( item, index )</code>.</p>
Parameters:
-
fn
Function<p><p><p><p>the function to execute</p></p></p></p>
-
context
Mixed<p><p><p><p>optional override 'this' in the function</p></p></p></p>
Returns:
<p><p><p><p>this instance</p></p></p></p>
filter
-
validator
<p>Create a new ArrayList (or augmenting class instance) from a subset of items as determined by the boolean function passed as the argument. The original ArrayList is unchanged.</p>
<p>The validator signature is <code>validator( item )</code>.</p>
Parameters:
-
validator
Function<p><p><p><p>Boolean function to determine in or out.</p></p></p></p>
Returns:
<p><p><p><p>New instance based on who passed the validator.</p></p></p></p>
indexOf
-
needle
Finds the first index of the needle in the managed array of items.
Parameters:
-
needle
Mixed<p><p><p><p>The item to search for</p></p></p></p>
Returns:
<p><p><p><p>Array index if found. Otherwise -1</p></p></p></p>
isEmpty
()
Boolean
Is this instance managing any items?
Returns:
<p><p><p><p>true if 1 or more items are being managed</p></p></p></p>
item
-
i
Get an item by index from the list. Override this method if managing a list of objects that have a different public representation (e.g. Node instances vs DOM nodes). The iteration methods that accept a user function will use this method for access list items for operation.
Parameters:
-
i
Integer<p><p><p><p>index to fetch</p></p></p></p>
Returns:
<p><p><p><p>the item at the requested index</p></p></p></p>
itemsAreEqual
-
a
-
b
Default comparator for items stored in this list. Used by remove().
Parameters:
-
a
Mixed<p><p><p><p>item to test equivalence with.</p></p></p></p>
-
b
Mixed<p><p><p><p>other item to test equivalance.</p></p></p></p>
Returns:
<p><p><p><p>true if items are deemed equivalent.</p></p></p></p>
remove
-
needle
-
all
-
comparator
Removes first or all occurrences of an item to the ArrayList. If a comparator is not provided, uses itemsAreEqual method to determine matches.
Parameters:
-
needle
Mixed<p><p><p><p>Item to find and remove from the list.</p></p></p></p>
-
all
Boolean<p><p><p><p>If true, remove all occurrences.</p></p></p></p>
-
comparator
Function<p><p><p><p>optional a/b function to test equivalence.</p></p></p></p>
Returns:
<p><p><p><p>the instance.</p></p></p></p>
size
()
Integer
How many items are in this list?
Returns:
<p><p><p><p>Number of items in the list</p></p></p></p>
some
-
fn
-
context
<p>Execute a function on each item of the list, optionally providing a custom execution context. Default context is the item.</p>
<p>The callback signature is <code>callback( item, index )</code>.</p>
<p>Unlike <code>each</code>, if the callback returns true, the iteration will stop.</p>
Parameters:
-
fn
Function<p><p><p><p>the function to execute</p></p></p></p>
-
context
Mixed<p><p><p><p>optional override 'this' in the function</p></p></p></p>
Returns:
<p><p><p><p>True if the function returned true on an item</p></p></p></p>
toJSON
()
Array
Provides an array-like representation for JSON.stringify.
Returns:
<p><p><p><p>an array representation of the ArrayList</p></p></p></p>