Just What Is jLinq?

A while back I had a project that required a lot of sorting and selecting of information that pretty much wasn't going to change. I started out by writing a couple sorting functions and all was good.

But as any developer knows, the requirements began to change and sorting alone wasn't good enough. Steadily, the requirements went from sorting by type, to sorting by type or name, then type, name or date...

Sooner or later I had to start including query terms in each of my sorting functions! Quite literally all of my efforts had been wasted. My only thought was "this would be so much easier with LINQ"...

And why not? It was a perfect candidate. And so with some effort, jLinq was quickly born.

Now you may have noticed, this isn't the first project like this. Some people have put together some neat little query languages, like JSLINQ. What sets jLinq apart is simplicity and extensibility.

A lot of examples of other query languages I've found look something along the lines of...

var result = new JSLINQ(records)
  .Where(function(r) { return r.Age < 5 && r.Name == "Jim" })

I'm not picking on JSLINQ, but to me it seems a lot like using the code below...

var results = [];
for (var item in records) {
    if (records[item].Age < 5 && records[item].Name == "Jim") {
        results.push(records[item]);
    }
}

I'm making some unfair assumptions here, don't let my explanation drive you away from JSLINQ. I've haven't used enough to critique how it works. I'm basing this off of code samples I've seen.

Regardless, I wanted jLinq to focus on a couple things...

Simplicity

jLinq focuses more on writing your comparisons in advance, for example, consider the command 'greater()'. It is used like the following...

.greater("nameOfField", someValue)

But the command checks all of the following...

  • If the field is a number, is the number greater than the value provided?
  • If the field is a string, is the string length greater than the value provided?
  • If the field is an array, is the element length greater than the value provided?
  • If it is anything else, just try (field > value)

The specifics of how those values are checked remain hidden in the command allowing better abstraction for queries.

Secondly, because queries tend involve more than just one comparison, jLinq has methods such as or(), and() and not() that all work with extended functions automatically.

Extensibility

It's a good day when a programmer realizes they can't make a project the best it can be on his own. Extensibility was built into jLinq from the start. In fact, the entire jLinq core library is built from extension methods! This way developers can create share their jLinq functions and libraries with others!

jLinq allows you to extend upon the existing jLinq library, extend into separate namespaces on the jLinq library or even create your own jLinq library from scratch (for example, a jLinq/jQuery library is in the works).

Extending jLinq is simple. Consider we wanted to add in a pre-built function for even numbered values.

jLinq.extend({
  name:"isEven",
  type:"query",
  count:0,
  method:function(query) {
    return ((query.value % 2) == 0);
  }
});

And then accessed within a query like...

var results = jLinq.from(records)
  .isEven("age")
  .select();

And that is really about as hard as it gets!

I'll blog more about the full detail of jLinq, but as for now please ask me any questions that are on your mind!

April 27, 2009

Just What Is jLinq?

Post titled "Just What Is jLinq?"