Javascripts, Linq

Overview: jLinq

jLinq is a Javascript library that lets you write queries against arrays of javascript objects and JSON data.

Syntax and Examples

Syntax for jLinq is much similar to LINQ ,

var results = jLinq.from(data.users)
.startsWith("first", "a")
.orEndsWith("y")
.orderBy("admin", "age")
.select();

Some important commands

  • .startsWith( field?, value ): Check to see if the field starts with the specified value.
    //Some sample queries
    var results = jLinq.from(records)
    .startsWith("firstname", "jo") // first name starting with 'jo'
    .or("ji") // or first name starting with 'ji'
     
    var results = jLinq.from(records)
    //you can use an array of values to check
    .startsWith("firstname", ["a","e","i","o","u"])
    
  • .endsWith( field?, value ) : Check to see if the field starts with the specified value.
  • .contains( field?, value ) : Check to see if the field contains the specified value.
  • .match( field?, regexp ): Check to see if the
    value matches the regular expression. This command will still take case
    sensitivity into account.
  • .empty( field? ): Check to see if the field contains any value.
  • .greater( field?, value ): Check to see if the field is less than the provided value.

If the field is…

– a string, checks if it ends with the value
– an array, checks if the last element matches the value
– else, convert to a string and check returns jLinq Object

For all commands with examples see this.

Retrieve and Display Results

results[index][‘field’] will retrieve values from result object.

Following e.g. will retrieve data and displays in table,

var results = jLinq.from(data.users)
.startsWith("first", "a")
.toTable();
document.write(results);

Creating A Query Command

We can also create our own commands,

jLinq.extend({
    name:"evenValues", //the name of this command i.e. method
    type:"query", //the kind of command 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;
    }
});

E.g using custom command,

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

When you extend a query command , several additional operator prefixed name command are added to jLinq as well. Each of these perform the correct switches when they are used.

//this orEvenValues command is generated automatically
 
jLinq.from(data.users)
    .less("age", 30)
    .orEvenValues()
    .select();
Freebies

HTML 5 Cheat Sheets

Last week I had given useful cheats sheets , which contains useful information for developer which gives quick reference to programming language.

Now here are some new cheat sheets for HTML 5,

html5cheat

Html & CSS

Overview: HTML 5

HTML 5 is the next major version of HTML. It introduces a new elements that will make our pages more semantic. This will make it a lot easier for search engines and screen readers to navigate our pages, and improve the web experience for everyone.

Some basics that we should know

  • DOCTYPE :

    HTML 5 uses < !DOCTYPE html>which tells browser that current document is written in HTML5 .

  • New Elements:

  1. <header>
    The header element contains introductory information to a section or page. This can involve anything from our normal documents headers (branding information) to an entire table of contents.
  2. <nav>
    The nav element is reserved for a section of a document that contains links to other pages or links to sections of the same page. Not all link groups need to be contained within the <nav> element, just primary navigation.
  3. <section>
    The section element represents a generic document or application section. It acts much the same way a <div> does by separating off a portion of the document.
  4. <article>
    The article element represents a portion of a page which can stand alone such as: a blog post, a forum entry, user submitted comments or any independent item of content.
  5. <aside>
    Aside, represents content related to the main area of the document. This is usually expressed in sidebars that contain elements like related posts, tag clouds, etc. They can also be used for pull quotes.
  6. <footer>
    The footer element is for marking up the footer of, not only the current page, but each section contained in the page. So, it’s very likely that you’ll be using the <footer>
  • APIs

HTML 5 introduces a number of APIs that help in creating Web applications. These can be used together with the new elements introduced for applications:

  1. 2D drawing API which can be used with the new canvas element.
  2. API for playing of video and audio which can be used with the new
    video and audio elements. Syntax for video embedding may like
    <video width=”400″ height=”360″ src=”vid.mp4″>
  3. An API that enables offline Web applications.
  4. Editing API in combination with a new global
    contenteditable attribute.
  5. Drag & drop API in combination with a draggable attribute.
  6. API that exposes the history and allows pages to add to it to prevent
    breaking the back button.

Demo:

I have created demo page by referring post by Mads Kjaer and HTML 5 working draft . Mads has explained very well in the post.

The structure of page is,

<!doctype html>
<html>
<head>
	<title>Page title</title>
</head>
<body>
	<header>
		<h1>Page title</h1>
	</header>
	<nav>
		<!-- Navigation -->
	</nav>
	<section>
		<!-- Main content area -->
	</section>
	<aside>
		<!-- Sidebar -->
	</aside>
	<footer>
		<!-- Footer -->
	</footer>

</body>
</html>
html5Layout
html5 Demo
JQuery

Gritter: jquery notification plugin

Gritter is jquery plugin for notification which displays messages in bubble/popup that appears in the top of web page.

gritter

It has facility to disappear automatically after x seconds or display until manually cloase with the “sticky” option.

We can set the speed of the fade effect used while the notifications appear/disappear.

Asp.net, C#, Freebies, Html & CSS, Linq, Regex

8 useful Cheat Sheets for developer

I have found awesome collections of  cheat sheets on internet which gives quick reference guide of various functionality.

Here are some  good cheats links,

JQuery

jQuery Image Cropping Plugin

Jcrop is the quick and easy way to add image cropping functionality to your web application. It combines the ease-of-use of a typical jQuery plugin with a powerful cross-platform DHTML cropping engine that is faithful to familiar desktop graphics applications.

jcrop

Asp.net

Encoding HTML special characters

The HTML Encode method in asp.net applies HTML encoding to a specified string. This is useful method of encoding form data.It converts potentially unsafe characters to their HTML-encoded equivalent.

e.g.
() is converted to >.
(&) is converted to &.
(“) is converted to ".

How to Use:

1.
2. div.InnerHtml = Server.HtmlEncode(“Enter a word <here>”);

Asp.net

URL Encryption

Why:

It always good to send query string values in encrypted format. Encrypted URLs prevents sending illegal requests or invalid data to the application server. Users or attackers cannot see the request details of the URL as they are encrypted.

Example

Before encryption,

http://localhost:60608/EncryptURLDemo/Default2.aspx?text=amol

After encryption,

http://localhost:60608/EncryptURLDemo/Default2.aspx?q=dSoxSTYQLHY3arcO+hfCO7uMyRNnIpRCoUqk0oyR80c=

How:

Method 1: Manually encrypt / decrypt query string parameter

  1. Copy EncryptionHelper.dll into Bin directory of your web application.
  2. To encrypt query string values,
    string url=string.Format("~/Default2.aspx?text= {0}",EncryptionHelper.Encrypt("valuehere"));
  3. To decrypt query string values,
    if (Request.QueryString["text"] != null)
    Text = EncryptionHelper.Decrypt(Request.QueryString["text"]);

Method 2: Automatic encrypt / decrypt query string using HttpModule

  1. Copy EncryptionHelper.dll into Bin directory of your web application.
  2. Add the following lines to the web.config’s section:
    <httpModules>
     <add type = "QueryStringModule" name = "QueryStringModule" />
    </httpModules>