Posts Tagged ‘javascript’

Protoaculous 1.9.0 Minified

Premise
I used to download the updated versions of protoaculous from Prototype Core Google Group. Recently, Google announced that the Files section of the Google Groups will be deprecated. Also, nobody has been updating the copy of protoaculous in that group (or anywhere else on the public google-searched internet for that matter).

Instructions
The instructions to create it are really simple and so you can do it yourself. You can read them at Boog Design.

Download
For the lazy (like me), here’s the minified version of Prototype.js 1.7.0 (using the new Sizzle CSS Selector Engine – v1.0) + Scriptaculous 1.9.0:
Download

Coral CDN
Cached version (similar to ajax.googleapis.com versions):
http://inderpreetsingh.com.nyud.net/wp-content/uploads/protoaculous.1.9.0.min_.js

Other info
Size of Prototype.js + Scriptaculous = 287,939 bytes
Size of Protoaculous.js Minified = 168,754 bytes

Packed: prototype.js + scriptaculous.js + builder.js + effects.js + dragdrop.js + controls.js + slider.js + sound.js

Chrome’s javascript sort array function is different, yet proper

Chrome’s javascript sort function behaves more like the ECMA standards than the sort function implemented in other browsers (like Firefox, IE, etc), which try to maintain some backwards compatibility with legacy javascript code.

Generally, when sorting an array, I’ve always found the following (incorrect) code:

result.sort(function(a,b) { return a > b } );

According to the ECMA standards, the right way to do it is not to return true or false, like the above function, but to return -1, 0, or 1, depending on how the two strings compare, like the following:

function sortfn (a,b) {
	var upA = a.toUpperCase();
	var upB = b.toUpperCase();
	return (upA < upB) ? -1 : (upA > upB) ? 1 : 0;
}

Javascript/JSON: Find Index in an Array of Objects

JSON is one of my favorite data models notation (xml = :(). A lot of times, I get arrays of objects (hash tables) that I then have to search again an again to find the index of the object inside that array. Here’s a quick function to find it:


/*
function findIndexByKeyValue: finds "key" key inside "ob" object that equals "value" value
example: findIndexByKeyValue(students, 'name', "Jim");
object: students = [
   {name: 'John', age: 100, profession: 'Programmer'},
   {name: 'Jim', age: 50, profession: 'Carpenter'}
];
would find the index of "Jim" and return 1
*/

function findIndexByKeyValue(obj, key, value)
{
	for (var i = 0; i < obj.length; i++) {
		if (obj[i][key] == value) {
			return i;
		}
	}
	return null;
}

Avoid for (var x in array) when using jQuery/PrototypeJS

I was looking to make my code look more readable by “cleverly” using for (var x in array) loops instead of for (var x=0; x < array.length; x++), even though the shorter for loops are not supposed to be used with arrays, but used only with objects.

Turns out that jquery/prototypeJS put in extra hidden variables inside the array. If for whatever reason you are not using a JavaScript framework/library, you can use cross-browser shortcut: for (var x in array). However, be cautioned JavaScript experts are particularly annoyed by it.

Return top