Hugoware

The product of a web developer with a little too much caffeine

Just What Is jLinq?

with 7 comments

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" })
&#91;/sourcecode&#93;

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

&#91;sourcecode language='jscript'&#93;
var results = &#91;&#93;;
for (var item in records) {
    if (records&#91;item&#93;.Age < 5 && records&#91;item&#93;.Name == "Jim") {
        results.push(records&#91;item&#93;);
    }
}
&#91;/sourcecode&#93;

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...

<h2>Simplicity</h2>
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();

Now here is the scary part…… 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!

Advertisements

Written by hugoware

April 28, 2009 at 3:35 am

7 Responses

Subscribe to comments with RSS.

  1. I love your script! It’s awesome!!
    but i’m missing a function: the .take() function would be nice 🙂

    regards

    Dave

    April 29, 2009 at 3:15 pm

  2. That is a great idea – I’ll add that to the library.

    There is a function called ‘skipTake’ that lets you provide a 0 for the ‘skip’ value, which might tide you over for a bit.

    hboss

    April 29, 2009 at 5:05 pm

  3. cool, nice to hear that.
    i got another question
    i’m trying to order my array, but the values are doubles (3.4, 2.43,..), and that isn’t working correctly..
    is there a support for it or is the mistake at my code?

    Dave

    April 30, 2009 at 11:00 am

  4. What does your query look like?

    hboss

    April 30, 2009 at 11:31 am

  5. My array looks like this:
    [{“__type”:”UserStat”,”Direction”:”BadgeIn”,”Person”:”x”,”Today”:5.42,”Week”:30.93,”WeekLag”:0
    .8,”TotalLag”:37.47},….]

    and my query:
    var FilteredUserStats = jLinq.from(UserStats).orderBy(“Today”).select();

    Dave

    April 30, 2009 at 1:31 pm

  6. Hmm… more or less, last night I discovered orderBy wasn’t behaving as expected. I had to make some changes to the way it worked.

    Unfortunately, I ran out of time to finish, but I should be done by tonight.

    If you want to get it working now, try using it like .orderBy({“Today”:”a”}) – **BUT** that will only work until the next time you download the library.

    Future versions will simply be done like… .orderBy(“Today”, … other field names…)

    Orignally I wanted to use a format like…
    .orderBy({
    “firstField”: “a”, // ascending
    “secondField”: “d” //descending
    //etc…
    })

    but as it turns out, IE doesn’t always enumerate each of the properties in the same order that they were entered. The new format will look like…

    .orderBy(
    “firstField”, //ascending is implied
    “-secondField”, // ‘-‘ marks it as descending
    );

    like I said though, this is still in progress so as of tonight we should be all fixed up.

    Thanks!!

    hboss

    April 30, 2009 at 1:58 pm

  7. Awesome!!
    thanks you very much!

    Btw, i included your library in a blog post of me, as it is awesome to work with!

    http://www.xcite.ch/?p=63

    regards
    Dave

    Dave

    April 30, 2009 at 2:04 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: