Skip to content

Instantly share code, notes, and snippets.

@blindsey
Created October 3, 2011 22:44
Show Gist options
  • Save blindsey/1260461 to your computer and use it in GitHub Desktop.
Save blindsey/1260461 to your computer and use it in GitHub Desktop.
A really cool JS library
/**
* OVEE namespace
*/
OVEE = {
BLOCKED_COMMENT_TEXT : 'This comment has been removed by the moderator'
};
/**
* Number
*/
Number.prototype.pad = function( digits ) {
return ( Math.pow( 10, digits ) + this ).toString().slice(1);
};
Number.prototype.duration = function() {
return Math.floor( this / 60000 ) + 'min ' + Math.floor( (this % 60000) / 1000 ) + 'sec';
};
/**
* String
*/
String.prototype.duration = function() {
return parseInt( this, 10 ).duration();
};
/**
* Date
*/
Date.prototype.toLocaleString = function() {
if( isNaN( this.getTime() ) ) {
return "Unavailable";
} else {
var hours = this.getHours() % 12;
if( hours === 0 ) hours = 12;
var time = hours + ':' + this.getMinutes().pad(2) + (this.getHours() > 11 ? 'pm' : 'am');
return this.toLocaleDateString() + " at " + time;
}
};
/**
* Enumerable
*/
Array.prototype.first = function() {
return this[0];
};
Array.prototype.last = function() {
return this[ this.length - 1 ];
};
Array.prototype.any = function( iterator ) {
for( var i = 0, max = this.length; i < max; ++i ) {
if( iterator( this[i] ) ) return true;
}
return false;
};
Array.prototype.all = function( iterator ) {
for( var i = 0, max = this.length; i < max; ++i ) {
if( !iterator( this[i] ) ) return false;
}
return true;
};
Array.prototype.collect = Array.prototype.map;
Array.prototype.detect = function( iterator ) {
for( var i = 0, max = this.length; i < max; ++i ) {
if( iterator( this[i] ) ) return this[i];
}
return;
};
Array.prototype.select = function( iterator ) {
var matches = [];
for( var i = 0, max = this.length; i < max; ++i ) {
if( iterator( this[i] ) ) matches.push( this[i] );
}
return matches;
};
Array.prototype.reject = function( iterator ) {
var matches = [];
for( var i = 0, max = this.length; i < max; ++i ) {
if( !iterator( this[i] ) ) matches.push( this[i] );
}
return matches;
};
Array.prototype.inject = function( memo, iterator ) {
for( var i = 0, max = this.length; i < max; ++i ) {
memo = iterator( memo, this[i] );
}
return memo;
};
Array.prototype.clear = function() {
this.slice( 0 );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment