function assert(c, msg) {
if(!c) {
throw new Error(msg);
}
}
Object.prototype.equals = function(obj) {
return this == obj;
};
Object.prototype.compareTo = function(obj) {
return this < obj ? -1 : this == obj ? 0 : 1;
};
Number.prototype.doubleValue = function() {
return this.valueOf();
};
Number.prototype.intValue = function() {
var v = this.doubleValue();
return v < 0 ? Math.ceil(v) : v > 0 ? Math.floor(v) : 0;
};
Number.prototype.compareTo = function(obj) {
assert(obj instanceof Number, "param type is error!");
var v = obj.valueOf();
return this.valueOf() < v ? -1 : this.valueOf() > v ? 1 : 0;
};
Number.prototype.equals = function(obj) {
return this.compareTo(obj) == 0;
};
function Integer(value) {
Number.call(this, parseInt(value));
}
Integer.prototype = new Number();
Integer.prototype.toXxxString = function(obj, r) {
try {
obj = new Integer(obj);
} catch(error) {
throw error;
}
switch(arguments.length) {
case 0: return this.toString(r);
case 1: return obj.tostring(r)
}
};
Integer.prototype.toHexString = function(obj) {
return this.toXxxString(obj, 16);
};
Integer.prototype.toOctalString = function(obj) {
return this.toXxxString(obj, 8);
};
Integer.prototype.toBinaryString = function(obj) {
return this.toXxxString(obj, 2);
};
Boolean.prototype.booleanValue = function() {
return this.valueOf();
};
Boolean.prototype.equals = function(obj) {
obj = Boolean(obj);
return this.valueOf() && obj.valueOf() || !this.valueOf() && !obj.valueOf();
};
Boolean.prototype.compareTo = function(obj) {
try {
obj = Number(obj);
} catch(error) {
throw error;
}
var t = Number(this);
return t < obj ? -1 : t == obj ? 0 : 1;
};
Boolean.prototype.xor = function(b) {
b = Boolean(b);
var tmp = this.valueOf();
return tmp && !b || !tmp && b;
};
Boolean.prototype.imp = function(b) {
return !this.xor(b);
};
Boolean.prototype.notor = function(b) { // P -> Q = !P || Q
b = Boolean(b);
var tmp = this.valueOf();
return !tmp || b;
};
String.prototype.codePointAt = function(index) {
assert(index instanceof Number, "param type is error!");
return this.charCodeAt(index);
};
String.prototype.compareTo = function(str) {
assert(str instanceof String, "param type is error!");
var index = 0;
var v1 = null, v2 = null;
while(index < this.length && index < str.length) {
v1 = this.charAt(index);
v2 = str.charAt(index);
if(v1 != v2) {
return v1 > v2 ? 1 : -1;
}
index++;
}
return this.length == str.length ? 0 : index >= this.length ? -1 : 1;
};
String.prototype.compareToIgnoreCase = function(str) {
assert(str instanceof String, "param type is error!");
var tmp = this.toLowerCase();
str = str.toLowerCase();
return tmp.compareTo(str);
};
String.prototype.equals = function(str) {
return this.compareTo(str) == 0;
};
String.prototype.equalsIgnoreCase = function(str) {
assert(str instanceof String, "param type is error!");
var tmp = this.toLowerCase();
str = str.toLocaleString();
return tmp.equals(str);
};
String.prototype.toCharArray = function() {
return this.split("");
};
String.prototype.isEmpyt = function() {
return this.length == 0;
};
String.prototype.startsWith = function(str, start) {
return this.index(str, start) >= 0;
};
String.prototype.endsWith = function(str, start) {
return this.lastIndexOf(str, start) >= 0;
};
Date.prototype.compareTo = function(date) {
if(!(date instanceof Date)) {
try {
date = new Date(Date.parse(date.toString()));
} catch(error) {
throw error;
}
}
var t1 = this.getTime(), t2 = date.getTime();
return t1 > t2 ? 1 : t1 == t2 ? 0 : -1;
};
Date.prototype.equals = function(date) {
return this.compareTo(date) == 0;
};
Date.prototype.before = function(date) {
return this.compareTo(date) < 0;
};
Date.prototype.after = function(date) {
return this.compareTo(date) > 0;
};
Array.prototype.compareTo = function(arr) {
assert(arr instanceof Array, "param type is error!");
var index = 0;
while(index < this.length && index < arr.length) {
if(this[index].compareTo(arr[index]) > 0) {
return 1;
} else if(this[index].compareTo(arr[index]) < 0) {
return -1;
}
index++;
}
var max = Math.max(this.length, arr.length);
return index >= max ? 0 : index >= this.length && index < max ? -1 : 1;
};
Array.prototype.equals = function(arr) {
return this.compareTo(arr) == 0;
};
呃~~招免费测试的哈,嘿嘿
最新推荐文章于 2024-08-23 14:42:33 发布