Javascript Language Sugar (one liners)


Here are some good one liners in javascript

//Hello World Program Which is Palindrome

(alert)('Hello, World') && ('dlroW ,olleH')(trela)


//Reverse a string

"this is a string".split("").reverse().join("");


//Trim a string

" Hello World ".replace(/^\s*|\s*$/g,'')


//HTML escape

"<b>Hello HTML</b>".replace(/([&<>])/g,function (c) {return "&" + {"&": "amp","<": "lt",">": "gt"}[c] + ";";});


//Shuffle an array

[1,2,3,4,5].sort(function(){return (4*Math.random()>2)?1:-1})


//Detect IE

isIE='\v'=='v';


//Force String to be a Number (multiply or divide by 1)

"100"*1


//Force to be boolean

!!"some value"


//Converting string to Leet

"Hello Hacker".replace(/[a-z]/g,function f(a){return "4BCD3F6H1JKLMN0PQR57"[parseInt(a, 36)-10] || a.replace(/[a-t]/gi,f)});


//Clone an array

var clone = [1,2,4,5,6,9].slice(0);

YUI DateTime Chooser

I just created YUI3 DateTime Chooser Module.

What is it?


At some time in a webform you have to capture date and time together with timezone. This YUI3 module is to help you in progressively enhance "date-time form input" to popup (calendar and slider based) UI to input date and time with TimeZone.

How to Use?



<script src="http://yui.yahooapis.com/3.7.3/build/yui/yui-min.js"></script>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?gallery-2012.12.12-21-11/build/gallery-datetime-input/assets/gallery-datetime-input/gallery-datetime-input-core.css">
<script>
YUI({
gallery: 'gallery-2012.12.12-21-11'
}).use('gallery-datetime-input', function (Y) {
  new Y.DateTimeInput({
    inputBox:"#datetime-input"
   });
});
</script>