BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News JSINQ, a JavaScript Implementation of LINQ

JSINQ, a JavaScript Implementation of LINQ

Leia em Português

This item in japanese

Bookmarks

JSINQ stands for JavaScript INtegrated Query, a beta project implementing LINQ to Objects in JavaScript and recently released on CodePlex by Kai Jäger. JSINQ contains two modules, Enumerable and Query implementing System.Linq.Enumerable and System.Linq.Queryable respectively, allowing one to enumerate over arrays, DOM node lists or other objects.

The two modules are jsinq.Enumerable and jsinq.Query. An example of a query is:

var query = new jsinq.Query('\
    from order in $1 \
    group order by order.customerId into g \
    select {customerId: g.getKey(), items: g.sum(function(g) { return g.items; })} \
    into h \
    join customer in $0 on h.customerId equals customer.id \
    where h.items > 10 \
    orderby h.items descending \
    select {lastname: customer.lastname, items: h.items} \
');
query.setValue(0, customers);
query.setValue(1, orders);
var result = query.execute();

An example of enumerating is:

var enumerator = namesThatStartWithAnA.getEnumerator();
while (enumerator.moveNext()) {
	var name = enumerator.current();
	document.write(name + '<br  />');
}

According to Kai, JSINQ can do:

  • Write arbitrarily complex queries against JavaScript arrays, DOM node lists or your own enumerable types
  • Find elements in the HTML DOM tree using SQL-like queries
  • Dynamically create HTML elements from JSON you have received via XMLHttpRequest in a declarative manner
  • Tinker with XML and turn it into something else
  • Combine it in interesting ways with the JavaScript-/Ajax-frameworks you are already using
  • Write less code by exploiting the power of declarative programming
  • And for the ambitious: write raytracers, monadic parser combinators, etc.

Kai has put up a playground web page allowing anyone to test JSINQ. The source code can be downloaded for free from CodePlex under the MIT License. Other similar projects: JSLINQ and JLINQ.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT