Friday, March 23, 2012

Unix sed - Fun at Work - Its Friday!

In my constant pursuit of efficiency vis-a-vis laziness, I ended up writing this stream editor command. I needed to transform this long list of constants in java into an HTML select tag. This served as a perfect opportunity to use sed.
Input: This comes from my file.
public static final String SOME_CONSTANT_1 = "CST1";
public static final String SOME_CONSTANT_2 = "CST2";
Output:


Here is the magical command.
sed -e 's/[[:space:]]\{1,\}/ /g' \                                                    
-e 's/.*g[[:space:]]\(.*\);/\1/g' \                                                   
-e 's/\(.*\)[[:space:]]*=[[:space:]]*"\(.*\)"/<option value="\2">\1<\/option>/g' \ 
fileName
  • [1]: Replace multiple spaces/tabs [white spaces] in and around a line in a file with a single space.
  • [2]: Get rid of unnecessary text
  • [3]: Swap and format the output to HTML

Friday, March 16, 2012

Javascript Singleton Pattern

This is just a working example of the idea discussed here. I thought it would help to see the working behavior.
var QuickDialog = function(){
    var privateInit = function(){console.log("init")};
    return{
        init: privateInit
    };
}();

QuickDialog.init();
QuickDialog.privateInit();
This is the firebug output for the above code.

Javascript === vs ==

=== also looks at the type


var a = "1";
var b = 1;
console.log(a===b); //false
console.log(a==b); //true