Chaining Commands

jLINQ chains commands together by returning the same instance each time it is called. So for example, you could do the following.

//this is a verbose way to do this command, a better version is listed below
var results = jLinq.from(data)
  .startsWith("first","M")
  .endsWith("first","t")
  .greater("age",25)
  .orderBy("age")
  .select();

This command selects anyone with a first name starting with a letter "M" and ends with a "t" and whose age is greater than 23. The problem though is it isn't very effective. It doesn't let you repeat the same command or specify other modifiers like or, and or not. To fix this, jLINQ uses Command Memorizing.

Chaining with Command Memorizing

Consider if we wanted to get a list of all people that had a first name that started with "A", "B" or "C". We could write the following command.

//this is a verbose way to do this command, a better version is listed below
var results = jLinq.from(data)
  .startsWith("first","A").or()
  .startsWith("first","B").or()
  .startsWith("first","C")
  .orderBy("first")
  .select();

This command works, but is seems to be a waste of space. That is where Command Memorizing comes in. When I use the or() command the jLINQ query already knows what my last command was. It also already knows what field I queried. Let's see that same command, but using Command Memorizing.

var results = jLinq.from(data)
  .startsWith("first","A").or("B").or("C")
  .orderBy("first")
  .select();

You can also repeat the same command, but change the field each time. So for example, if I wanted to select everyone with a first name starting with an "A" and their last name starting with a "B", I could use the following.

var results = $.from(data)
  .startsWith("first","A").and("last","B")
  .orderBy("first")
  .select();

It's worth noting that when chaining commands, and is always implied unless you say otherwise. If you are repeating a command you still need to use and() in order to repeat it (unless you just call it all over again).

You can also use different commands and the field name will be implied. So for example.

var results = jLinq.from(records)
  .startsWith("first","M")
  .endsWith("q") //knows the field is still 'first'
  .select()

Step By Step Walk Throughs

Because command chaining can be a little confusing, here are some examples that might explain what is happening behind the scene.

var results = jLinq.from(records)
  .startsWith("name", "a") // command:startsWith, field:name
  .or("b") //command(repeated):startsWith, field(repeated):name
  .or("last", "c") //command(repeated):startsWith, field:last
  .endsWith("d") //command:endsWith, field(repeated):last
  .select()

You will notice that unless you specify a new command, the previous command is remembered. The same goes for the name of the last queried field. Unless you provide a new name, the previous value is used. This also works for commands with more than one parameter

var results = jLinq.from(records)
  .between("age",0,10) //command:between, field:age
  .or(20,30) //command(repeated):between, field(repeated):age
  .betweenEquals(50,60) //command:betweenEquals, field(repeated):age
  .select()

Future Plans

Future versions will allow you to use an array when searching a field to specify that the field can match any of the values, so for example, the code below does not work as of version 2.0.0, but will be implemented in later versions.

var results = jLinq.from(records)
  .startsWith("name",["a","b","c"])
  .equals("age",[10,20,30,40])
  .select()

Last edited Apr 9, 2009 at 3:33 AM by hboss, version 2

Comments

No comments yet.