Some JS nuggets I use frequently in the projects that I work on. Since all I am ever doing is epic-grade intensive agile development inside of the cloud on my magic carpet powered by Amazon AWS and Rails and ZFS and Perl6 and Python3000 and Google BigTable… I really have to have some really intense and powerful javascript functionality at my disposal. Typically I would create each of these as a jQuery plugin that requires nothing at all but simply $(document).whaleify(); to automagically and intelligently calculate where each of these might be needed… but since I am too busy re-writing Rails apps in Haskell atop .NET because they are too slow… well you can just see why. It’s also safe to say that all of these functions are 100% scalable and entirely concurrent. It’s really neat-o stuff, bleeding edge.
P.S. Don’t read any of that, start reading here:
String Shortening Function
You can use this to shorten a string to a desired length (n), with a desired indicator (i). Some might whine about modifying the String object, but haters can suck my balls.
String.prototype.shorten = function(n, i) {
var indicator = i || '…';
return (this.length > n) ? String(this.substr(0, n)+indicator) : String(this);
};
// Examples
var foo = 'Snuggleuffagus';
alert(foo.shorten(10)); // Snuggleuff…
alert(foo.shorten(3, '»')); // Snu»
APNumber Function
This extends the Number object with a method which will convert (in this case) any number less than 13 to it’s string equivalent. 3 becomes Three, 4 becomes Four, etc… Of course you can make the array of numbers as long as you’d like. One downside to this is the fact that the strings are all capitalized, but work your magic young Jedi and improve on this chestnut of shit.
It is important to mention here that anything over 9 is actually not technically an “apnumber” (thanks obeattie, my jQuery scalability expert / broheem) but like I said earlier… haters can suck my balls.
Number.prototype.apnumber = function() {
var numberMap =
[ 'Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen' ];
return (13 >= this) ? numberMap[this] : this;
};
// Examples
var foo = 4;
alert(foo); // 4
alert(foo.apnumber()); // Four
var bar = 45;
alert(bar.apnumber()); // 45
Pluralize Function
This final nugget of crap you can chew on also extends the Number object, giving you the ability to make words plural based upon a variable value. 1 cherry, four cherries, etc… you get the deal.
It accepts two optional parameters, the singular and plural versions of the word you are trying to pluralize. Certain words would require no arguments such as dog vs. dogs, nugget vs nuggets, etc… but there are of course exceptions to everything in this marvelous language of ours… like child and children, cherry and cherries, etc…
Number.prototype.pluralize = function(singular, plural) {
var a = '', b = 's';
if (singular && plural) { a = singular; b = plural; }
return (this == 1) ? a : b;
};
// Examples
var foo = 3;
alert('You have '+foo+' cherr'+foo.pluralize('y', 'ies')+' in mai bucket.'); // You have 3 cherries in mai bucket.
foo = 1;
alert('You have '+foo+' cherr'+foo.pluralize('y', 'ies')+' in mai bucket.'); // You have 1 cherry in mai bucket.
// Combine these nuggets of crap for even more craptacular javascript!
foo = 3;
alert('You have '+foo.apnumber()+' cherr'+foo.pluralize('y', 'ies')+' in mai bucket.'); // You have One cherry in mai bucket.
Enjoy.