2011-08-24 17:15:42 +02:00
|
|
|
/*** Array ***/
|
|
|
|
|
2011-08-22 20:43:16 +02:00
|
|
|
Array.prototype.random = function() {
|
|
|
|
return this[Math.floor((Math.random()*this.length))];
|
|
|
|
};
|
2011-08-24 02:57:52 +02:00
|
|
|
|
|
|
|
Array.prototype.each = function(fun) {
|
|
|
|
for(var i=0;i<this.length;i++) {
|
|
|
|
fun(this[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Array.prototype.collect = function(fun) {
|
|
|
|
var collect = [];
|
|
|
|
for(var i=0;i<this.length;i++) {
|
|
|
|
collect.push(fun(this[i]));
|
|
|
|
}
|
2011-08-24 17:15:42 +02:00
|
|
|
return collect;
|
2011-08-24 02:57:52 +02:00
|
|
|
};
|
|
|
|
|
2011-08-24 03:20:13 +02:00
|
|
|
Array.prototype.include = function(value) {
|
|
|
|
for(var i=0;i<this.length;i++) {
|
|
|
|
if(this[i] == value) {
|
2011-08-24 17:15:42 +02:00
|
|
|
return true;
|
2011-08-24 03:20:13 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-24 17:15:42 +02:00
|
|
|
return false;
|
2011-08-24 03:20:13 +02:00
|
|
|
};
|
|
|
|
|
2011-08-24 02:57:52 +02:00
|
|
|
/*** String ***/
|
|
|
|
|
|
|
|
String.prototype.endsWith = function(needle) {
|
|
|
|
var end = this.slice(this.length - needle.length);
|
|
|
|
if(needle === end) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
String.prototype.startsWith = function(needle) {
|
2011-08-24 03:20:13 +02:00
|
|
|
var start = this.slice(0, needle.length);
|
2011-08-24 02:57:52 +02:00
|
|
|
if(needle === start) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*** Object ***/
|
|
|
|
|
|
|
|
Object.prototype.isFunction = function(obj) {
|
|
|
|
if(typeof(obj) == 'function') {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2011-08-24 03:20:13 +02:00
|
|
|
|
|
|
|
Object.prototype.isArray = function(obj) {
|
|
|
|
if(Object.prototype.toString.call(obj) === '[object Array]') {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|