PDA

View Full Version : Ext.Array.Date



pdesmet
7 Jul 2011, 11:47 PM
I made an extension on Ext.Array for Dates.
An indexOf and a unique function.

An example:


var array = [new Date('Tue Jun 21 2011 16:00:58 GMT-0700 (Pacific Daylight Time)'),
new Date('Tue Jun 21 2011 16:00:58 GMT-0700 (Pacific Daylight Time)'),
new Date('Fri Jul 01 2011 16:00:58 GMT-0700 (Pacific Daylight Time)')];
console.log(Ext.Array.Date.unique(array));

Source code:


/**
* @author Pieterjan de Smet <pdesmet@paprikadesign.be>
* @docauthor Pieterjan de Smet <pdesmet@paprikadesign.be>
* @class Ext.Array.Date
*
* A set of useful static methods to deal with arrays; provide missing methods for older browsers.

* @singleton
* @markdown
*/
ExtArrayDate = Ext.Array.Date = {

/**
* Get the index of the provided `item` in the given `array`
*
* @param {Array} array The array to check
* @param {Mixed} item The item to look for
* @param {Number} from (Optional) The index at which to begin the search
* @return {Number} The index of item in the array (or -1 if it is not found)
*/
indexOf: function(array, item, from){
var i, length = array.length, returnval;

for (i = (from < 0) ? Math.max(0, length + from) : from || 0; i < length; i++) {
if (array[i].toUTCString() === item.toUTCString()) {
return i;
}
}

return -1;
},

/**
* Returns a new Date array with unique Dates
*
* @param {Array} array of Dates
* @return {Array} results
*/
unique: function(array){
var clone = [],
i = 0,
ln = array.length,
item;

for (; i < ln; i++) {
item = array[i];
if (ExtArrayDate.indexOf(clone, item) === -1) {
clone.push(item);
}
}
return clone;
}
};
Zip file:
26917