/*
该程序定义对已有对象的扩展
*/
Array.prototype.foreach = function(lpfun) {for (i = 0; i < this.length; i++){if(this[i]!='') {lpfun(this[i])}}}
Array.prototype.foreachindex = function(lpfun) {for (i = 0; i < this.length; i++){if(this[i]!='') {lpfun(i,this[i])}}}
Array.prototype.Add=function(value){this.push(value)}
Array.prototype.foreachdic=function(lpfun){dic=this;for(key in dic){lpfun(dic[key])}}
Array.prototype.del = function(n){if (n < 0)return this;else return this.slice(0, n).concat(this.slice(n + 1, this.length));}
String.prototype.foreach=function(strsplit,lpfun){data=this.split(strsplit);for(i=0;i<data.length;i++){if(data[i]!=''){lpfun(i,new String(data[i]))}}}
String.prototype.toCSV=function(splitcell,splitrow,Fields,priindex){
data=this.split(splitrow);if(Fields==undefined){Fields=data[0].split(splitcell);data.del(0)}
if(priindex==undefined)priindex=-1;if (priindex>=Fields.length)priindex=-1;
var ret=[]
for(i=0;i<data.length;i++){
rs=data[i].split(splitcell);record=[];
for(j=0;j<rs.length;j++){record[Fields[j]]=rs[j]}
if(priindex>=0) {ret[record[Fields[priindex]]]=record}
else ret[priindex]=record
}
return ret
}
//是否是正确的IP地址
String.prototype.isIP = function() {
var reSpaceCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
if (reSpaceCheck.test(this)) {
this.match(reSpaceCheck);
if (RegExp.$1 <= 255 && RegExp.$1 >= 0 && RegExp.$2 <= 255 && RegExp.$2 >= 0 && RegExp.$3 <= 255 && RegExp.$3 >= 0 && RegExp.$4 <= 255 && RegExp.$4 >= 0) return true;
else return false;
} else return false;
}
//是否是正确的长日期
String.prototype.isLongDate = function() {
var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
if (r == null) return false;
var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4] && d.getHours() == r[5] && d.getMinutes() == r[6] && d.getSeconds() == r[7]);
}
//是否是正确的短日期
String.prototype.isShortDate = function() {
var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if (r == null) return false;
var d = new Date(r[1], r[3] - 1, r[4]);
return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]);
}
//是否是正确的日期
String.prototype.isDate = function() {return this.isLongDate() || this.isShortDate();}
//是否是手机
String.prototype.isMobile = function() {return /^0{0,1}13[0-9]{9}$/.test(this);}
//是否是邮件
String.prototype.isEmail = function() {return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);}
//获取字符数组
String.prototype.toCharArray = function() {return this.split("");}
//获取N个相同的字符串
String.prototype.repeat = function(num) {
var tmpArr = [];
for ( var i = 0; i < num; i++)
tmpArr.push(this);
return tmpArr.join("");
}
//逆序
String.prototype.reverse = function() {return this.split("").reverse().join("");}
//测试是否是数字
String.prototype.isNumeric = function() {
var tmpFloat = parseFloat(this);
if (isNaN(tmpFloat))
return false;
var tmpLen = this.length - tmpFloat.toString().length;
return tmpFloat + "0".Repeat(tmpLen) == this;
}
//测试是否是整数
String.prototype.isInt = function() {if (this == "NaN")return false;return this == parseInt(this).toString();}
// 合并多个空白为一个空白
String.prototype.resetBlank = function() {return this.replace(/s+/g, " ");}
// 除去左边空白
String.prototype.LTrim = function() {return this.replace(/^s+/g, "");}
// 除去右边空白
String.prototype.RTrim = function() {return this.replace(/s+$/g, "");}
// 除去两边空白
String.prototype.trim = function() {return this.replace(/(^s+)|(s+$)/g, "");}
// 保留数字
String.prototype.getNum = function() {return this.replace(/[^d]/g, "");}
// 保留字母
String.prototype.getEn = function() {return this.replace(/[^A-Za-z]/g, "");}
// 保留中文
String.prototype.getCn = function() {return this.replace(/[^u4e00-u9fa5uf900-ufa2d]/g, "");}
// 得到字节长度
String.prototype.getRealLength = function() {return this.replace(/[^x00-xff]/g, "--").length;}
// 从左截取指定长度的字串
String.prototype.left = function(n) {return this.slice(0, n);}
// 从右截取指定长度的字串
String.prototype.right = function(n) {return this.slice(this.length - n);}
// HTML编码
String.prototype.HTMLEncode = function() {
var re = this;
var q1 = [ /x26/g, /x3C/g, /x3E/g, /x20/g ];
var q2 = [ "&", "<", ">", " " ];
for ( var i = 0; i < q1.length; i++)
re = re.replace(q1[i], q2[i]);
return re;
}
// Unicode转化
String.prototype.ascW = function() {
var strText = "";
for ( var i = 0; i < this.length; i++)
strText += "&#" + this.charCodeAt(i) + ";";
return strText;
}
String.prototype.foreach=function(strsplit,lpfun){data=this.split(strsplit);for(i=0;i<data.length;i++){if(data[i]!=''){lpfun(i,new String(data[i]))}}}
String.prototype.toJson=function(){return eval('(' + this + ')');}
String.prototype.toCSV=function(splitcell,splitrow,Fields,priindex){
data=this.split(splitrow);if(Fields==undefined){Fields=data[0].split(splitcell);data.del(0)}
if(priindex==undefined)priindex=-1;if (priindex>=Fields.length)priindex=-1;
var ret=[]
for(i=0;i<data.length;i++){
rs=data[i].split(splitcell);record=[];
for(j=0;j<rs.length;j++){record[Fields[j]]=rs[j]}
if(priindex>=0) {ret[record[Fields[priindex]]]=record}
else ret[priindex]=record
}
return ret
}
//是否是正确的IP地址
String.prototype.isIP = function() {
var reSpaceCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
if (reSpaceCheck.test(this)) {
this.match(reSpaceCheck);
if (RegExp.$1 <= 255 && RegExp.$1 >= 0 && RegExp.$2 <= 255 && RegExp.$2 >= 0 && RegExp.$3 <= 255 && RegExp.$3 >= 0 && RegExp.$4 <= 255 && RegExp.$4 >= 0) return true;
else return false;
} else return false;
}
//是否是正确的长日期
String.prototype.isLongDate = function() {
var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
if (r == null) return false;
var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4] && d.getHours() == r[5] && d.getMinutes() == r[6] && d.getSeconds() == r[7]);
}
//是否是正确的短日期
String.prototype.isShortDate = function() {
var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if (r == null) return false;
var d = new Date(r[1], r[3] - 1, r[4]);
return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]);
}
//是否是正确的日期
String.prototype.isDate = function() {return this.isLongDate() || this.isShortDate();}
//是否是手机
String.prototype.isMobile = function() {return /^0{0,1}13[0-9]{9}$/.test(this);}
//是否是邮件
String.prototype.isEmail = function() {return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);}