From LINQ to JSINQ

The heart of JSINQ is jsinq.Enumerable which is a faithful implementation of System.Linq.Enumerable in JavaScript. It differs from its .NET counterpart in that it uses JavaScript naming conventions, lacks types and has a few subtle differences in how it handles overloads. In general though, it is API-compatible with the .NET implementation. What that means is that you can use the .NET documentation for reference: http://msdn.microsoft.com/en-us/library/system.linq.enumerable_members.aspx

jsinq.Enumerable

jsinq.Enumerable unlike its .NET counterpart is an actual constructor that you can use to create enumerables from arrays or DOM node lists. Let's look at an example:

var nodes = document.getElementsByTagName('*');
var enumerable = new jsinq.Enumerable(nodes);

The variable "enumerable" now holds a new enumerable that can be used to enumerate over the elements of the node list. To do so, simply write:

var enumerator = enumerable.getEnumerator();
while (enumerator.moveNext()) {
    var current = enumerator.next();
    // do something with "current" here
}

You can also write your own enumerables; all you have to do is implement the method getEnumerator and then return an object that supports the three methods current, moveNext and reset. The signatures and semantics of these methods are described here: http://msdn.microsoft.com/en-us/library/system.collections.ienumerator_members.aspx. Also check out the implementation of Enumerable.range in the file "jsinq.js". If you set the prototype of your enumerable to jsinq.Enumerable.prototype, you get all of its querying-goodness for free.

Methods

jsinq.Enumerable provides the following methods:

Method Static Description
aggregate no Accumulates the elements of an Enumerable using an accumulator function.
all no Returns true if all elements in the Enumerable satisfy the specified condition.
any no Returns true if the Enumerable contains any elements or if at least one of the elements in the Enumerable satisfy a specified condition.
average no Returns the average of the elements in the Enumerable, optionally using the specified selector function.
concat no Creates a new Enumerable that is the result of the concatenation of two Enumerables.
contains no Returns true if the Enumerable contains the specified element. Optionally uses the specified comparer.
count no Returns the number of elements in the Enumerable. If a predicate function is specified, only those elements will be counted that satisfy the given condition.
defaultIfEmpty no Returns the Enumerable or a new Enumerable containing only the specified default value, should the Enumerable be empty.
distinct no Returns a new Enumerable that contains only distinct elements. Optionally uses a specified comparer.
elementAt no Returns the element at the specified offset or throws an exception if the index is out of bounds.
elementAtOrDefault no Same as elementAt, except it returns the specified defaultValue if the given index is out of bounds. This differs from the LINQ implementation in that the default value needs to be specified explicitly.
empty yes Returns an empty Enumerable
except no Returns a new Enumerable containing all elements from this Enumerable except those contained in a second Enumerable (i.e. the set difference between two enumerables). Optionally uses the specified comparer.
first no Returns the first element in the Enumerable or throws an exception if the Enumerable is empty. If a predicate function is specified, the method returns the first element that satisfies the given condition. If the Enumerable is empty or no element satisfies the specified condition, an exception is thrown.
firstOrDefault no Same as first, except it returns a specified defaultValue if the Enumerable is empty or no element satisfies the specified condition. This method is different from its LINQ counterpart in that it requires the default value to be specified explicitly.
groupBy no Groups the elements in the Enumerable. Note that this method resolves overloads based on function arity. Result selectors are expected to have two or more formal parameters while element- and key-selectors are expected to have just one.
groupJoin no Correlates the elements in two Enumerables based on their keys and groups the results.
intersect no Returns a new Enumerable that is the result of the intersection of two Enumerables. Optionally uses the specified comparer.
join no Correlates the elements in two Enumerables based on their keys.
last no Returns the last element in the Enumerable or throws an exception, if the Enumerable is empty. If a predicate function is specified, the method returns the last element that satisfies the given condition. If no element satisfies the specified condition, an exception is thrown.
lastOrDefault no Same as last, except it returns a specified defaultValue if the Enumerable is empty or not element satisfies the specified condition. This method is different from its LINQ counterpart in that it requires the default value to be specified explicitly.
max no Returns the maximum value in the Enumerable.
min no Returns the minimum value in the Enumerable.
orderBy no Sorts the elements in the Enumerable in ascending order.
orderByDescending no Sorts the elements in the Enumerable in descending order.
repeat yes Returns an Enumerable that contains the specified element "count" times
reverse no Reverses the order of the elements in the Enumerable.
select no Projects each of the elements in the Enumerable into a new form. This differes from its LINQ counterpart in that the selector function always receives the index of the current element as its second parameter.
selectMany no Projects each of the elements in the Enumerable into a new Enumerable and then flattens the result into a single Enumerable again. This differes from its LINQ counterpart in that the collection selector function always receives the index of the current element as its second parameter.
sequenceEqual no Returns true if the Enumerable is identical to another Enumerable by comparing the elements using an optional comparer.
single no Returns the only element in the Enumerable or throws an exception either if the Enumerable is empty or if there is more than one element in the Enumerable. Optionally tests the returned element against a predicate function and throws an exception if the element does not satisfy its condition.
singleOrDefault no Same as single, except that a specified default value is returned if the Enumerable is empty, contains more than one element or if the element does not satisfy the specified condition. This method differs from its LINQ counterpart in that it requires the default value to be specified explicitly.
skip no Returns a new Enumerable that is the result of skipping the first "count" elements of the Enumerable.
skipWhile no Returns a new Enumerable that is the result of skipping all elements from the beginning of the Enumerable that satisfy a specified condition.
sum no Returns the sum of the elements in the Enumerable, optionally using a specified selector.
take no Returns a new Enumerable that is the result of extracting the first "count" elements from the Enumerable.
takeWhile no Returns a new Enumerable that is the result of extracting all elements from the beginning of the Enumerable that satisfy the specified condition.
toArray no Returns an array containg all the elements in Enumerable.
toList no Returns a jsinq.List containg all the elements in Enumerable.
toDictionary no Returns a jsinq.Dictionary containg all the elements in Enumerable. A key selector is used to determine the key for each element.
toLookup no Returns a jsinq.Lookup containg all the elements in Enumerable. A key selector ise used to determine the key for each element.
union no Returns a new Enumerable that is the union of two Enumerables. Optionally uses the specified comparer.
where no Returns a new Enumerable that contains only the elements of the Enumerable that satisfy the specified condition.
zip no Returns a new Enumerable that is the result of merging two Enumerables using a specified result selector.


Please check out the official .NET reference for additional information on how to use these methods: http://msdn.microsoft.com/en-us/library/system.linq.enumerable_members.aspx
Also look at the comments in the source code and of course the examples.

Last edited Mar 27, 2010 at 2:15 PM by kaijaeger, version 2

Comments

No comments yet.