Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

jLinq 2.2.0 Released!

5.00/5 (7 votes)
13 Aug 2009CC (ASA 2.5)4 min read 40.2K  
jLinq is a Javascript query language that makes it easy to work with large arrays of information. Today, jLinq 2.2.0 is released which offers new commands, bug fixes and more!

Ever hear of jLinq. If not, it’s no big deal. It's a personal project of mine to make a LINQ style query language for JavaScript. With all these “Web 2.0″ applications floating around, the ability to sort, query and manage your records on the client side may become a necessity. Use jLinq with the JSON data you already have and you’ve got quite a combination.

If you haven’t tried it out, there is an online version available you can use to see it in action.

Below are some of the new features in jLinq 2.2.0.

Smarter Comparisons

Let’s say you ran the command below…

JavaScript
jLinq.from(users)
    .less("firstname", 5)
    .select();

Seems simple enough, first names less than 5 characters long?

Nope…

The previous version of jLinq expected certain data types depending on the command. For example, if you used less, lessEquals, greater, greaterEquals, between or betweenEquals, then the expected parameter was a number as was the field you were querying against.

In the new version, you’ll find code like this when checking values.

JavaScript
//get the parameter value
value = query.helper.when(value, {
    number:function() { return value; },
    other:function() { return value.length; }
});

//determine how to check this against the value
return query.when({
    string:function() {
        return (query.value.length < value);
    },
    array:function() {
        return (query.value.length < value);
    },
    other:function() {
        return (query.value < value);
    }
});

Instead of making any assumptions, jLinq now checks the types and returns the values required to make the query work. For example, if you use ‘less’ with a string value, the length property is automatically used instead of the string value itself.

jLinq also is more intelligent with different command types. For example, the .contains() used to work with string values only. Now it works with arrays or converts a value to a string then checks the result.

11 New Commands

  • attach(alias, function): Executes and attaches the return value of your function onto the record using the alias name you provide.
  • sum(field): Returns an object with information about the sum of the field name used.
    returns
    {<br />
    	count:the number of records counted,<br />
    	result:the sum of all records selected,<br />
    	records:the records used to find the sum<br />
    	}
  • average(field): Returns an object with information about the average of the field name used.
    returns
    {<br />
    	total:the sum before the average was calculated,<br />
    	count:the number of records used,<br />
    	result:the average of all records selected,<br />
    	records:the records used to find the average <br />
    	}
  • max(field): The maximum value found for the field name provided.
  • min(field): The minimum value found for the field name provided.
  • except(collection): Compares a second set of records and only returns records that are not found in the comparison list. Returns a new jLinq object. (example)
  • intersect(collection): Compares a second set of records and only returns records that are found in both lists. Returns a new jLinq object. (example)
  • union(collection): Compares a second set of records and only returns records that are unique in both lists. Returns a new jLinq object. (example)
  • skipWhile(delegate): Skips selecting records until the first ‘false’ result is returned from your function. After that point, all records are selected. Returns an array of results. (example)
  • takeWhile(delegate): Selects records until the first ‘false’ result is returned from your function. After that point, all records are ignored. Returns an array of results. (example)
  • selectMany(collection, compareFunction, selectionFunction): Compares each value of the current query against the provided collection array (example). For each successful comparison, a new record is added to your results. If you use your own selection method, then you need to write a function similar to function(queryRecord, compareRecord) {. The return value will be added to the list. If no selection method is used, then the following object is returned.
    returns
    {<br />
    	source:the record in the jLinq query that is compared,<br />
    	compare:the record in the collection array that is compared<br />
    	}

Bug Fixes

There was an Opera bug when you tried to sort records. I’m not sure that I understand why, but basically, there really is a difference between for loops in Opera.

jLinq Testing

If you’re interested in creating your own jLinq extension methods, you can now download a series of tests that you can use to determine that your command doesn’t break anything else in jLinq.

Wrapping Up

Whew! You’re still with me? You must be dedicated!

Right now, I realize that there still may be some confusion on how to use jLinq. I’m currently working on some screencasts that I can share to help people get started.

If you have some requests on what you would like to see in an upcoming screencast, please send me your ideas and I’ll add them to my list.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License