Class: Enumerable

arrgh. Enumerable

new arrgh.Enumerable(enumerable)

Represents the base class for any collection.
Name Type Default Description
enumerable Array | String | arrgh.Enumerable | function | params [] optional An array, string or enumerable to add to the new collection or a parameterless function that returns an arrgh.Iterator.

Methods

staticarrgh.Enumerable.empty(){arrgh.Enumerable}

Returns an empty singleton instance of enumerable.
Returns:
Type Description
arrgh.Enumerable - An empty singleton collection.

staticarrgh.Enumerable.range(start, count){arrgh.Enumerable}

Generates a collection of integral numbers within a specified range.
When no count is supplied the range will stop at 9007199254740991 (a.k.a. Number.MAX_SAFE_INTEGER).
Depending on your start value your browser will probably flip though, so just provide a count.
Name Type Description
start Number The value of the first integer in the collection.
count Number optional The number of sequential integers to generate.
Throws:
Throws when count is lower than 0 or when the range exceeds 9007199254740991 (a.k.a. Number.MAX_SAFE_INTEGER).
Returns:
Type Description
arrgh.Enumerable - A collection that contains a range of sequential integral numbers.

staticarrgh.Enumerable.repeat(element, count){arrgh.Enumerable}

Generates a collection that contains one repeated value.
Name Type Description
element * The element to repeat.
count Number The number of times to repeat the element.
Throws:
Throws when count is lower than 0.
Returns:
Type Description
arrgh.Enumerable - A collection that contains a one value count times.

aggregate(seed, accumulator, resultSelector){*}

Determines whether all elements of the collection satisfy a condition.
When a result selector is passed to the function a seed should also be specified.
Name Type Description
seed * optional The initial accumulator value (mandatory if a result selector is specified).
accumulator accumulator An accumulator function to be invoked on each element.
resultSelector selector optional A function to transform the final accumulator value into the result value.
Returns:
Type Description
* - The final accumulator value.

all(predicate){Boolean}

Determines whether all elements of the collection satisfy a condition.
Name Type Description
predicate predicate A function to test each element for a condition.
Returns:
Type Description
Boolean - True if the list is empty or if all elements in the collection satisfy a condition, else false.

any(predicate){Boolean}

Determines whether the collection contains any elements or if any elements satisfy a condition.
Name Type Description
predicate predicate optional A function to test each element for a condition.
See:
Returns:
Type Description
Boolean - True if the collection contains any elements or if any elements satisfy a condition, else false.

asEnumerable(){arrgh.Enumerable}

Returns the input typed as Enumerable.
Returns:
Type Description
arrgh.Enumerable - The input collection as Enumerable.

average(selector){Number}

Computes the average of a collection of values.
Values are converted to numerics, but if this fails unexpected averages may occur.
Name Type Description
selector selector optional A function that projects an element into a new form.
Throws:
Throws an error if the collection contains no elements.
Returns:
Type Description
Number - The average of all values in the collection, or NaN.

concat(other){arrgh.Enumerable}

Concatenates two collections.
Name Type Description
other other The collection to concatenate to the current collection.
See:
Returns:
Type Description
arrgh.Enumerable - A collection that contains all the elements of both the current and the other collection.

contains(elem, eqComparer){Boolean}

Determines whether a collection contains a specified element, optionally uses a custom equality comparer.
Name Type Default Description
elem * The element to locate in the collection.
eqComparer equals | equalityComparer (===) optional A function or object that tests if two elements are equal.
Returns:
Type Description
Boolean - Returns whether the specified element is contained in the collection.

count(predicate){Number}

Specifies how many elements the collection has, or how many satisfy a certain condition.
Name Type Description
predicate predicate optional A function to test each element for a condition.
Returns:
Type Description
Number - A number that specifies how many elements the collection has, or how many satisfy a certain condition.

defaultIfEmpty(defaultValue){arrgh.Enumerable}

Returns the elements of the specified collection or a collection containing only the default value if the collection is empty.
Name Type Description
defaultValue * The default value to be returned when the collection is empty.
Returns:
Type Description
arrgh.Enumerable - A new collection containing the elements of the specified collection or a new collection containing only the default value if the collection is empty.

distinct(eqComparer){arrgh.Enumerable}

Returns distinct elements from a collection by using the default or a custom equality comparer to compare values.
Name Type Default Description
eqComparer equals | equalityComparer (===) optional A function or object that tests if two elements are equal.
Returns:
Type Description
arrgh.Enumerable - A new collection with unique elements.

elementAt(index){*}

Returns the element at a specified index.
Name Type Description
index Number The index of the element to find.
Throws:
Throws an error if the specified index is outside the bounds of the collection.
Returns:
Type Description
* - The element at the specified index.

elementAtOrDefault(index, defaultValue){*}

Returns the element at a specified index or a default value.
Name Type Description
index Number The index of the element to find.
defaultValue * optional The value that is returned when the specified index is not found.
Returns:
Type Description
* - The element at the specified index or a default value.

except(other, eqComparer){arrgh.Enumerable}

Produces the set difference of two collections.
Name Type Default Description
other arrgh.Enumerable A collection whose elements that also occur in the first sequence will cause those elements to be removed from the returned collection.
eqComparer equals | equalityComparer (===) optional A function or object that tests if two elements are equal.
Returns:
Type Description
arrgh.Enumerable - A collection that contains the set difference of the elements of two collections.

filter(predicate){arrgh.Enumerable}

Filters a collection of values based on a predicate. Each element's index is used in the logic of the predicate function.
Name Type Description
predicate indexPredicate A function to test each element for a condition.
See:
Returns:
Type Description
arrgh.Enumerable - A collection that contains all elements that satisfy the condition.

first(predicate){*}

Returns the first element in a collection, or the first element that satisfies a condition.
Name Type Description
predicate predicate optional A function to test each element for a condition.
Throws:
Throws an error if the collection is empty or when no element matches the condition.
Returns:
Type Description
* - Returns the first element of the collection, or the first element that satisfies a condition.

firstOrDefault(predicate, defaultValue){*}

Returns the first element in a collection, or the first element that satisfies a condition.
If the element is not found returns a default value.
Name Type Description
predicate predicate optional A function to test each element for a condition.
defaultValue * optional The value that is returned when the collection is empty or no element matches the condition.
Returns:
Type Description
* - Returns the first element of the collection, or the first element that satisfies a condition, or a specified default value.

forEach(callback)

Performs the specified action on each element of the collection.
Name Type Description
callback forEachCallback The callback that is applied to each element in the enumerable.

getIterator(){arrgh.Iterator}

Returns an iterator that iterates through the collection.
Returns:
Type Description
arrgh.Iterator - Returns an iterator that iterates through the collection.

groupBy(keySelector, elementSelector, resultSelector, eqComparer)

Groups the elements of a collection according to a specified key selector.
Name Type Default Description
keySelector keySelector A function that returns the key value from an element.
elementSelector selector optional A function to project an element into a new form.
resultSelector groupByResultSelector optional A function to create a result value from each group.
eqComparer equalityComparer (===) optional An object that tests if two keys are equal.

groupJoin(inner, outerKeySelector, innerKeySelector, resultSelector, eqComparer){arrgh.Enumerable}

Correlates the elements of two collections based on equality of keys and groups the results.
Name Type Default Description
inner arrgh.Enumerable The collection to join with.
outerKeySelector keySelector A function that returns the key value from an element of the outer collection.
innerKeySelector keySelector A function that returns the key value from an element of the inner collection.
resultSelector groupJoinResultSelector A function to create a result value from each group.
eqComparer equalityComparer (===) optional An object that tests if two keys are equal.
Returns:
Type Description
arrgh.Enumerable - A collection that contains elements that are obtained by performing a grouped join on two collections.

indexOf(searchElem, fromIndex){Number}

Finds the first index at which a given element can be found in the collection, or -1 if it is not present.
Name Type Default Description
searchElem * The element to locate in the collection.
fromIndex Number 0 optional The index to start the search at.
Returns:
Type Description
Number - The first index of the element in the array or -1 if not found.

intersect(other, eqComparer){arrgh.Enumerable}

Produces the set intersection of two collections.
Name Type Default Description
other arrgh.Enumerable A collection whose elements that also occur in the first sequence will cause those elements to be included in the returned collection.
eqComparer equals | equalityComparer (===) optional A function or object that tests if two elements are equal.
Returns:
Type Description
arrgh.Enumerable - A collection that contains the set intersection of the elements of two collections.

join(inner, outerKeySelector, innerKeySelector, resultSelector, eqComparer)

Correlates the elements of two collections based on equality of keys.
Name Type Default Description
inner arrgh.Enumerable The collection to join with.
outerKeySelector keySelector A function that returns the key value from an element of the outer collection.
innerKeySelector keySelector A function that returns the key value from an element of the inner collection.
resultSelector joinResultSelector A function to create a result from two matched elements.
eqComparer equalityComparer (===) optional An object that tests if two keys are equal.

last(predicate){*}

Returns the last element in a collection, or the last element that satisfies a condition.
Name Type Description
predicate predicate optional A function to test each element for a condition.
Throws:
Throws an error when the collection is empty or when no element matches the condition.
Returns:
Type Description
* - Returns the last element of the collection, or the last element that satisfies a condition.

lastIndexOf(searchElem, fromIndex){Number}

Finds the last index at which a given element can be found in the collection, or -1 if it is not present.
Name Type Default Description
searchElem * The element to locate in the collection.
fromIndex Number 0 optional The index to start the search at.
Returns:
Type Description
Number - The last index of the element in the array or -1 if not found.

lastOrDefault(predicate, defaultValue){*}

Returns the last element in a collection, or the last element that satisfies a condition.
If the element is not found returns a default value.
Name Type Description
predicate predicate optional A function to test each element for a condition.
defaultValue * optional The value that is returned when the collection is empty or no element matches the condition.
Returns:
Type Description
* - Returns the last element of the collection, or the last element that satisfies a condition, or a specified default value.

map(selector){arrgh.Enumerable}

Projects each element of a collection into a new form.
Name Type Description
selector indexSelector A function that projects an element into a new form.
See:
Returns:
Type Description
arrgh.Enumerable - A collection whose elements are the result of invoking the transform function on each element of source.

max(selector){*}

Returns the maximum value in a collection.
Values are converted to numerics. If this fails and the value is NaN then NaN is treated as smaller than anything.
Name Type Description
selector selector optional A function that projects an element into a new form.
Throws:
Throws an error when the collection is empty.
Returns:
Type Description
* - Returns the maximum value in the collection or NaN.

min(selector){*}

Returns the minimum value in a collection.
Values are converted to numerics. If this fails the function returns NaN.
Name Type Description
selector selector optional A function that projects an element into a new form.
Throws:
Throws an error when the collection is empty.
Returns:
Type Description
* - Returns the minimum value in the collection or NaN.

ofType(type){arrgh.Enumerable}

Filters elements based on a specified type (constructor).
Object and null do not evaluate to the same type and neither do undefined and null.
Name Type Description
type * | undefined | null The constructor of a type or undefined or null.
Returns:
Type Description
arrgh.Enumerable - Returns a collection containing only values that are of the specified type.

orderBy(keySelector, compare){arrgh.OrderedEnumerable}

Sorts the elements of a sequence in ascending order according to a key.
Name Type Description
keySelector keySelector A function to extract a key from an element.
compare compare optional A function that tests if an object is smaller than, greater than or equal to another object.
Returns:
Type Description
arrgh.OrderedEnumerable - Returns an ordered enumerable.

orderByDescending(keySelector, compare){arrgh.OrderedEnumerable}

Sorts the elements of a sequence in descending order according to a key.
Name Type Description
keySelector keySelector A function to extract a key from an element.
compare compare optional A function that tests if an object is smaller than, greater than or equal to another object.
Returns:
Type Description
arrgh.OrderedEnumerable - Returns an ordered enumerable.

reverse(){arrgh.Enumerable}

Reverses the order of the elements in a collection.
Returns:
Type Description
arrgh.Enumerable - A collection that contains the original collection in reversed order.

select(selector){arrgh.Enumerable}

Projects each element of a collection into a new form.
Name Type Description
selector indexSelector A function that projects an element into a new form.
See:
Returns:
Type Description
arrgh.Enumerable - A collection whose elements are the result of invoking the transform function on each element of source.

selectMany(collectionSelector, resultSelector){arrgh.Enumerable}

Projects each element of a collection to an Enumerable and flattens the resulting collections into one collection.
Name Type Description
collectionSelector collectionSelector A function that projects an element into a new form.
resultSelector selectManyResultSelector optional A function that creates a result value from each element in the intermediate collection.
Returns:
Type Description
arrgh.Enumerable - A collection whose elements are the result of invoking the one-to-many transform function on each element of the input collection.

sequenceEquals(other, eqComparer){Boolean}

Determines whether two collections are equal by comparing the elements, optionally using a custom equality comparer for their type.
Name Type Default Description
other arrgh.Enumerable Another collection to compare with.
eqComparer equalityComparer (===) optional An object that tests if two objects are equal.
Returns:
Type Description
Boolean - True if all elements in both collections match, otherwise false.

single(predicate){*}

Returns the only element in a collection, or the only element that satisfies a condition.
Name Type Description
predicate predicate optional A function to test each element for a condition.
Throws:
Throws an error when the collection is empty or when no element matches the condition or when the collection (or predicate) returns more than a single element.
Returns:
Type Description
* - Returns the only element of the collection, or the only element that satisfies a condition.

singleOrDefault(predicate, defaultValue){*}

Returns the only element in a collection, or the only element that satisfies a condition.
If the element is not found returns a default value.
Name Type Description
predicate predicate optional A function to test each element for a condition.
defaultValue * optional The value that is returned when the collection is empty or no element matches the condition.
Throws:
Throws an error when the collection (or predicate) returns more than a single element.
Returns:
Type Description
* - Returns the only element of the collection, or the only element that satisfies a condition, or a specified default value.

skip(count){arrgh.Enumerable}

Bypasses a specified number of elements in a collection and then returns the remaining elements.
Name Type Description
count Number The number of elements to skip.
Returns:
Type Description
arrgh.Enumerable - A collection that contains the elements that occur after the specified index.

skipWhile(predicate){arrgh.Enumerable}

Bypasses elements in a collection as long as a specified condition is true and then returns the remaining elements.
Name Type Description
predicate indexPredicate A function to test whether to skip the element.
Returns:
Type Description
arrgh.Enumerable - A collection that contains the elements starting at the first element in the linear series that does not pass the test specified by predicate.

some(predicate){Boolean}

Determines whether the collection contains any elements or if any elements satisfy a condition.
Name Type Description
predicate predicate optional A function to test each element for a condition.
See:
Returns:
Type Description
Boolean - True if the collection contains any elements or if any elements satisfy a condition, else false.

sum(selector){Number}

Computes the sum of a collection of values.
If values are not numerics the result may be NaN or something unexpected (e.g. "2" + 2 will results 22).
Name Type Description
selector selector optional A function that projects an element into a new form.
Throws:
Throws an error if the collection contains no elements.
Returns:
Type Description
Number - The sum of all values in the collection, or NaN.

take(count){arrgh.Enumerable}

Returns a specified number of elements from the start of a collection.
Name Type Description
count Number The number of elements to take.
Returns:
Type Description
arrgh.Enumerable - A collection that contains the elements that occur before the specified index.

takeWhile(predicate){arrgh.Enumerable}

Returns elements from a collection as long as a specified condition is true.
Name Type Description
predicate indexPredicate A function to test whether to take the element.
Returns:
Type Description
arrgh.Enumerable - A collection that contains the elements that occur before the element at which the test no longer passes.

toArray(){Array}

Converts the collection to a JavaScript array.
Returns:
Type Description
Array - Returns a JavaScript array.

toDictionary(keySelector, elementSelector, eqComparer){arrgh.Dictionary}

Converts the collection to a dictionary.
Name Type Default Description
keySelector keySelector A function that returns the key value from an element of the inner collection.
elementSelector selector optional A function that projects an element into a new form.
eqComparer equalityComparer (===) optional An object that tests if two keys are equal.
Returns:
Type Description
arrgh.Dictionary - Returns a dictionary containing the keys and elements that are selected from the input collection.

toList(){arrgh.List}

Converts the collection to a list.
Returns:
Type Description
arrgh.List - Returns a List containing all the elements from the input collection.

toLookup(keySelector, elementSelector, eqComparer){arrgh.Lookup}

Converts the collection to a collection of keys each mapped to one or more values.
Name Type Default Description
keySelector keySelector A function that returns the key value from an element of the inner collection.
elementSelector selector optional A function that projects an element into a new form.
eqComparer equalityComparer (===) optional An object that tests if two keys are equal.
Returns:
Type Description
arrgh.Lookup - Returns a collection of keys mapped to one or more values.

union(other, eqComparer){arrgh.Enumerable}

Produces the set union of two collections by using the default or a custom equality comparer to compare values.
Name Type Default Description
other arrgh.Enumerable The other collection to union with.
eqComparer equalityComparer (===) optional An object that tests if two elements are equal.
Returns:
Type Description
arrgh.Enumerable - A collection that contains distinct element from the two input collections.

unionAll(other){arrgh.Enumerable}

Concatenates two collections.
Name Type Description
other other The collection to concatenate to the current collection.
See:
Returns:
Type Description
arrgh.Enumerable - A collection that contains all the elements of both the current and the other collection.

where(predicate){arrgh.Enumerable}

Filters a collection of values based on a predicate. Each element's index is used in the logic of the predicate function.
Name Type Description
predicate indexPredicate A function to test each element for a condition.
See:
Returns:
Type Description
arrgh.Enumerable - A collection that contains all elements that satisfy the condition.

zip(other, resultSelector){arrgh.Enumerable}

Applies a specified function to the corresponding elements of two collections, producing a collection of the results.
Name Type Description
other arrgh.Enumerable The collection to merge with.
resultSelector zipResultSelector A function that creates a result value from two elements.
Returns:
Type Description
arrgh.Enumerable - A collection that contains merged elements of two input collections.