Jun 01 2008

Counting, repeating, and unique ids

Tag: otherAndrea Ercolino @ 21:40:42

Here are some micro jQuery plugins that implement JavaScript closures, for object oriented pleasure.

Repeat Something n Times

/**
 * Repeat something n times
 */
$.repeat = function( n ) {
	return {
		times: function( something ) {
			for( var i = 0; i < n; i++ ) {
				something( i );
			}
			return this;
		}
	}
};

/*
 * Example: hip hip hurrah
 */
$.repeat(3).times( function( i ) { 
	$( 'body' ).append( '<span>' + (i < 2 ? "hip" : "hurrah") + ' </span>' );
} );

New Date Value (unique id)

/**
 * Return a new date value
 * 
 * Each call returns a value greater than the previous call
 */
$.newDateValue = (function() {
	var current = 0;
	return function() {
		do {
			var d = Number(new Date());
		} 
		while( current == d );
		return (current = d);
	}
})();

/*
 * Example: 4 unique ids, and 4 not unique
 */
$.repeat(4)
	.times( function( i ) {
		$( 'body' ).append( '<div>' + i + ': ' + $.newDateValue() + '</div>' );
	} )
	.times( function( i ) {
		$( 'body' ).append( '<div>' + i + ': ' + Number(new Date()) + '</div>' );
	} );

Counter

/**
 * Return a counter with three methods: next, current, and init
 */
$.counter = function() {
	var current = 0;
	var increment = 1;
	return {
		/**
		 * Initialize current value and increment
		 */
		  init: function( c, i ) {
			var newCurrent = parseInt( c, 10 ); 
			if (! isNaN( newCurrent )) {
				current = newCurrent;
			}
			var newIncrement = parseInt( i, 10 ); 
			if (! isNaN( newIncrement )) {
				increment = newIncrement;
			}
			return this;
		}
		/**
		 * Get the next value
		 */
		, next: function() { 
			return current += increment; 
		}
		/**
		 * Get the current value
		 */
		, current: function() { 
			return current; 
		}
	}
};

/*
 * Example: 1 2 3 3 1 -1 -3
 */
var k = $.counter();

$.repeat(3).times( function() {
	$( 'body' ).append( '<span>' + k.next() + ' </span>' );
} );

$.repeat(1).times( function() {
	$( 'body' ).append( '<span>' + k.current() + ' </span>' );
} );

k.init( null, -2 );

$.repeat(3).times( function() {
	$( 'body' ).append( '<span>' + k.next() + ' </span>' );
} );


May 17 2008

How to write a fast catch-all RegExp

Tag: FixingAndrea Ercolino @ 18:55:36

In How to write a safe catch-all RegExp I suggested to use (?:\w|\W)* for matching any character in a regular expression. It’s certainly true and safe, and the same stands for its siblings (?:\s|\S)* and (?:\d|\D)*.

If you want to match a large text, these expressions are not the best. I’ve prepared a simple test page where the GeSHi’s engine file, which is almost 120KB, is going to be matched by the regular expression you input.

In Firefox 2 the performance is quite good, about 100 ms on my PC, but in Internet Explorer 7 it takes more than 7 minutes !!!

The best catch-all regular expression is [\w\W]*, which employs about 50 ms in FF2, and 0 ms in IE7 !!! (yes, zero milliseconds)


Mar 18 2008

Metaobjects 1.5 Released

Tag: MetaobjectsAndrea Ercolino @ 23:10:23
Changes
  • Added support for easy namespacing
  • Added support for jQuery 1.2.3 cache
  • Rewritten the manual from scratch
Download

You can download metaobjects from Google Code


Next Page »