Skip to main content

Posts

Showing posts from September, 2012

JavaScript: underscorejs with examples

In previous post I have mention about underscorejs library. And here is the another post to explain bit more about this library.

Some functions used in below example to improve readability 
function prop(name){ return function(obj){ return obj[name]; } } function compareProp(prop, contextProp){ return function(obj){ return obj[prop] === this[contextProp]; } }
Find visible user and show it’s default view
Below is the list of JSON array with user object data. Now we want to show all visible user with it’s default view. var ar = [{ name: 'moe', age: 40, display: true, defaultView: 1, views: [ { name: 'Admin', id: 1 }, { name: 'Role1', id: 2 }, { name: 'Role2', id: 3 } ]},{ name: 'larry', age: 50, display: false, defaultView: 1, views: [ { name: 'Admin', id: 1 }, { name: 'Role1', id: 2 }, { name: 'Role2', id: 3 } ]},{ name: 'curly', age: 60, display: true, defaultVie…

Don’t reinvent the wheel: Some useful JS libraries

It is very common to see “Utility” file in many project, which have all commonly used functions. In other word you can say it is kitchen sink file, where people dump all common functions. Before you realize it grows more than 1000 lines and become a maintenance hell. So keep in mind, each time when you write a new class or function you are increasing the project maintenance coast. There are few things you should consider before writing the code Know Your Library – Most of the library has some utility functions so keep exploring it. Use Utility Library – If existing library doesn't have needed functionality then always consider using 3rd party Utility Library with good test converge.Some of my favourite libraries Underscore JSIt is one of the best utility library, which has collection of most commonly used functions. Here are few function with example code. Each_.each([1, 2, 3], function(num){ alert(num); }); => alerts each number in turn... _.each({one : 1, two : 2, three …

BOM Character: Unknown guest in your file

Many times I have seen special character like “  ” in starting of file and most of the time I ignored it. But recently it started causing more trouble, when my JS validation utility started failing. And log file shows same special character again. After more investigation, I find out it was introduce when I creates a new file in Eclipse.

So, what is this symbol?

It is a BOM (byte order mark) character used to signal the endianness (byte order) of a text file or stream[wiki]. This character differ based on encoding type like

Encoding Symbol UTF-8UTF-16 (BE)þÿUTF-16 (LE)ÿþ
Next question is, how to remove this character from my file. And if you are using Notepad++ editor it will be much easier. You have to select the “Encoding” option from menu bar and choose “Convert to UTF-8 without BOM”.




How to handle BOM character in your code?

I will suggest to use some sophisticated library instead of rolling out your own solution. And “Apache Commons IO” library has dedicated class “BOMInputStr…