Cool JavaScript code in

This weekend I stumbled over a cool repository to see what’s possible in JavaScript even in very short code snippets:

There you find functions and other small examples of JavaScript code in Tweet-length. Mostly very elegant solutions, sometimes hard to understand directly, but always with a longer, annotated version.

Most functions can be used as-is in vCO Scriptable elements, like this one to figure out the ordinal suffix of a number (the “st” of 1st, “nd” of 2nd and so on):

function b(a){return["th","st","nd","rd"][(a=~~(a10&&a3?0:a]}

In a vCO Scriptable Element (or an Action) the annotated long version would look like this (theNumber is an IN-Parameter of type Number):

var ret = ordinal(theNumber);
System.log(theNumber + ret);

function ordinal(
a                         // number
"th","st","nd","rd"     // array of ordinal suffixes
(a=~~                   // floor the value for usable index (integer)
(a10&&a3                      // all digits above 3 return "th"
?0                      // return "th" for above cases
:a                      // return "th", "st", "rd" for others

Just be aware that some of the snippets use the prototype-Feature of JavaScript (to extend basic objects with new methods). This is not available in vCO and you will get an error like

Cannot add a property to a sealed object: ….

But there are enough other functions for lots of nice use-cases in vCO. Enjoy! :mrgreen: