dbot/snippets.js

65 lines
1.3 KiB
JavaScript
Raw Normal View History

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))];
};
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 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
};
/*** String ***/
String.prototype.valMatch = function(regex, expLength) {
var key = this.match(regex);
if(key !== null && key.length == expLength) {
return key;
} else {
return false;
}
};
String.prototype.endsWith = function(needle) {
2011-08-26 13:45:49 +02:00
return needle === this.slice(this.length - needle.length);
};
String.prototype.startsWith = function(needle) {
2011-08-26 13:45:49 +02:00
return needle === this.slice(0, needle.length);
};
/*** Object ***/
Object.prototype.isFunction = function(obj) {
2011-08-26 13:45:49 +02:00
return typeof(obj) === 'function';
};
2011-08-24 03:20:13 +02:00
Object.prototype.isArray = function(obj) {
2011-08-26 13:45:49 +02:00
return Object.prototype.toString.call(obj) === '[object Array]';
2011-08-24 03:20:13 +02:00
};
/*** Integer ***/
Number.prototype.chanceIn = function(x, y) {
var num = Math.floor(Math.random() * (y + 1)) / x;
2011-08-26 13:45:49 +02:00
return num == 1;
};