Hugoware

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

Search Results

jLinq (LINQ for JSON) Screencast #2 – Creating Your Own Extension Methods

with one comment

jLinq is a Javascript library that allows you to do LINQ style queries with JSON data.

Did you know that jLinq let’s you extend the base library with your own methods? Did you realize that any method you create plugs right in and works with the built in jLinq features?

This screencast goes over some of the basics to creating your first extension method for jLinq.

Creating A Query Method

A Query method is what determines what records stay and which records are removed from a jLinq query. Here is a sample of what a extension method looks like.

jLinq.extend({
    name:"evenValues", //the name of this method
    type:"query", //the kind of method this is
    count:0, //how many parameters we expect
    method:function(query) {	

        //determine how to evaluate the value being compared
        var compare = query.when({
            "string":function() {
                return query.value.length;
            },
            "array":function() {
                return query.value.length;
            },
            "other":function() {
                return query.helper.getNumericValue(query.value);
            }
        });		
        
        //the actual comparison
        return compare % 2 == 0;		
    }
});

You’ll notice we provide the name of this new method, the kind of method (in this instance, a query method), the expected number of parameters (not counting the field name) and then the actual method we use to determine if we keep the record.

If you don’t understand how this works exactly then you might want to watch the screencast at the end of this post to see how this works step by step.

Operator Naming

This screencast also explains operator naming. When you extend a query method, several additional operator prefixed name methods are added to jLinq as well. Each of these perform the correct switches when they are used. For example…

//this method is one way to do it
jLinq.from(data.users)
    .less("age", 30)
    .or()
    .evenValues()
    .select();

//this method was generated automatically - saves us a step!
jLinq.from(data.users)
    .less("age", 30)
    .orEvenValues()
    .select();

Of course, if you name your method something like orHasSomeValue() you’re going to end up with some strangely named operator methods (for example orOrHasSomeValue()).

It’s Simple–ish

Extension methods are a little confusing at first but after you create your first one you should be on your way. This screencast will get you started in that direction.

Watch Screencast #2 – Extending A Method In jLinq

.

Written by hugoware

July 8, 2009 at 6:26 am

jLinq Screencast #1 – Getting Started With jLinq

with one comment

Want to get started using jLinq, but want to see it in action a little before you do? You could go try it out online or you could watch the brand new jLinq screencast!

Yep, with a lot of effort, I’ve done my first screencast and it turned out not half bad (my wife survived 45 seconds before she left – she felt it was too boring 🙂 )

jLinq Screencast #1

  • The four different types of commands in jLinq — source, query, action and selection.
  • How to select a new object type from the .select() command.
  • Reuse a query after selecting records.
  • Explained how Command Memorizing and Field Memorizing works.
  • Several sample queries.


screencast-screenshot

So, what would you like to see next?

Written by hugoware

July 1, 2009 at 7:12 am

jLinq 2.2.0 Released!

with 14 comments

Ever hear of jLinq. If not, it’s no big deal. Its 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…

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.

//get the 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); } }); [/sourcecode] 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 provice.
  • sum(field): Returns an object with information about the sum of the field name used.
    returns {
    count:the number of records counted,
    result:the sum of all records selected,
    records:the records used to find the sum
    }
  • average(field): Returns an object with information about the average of the field name used.
    returns {
    total:the sum before the average was calculated,
    count:the number of records used,
    result:the average of all records selected,
    records:the records used to find the average
    }
  • 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 {
    source:the record in the jLinq query that is compared,
    compare:the record in the collection array that is compared
    }

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.

Source Code

Download jLinq-2.2.0.js

.

Written by hugoware

June 28, 2009 at 3:04 pm

jLinq And Accepting Parameters

leave a comment »

One of the more complicated parts of jLinq is that any commands, especially query style commands, can accept different number of parameters at any given time. When using the features like command memorizing and field memorizing the function has to assume the values to use.

Another feature that is in development is to make it so that jLinq will accept different types of parameters for commands and then determine the best way to handle them.

For example, the other day I was writing some unit tests for jLinq and entered in the following query…

jLinq.from(data.users)
  .less("first", 5)
  .select();

Okay, all names less than 5 characters, right? Ah… oops…

So that doesn’t work… why is that? Well looking into the code for the ‘less()’ command we find the following.

{name:"greater", count:1, type:"query",
method:function(query, value) {
	return query.when({
		number:function() {
			return (query.value > value);
		},
		string:function() {
			return (query.value.length > value.toString().length);
		},
		array:function() {								
			return (query.value.length > value.length);
		},
		other:function() {
			return (query.value > value);
		}
	});					
}},

So if you notice that command – toString() – you’ll see our problem. Matching against a string field assumes the parameter should be treated as a string. The query is technically evaluating the 5 as a string. Or more specifically, the length of the string…

//the correct way that doesn't work in 2.1.1
record.first.length < 5.toString().length // or 1 (one)
&#91;/sourcecode&#93;

The majority if the time this is going to be correct, but occasionally we run into a query that it doesn't work with.

As of now most of the queries are having a second look to make sure the correct comparison is done. This is being done using a new query helper method named <strong>as()</strong>. This way, if a command expect a number, then it will search for all numeric versions for the value that was passed in.


//checks for the type of value - if it is already a number, then return it
//otherwise, check for a length value
value = query.helper.as(value, {
  number:function(v) { return v; },
  other:function(v) { return v.length; }
});

This way you can focus more on expressing your queries and less concern about what gets passed into it.

Written by hugoware

May 18, 2009 at 5:11 am

jLinq 2.1.1 Released

with 2 comments

A newer version of jLinq is now available – jLinq 2.2.0

jLinq 2.1.1 has been released.

This version makes a fix to a very annoying IE bug that would cause arrays to be sorted reversed from all other browsers.

You most likely will want to update to this version to avoid any problems using jLinq.

Written by hugoware

May 14, 2009 at 3:02 am

jLinq – Extending A Method

leave a comment »

It wasn’t until late into the jLinq project that I had the idea that you should be able to extend your own methods onto the main library. jLinq ended up having quite a rewrite in order to make it fit that model.

jLinq is actually entirely composed to extension methods which I suppose is a form of dog-fooding your own product. If I couldn’t do it with an extension method, then my framework didn’t expose enough to a future developer.

The jLinq Documentation section is still under-construction at this point, so this post serves as a brief introduction to extending jLinq.

jLinq allows you to extend jLinq in three separate ways.

  • Create an entirely new function in the jLinq library
  • Create a new function in a separate namespace in the jLinq library
  • Create your own library (with a copy of the existing jLinq library)

Each of these methods allow you to add one of four types of functions. This may change as time goes on to allow more methods, but for now these are what is available.

Source Commands
A source command is the first command you call when performing a jLinq query. The command .from() is a good example of a source command. Source commands return a jLinq object and any extension methods that have been provided. A source command should also set the data that is being queried

Query Commands
A query command is what sets the comparisons that are used when making a selection. Query commands are not run immediately so you are able to use operators like or() and not() to set how the query is evaluated. The criteria is not evaluated until you call a selection command which is explained more in a moment.

Action Commands
An action command affects a query or data, but doesn’t actually change what is selected. A good example is the .orderBy() command. This command resorts the information for the query, but doesn’t change what will ultimately be selected.

Selection Commands
A selection command can return anything, including the query being created. Normally, a selection command returns the results of the query as an array. Sometimes a selection command will return something different, like with commands like .count() or .isEmpty(), which return numbers and booleans.

More About Extensions

Namespaces
jLinq doesn’t allow you to overwrite a method in the core. Instead, if you want to create a command that is the same name as an existing command, you must provide a namespace to put it into. This way, naming conflicts are minimized. You access a command the same way you normally would, except you include the namespace first jLinq.from(...).custom.startsWith(...).

Custom Libraries
Another thing you can do is create a separate instance of the jLinq library under a different name (technically, you can override jLinq at that point, but that wouldn’t be very nice). If you’re working with a custom library then you can override anything (granted you don’t lock the library too, which will be explained in another post)

More Control

You can see that jLinq gives you complete control over how you use the library. Hopefully people begin to develop add-ins for jLinq that can be shared at the jLinq Message Boards. If you have questions or comments, please share them here or at the message boards. I’d love to get some feedback!

Written by hugoware

May 3, 2009 at 5:10 am

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!

Written by hugoware

April 28, 2009 at 3:35 am

jLinq – Going Online!

leave a comment »

This blog is focused on being about general development along with a lot of discussion about my projects I’m currently working on. I’ve cleared out all of my other blogs and created this one so it can be a broader discussion about development than the others could be.

jLinq is the main project I’ll be posting about for now. jLinq is similar to LINQ for .NET, but it is used in Javascript. It’s especially useful for working with medium sized arrays of information that won’t be changing.

Most of the time people use AJAX to make calls to their server and download the information they need. jLinq allows you use many of the same functions you use in LINQ, but against existing data on the page. 

One of the most important parts of jLinq was that I wanted it to be easy to extend. In fact, at one point the whole jLinq library was wiped and started over and built around an extensibility framework! I wanted the extensibility to work so well that even the core functions of jLinq was built around it!

I’ll blog more about using jLinq, but for now the best thing you can do is try it out on my website! Please leave me feedback about what you think!

Documentation is still in progress, but there are a lot of handy tutorials that will get you started!

Written by hugoware

April 26, 2009 at 5:46 am

jsshell 3.0

with one comment

This weekend I finished up an upgrade for jsshell to version 3.0. I also put together a new jsshell landing page to help users get started.

New Stuff In 3.0

  1. jQuery and jLinq Upgrade: Both libraries have been upgraded to their newest versions. jQuery 1.4.4 and jQuery UI 1.8.7. jLinq has been upgraded to 3.0.2.
  2. Auto Complete: Previously, jsshell would show css classes, IDs and tags that were on the page as a ‘auto-suggest” popup but you couldn’t select anything on the list. Now, jsshell allows you use TAB to select the currently highlighted item. You can use up and down to change the highlighted item.
  3. More Auto Complete Sources: jsshell now shows suggestions for jQuery and jLinq commands, current IDs, tag names and css classes on the page and now some Javascript keywords and common CSS properties.
  4. Remember Last Command: jsshell now remembers the last successfully executed command. If you browse to a new page or refresh the view, the last command you ran will still be waiting for you.
  5. Access ‘file:///’ URL: Previously, Google denied the app from being released because it had access to the file URL. It seems they have changed the way this rule is handled so this version now includes the local file url.
  6. A ‘Run’ Button – Finally…: The only way to run commands before was to press CTRL+Enter. Now there is a button that does the same thing.

Special thanks to @stevehebert for his great suggestions on improvements for jsshell.

Please feel free to let me know if anything isn’t behaving as expected.

Written by hugoware

December 20, 2010 at 1:01 am

100th Blog Post and Hugoware Podcast #1

with 2 comments

Today is my 100th blog post and the end of my first year of blogging so I thought I’d do something a little different.

Each Monday I lead a small programming group/class where we talk about… well… just whatever. One of people in the group is Keagan Williams, who is a high school student, recently competed in a regional programming competition. He and the rest of his team took first place.

Keagan works in a variety of languages and has written some apps for the Android app store, some of which have thousands of downloads.

I’d love to take some credit for how good this kid is but I really can’t.

Anyways, we talked with Keagan for a bit about what the competition was like and some of the things he did. Interesting to hear some of the stuff the upcoming talent is working on.

Download Hugoware Podcast: #1

Written by hugoware

April 26, 2010 at 11:25 pm