jquery下的js(常用)

//js、C#、sql三种的Trim竟然都不一样,才发现

//js
//Trim
String.prototype.Trim = function () {
return this.replace(/^s*|\s$/g, '');
};

//C#  value.Trim()

//sql //ltrim(rtrim(value))
//ToJsonString
String.prototype.ToJsonString = function () {
return this.replace(/'/g, "\\'");
};

//ToList
String.prototype.ToList = function (Separator) {
if (Separator == undefined) {
Separator = ",";
};
var NewString = this.Trim();
if (NewString.legth > 0) {
return NewString.split(Separator);
}
else {
return [];
};
};

//remove
Array.prototype.remove = function (obj) {
var index = this.indexOf(Obj);
if (index > -1) {
this.slice(index, 1);
};
};

//Contains
Array.prototype.Contains = function (obj) {
var len = this.length;
while (len--) {
if (this[len] == obj) {
return true;
};
};
return false;
};

//Search
Array.prototype.Search = function (fn) {
for (var fun in this) {
if (fn.call(this, this[fun])) {
return this[fun];
};
};
return null;
};

//Clone
Array.prototype.Clone = function () {
var newArray = [];
for (var property in this) {
newArray[property] = $.isArray(this[property]) ? this[property].Clone() : this[property];
};
return newArray;
};

//Tostring(FormatString)
Date.prototype.Tostring = function (FormatString) {
if (FormatString == undefined) {
FormatString = "yyyy-MM-dd HH:mm:ss";
};
if (FormatString == "UTCDateTime") {
return "/Date(" + (this.AddHour(-8) - "1970-01-01".ToDate()) + "+0800)/";
};
var yyyy = this.getFullYear();
//var yy = yyyy.toString().substring(2, 4);
var MM = this.getMonth() + 1;
if (MM < 1) {
MM = 12;
yyyy -= 1;
};
MM = MM > 9 ? MM : "0" + MM;
var dd = this.getDate();
dd = dd > 9 ? dd : "0" + dd;
var HH = this.getHours();
HH = HH > 9 ? HH : "0" + HH;
var mm = this.getMinutes();
mm = mm > 9 ? mm : "0" + mm;
var ss = this.getSeconds();
ss = ss > 9 ? ss : "0" + ss;
var ms = 000;
ms = ms > 99 ? ms : (ms > 9 ? "0" + ms : "00" + ms);
return FormatString.replace(/yyyy/g, yyyy).replace(/yy/g, yy).replace(/MM/g, MM).replace(/dd/g, dd).replace(/HH/g, HH).replace(/mm/g, mm).replace(/ss/g, ss).replace(/ms/g, ms);
};

//ToUTCDateTime
Date.prototype.ToUTCDateTime = function () {
return (this - "1970-01-01".ToDate());
};

//AddYear
Date.prototype.AddYear = function (Year) {
if (typeof Year == 'number') {
return new Date((this.getFullYear() + Year), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());
};
return "";
};

//AddMonth
Date.prototype.AddMonth = function (Month) {
if (typeof Month == 'number') {
return new Date(this.getFullYear(), (this.getMonth()) + Month, this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());
};
return "";
};

//AddSecond
Date.prototype.AddSecond = function (Second) {
if (typeof Second == 'number') {
return new Date(Date.parse(this) + (1000 * Second));
};
return "";
};

//AddMinute
Date.prototype.AddMinute = function (Minute) {
if (typeof Minute == 'number') {
return new Date(Date.parse(this) + (60000 * Minute));
};
return "";
};

//AddHour
Date.prototype.AddHour = function (Hour) {
if (typeof Hour == 'number') {
return new Date(Date.parse(this) + (3600000 * Hour));
};
return "";
};

//AddDay
Date.prototype.AddDay = function (Day) {
if (typeof Day == 'number') {
return new Date(Date.parse(this) + (86400000 * Day));
};
return "";
};

//AddWeek
Date.prototype.AddWeek = function (Week) {
if (typeof Week == 'number') {
return new Date(Date.parse(this) + (86400000 * 7 * Week));
};
return "";
};

//常用
var jQueryUsed = {
RandomNumber: function (Min, Max) {
return parseInt(Math.random() * (Max - Min + 1) + Min);
}
, RandomString: function (Count, ValueString) {
if (ValueString == undefined || ValueString == null) {
ValueString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
};
var CharArray = ValueString.split('');
if (CharArray.length < Count) {
Count = CharArray.length;
};
ValueString = "";
for (var i = 0; i < Count; i++) {
ValueString += CharArray[jExtension.RandomNumber(0, CharArray.length - 1)];
};
return ValueString;
}
, RandomItem: function (ItemList) {
var ValueString = "";
if (ItemList.length > 0) {
ValueString = ItemList[jExtension.RandomNumber(0, ItemList.length - 1)];
};
return ValueString;
}
, VirtualDirectory: ''
, GetWebSiteRootUrl: function () {
var Root = window.location.protocol + "//" + window.location.host + "/";
VirtualDirectory = jExtension.VirtualDirectory.replace(/\\/g, "/");
if (VirtualDirectory != undefined && VirtualDirectory != null && VirtualDirectory.length > 0) {
var FromatVirtualDirectory = function (VirtualDirectory) {
var StringS = VirtualDirectory.split('');
if (StringS[0] == "/") {
return FromatVirtualDirectory(VirtualDirectory.substring(1));
} else if (StringS[StringS.length - 1] == "/") {
return FromatVirtualDirectory(VirtualDirectory.substring(0, StringS.length - 2));
} else {
return VirtualDirectory;
};
};
VirtualDirectory = FromatVirtualDirectory(VirtualDirectory);
return Root + VirtualDirectory + "/";
} else {
return Root;
};
}
, Request: function (ParameterName) {
var ParameterValue = "";
var Url = window.location.href;
var IndexOfString = "?";
var LastIndex = Url.lastIndexOf(IndexOfString);
if (LastIndex > -1) {
var ParametersString = Url.substring(LastIndex + IndexOfString.length).replace(/(^\s*)|(\s*$)/g, "");
var ParameterS = ParametersString.split('&');
for (var i = 0; i < ParameterS.length; i++) {
var Parameter = ParameterS[i].split('=');
if (Parameter[0] == ParameterName) {
ParameterValue = decodeURIComponent(Parameter[1]);
break;
};
};
};
return ParameterValue;
}
, GetFileName: function (FilePath) {
var FileName = null;
var LastPointIndex = FilePath.lastIndexOf(".");
if (LastPointIndex > 0) {
for (var i = (LastPointIndex - 1); i > -1; i--) {
if ("/\\".indexOf(FilePath.substr(i, 1)) > -1) {
i++;
FileName = FilePath.substr(i, FilePath.length - i)
break;
};
};
};
return FileName;
}
, ObjectToJsonString: function (Object, IsUseCustomFormat) {
switch (Object.prototype.toString.apply(Object)) {
case "[object String]":
if (IsUseCustomFormat && Object.indexOf("/Date(") > -1) {
return "\"" + Object.ToDate().ToString("yyyy-MM-dd HH:mm:ss") + "\"";
} else if (Object.indexOf("/Date(") == 0 && Object.lastIndexOf(")/") == (Object.length - 2) && Object.length == 21) {
return "\"" + Object.replace(/\//g, "\\/") + "\"";
} else {
return "\"" + Object.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function () { var a = arguments[0]; return (a == "\n") ? "\\n" : (a == "\r") ? "\\r" : (a == "\t") ? "\\t" : "" }) + "\"";
};
case "[object Number]":
return Object;
case "[object Boolean]":
return Object;
case "[object Object]":
if (Object == undefined || Object == null) {
return null;
} else {
var JsonS = [];
for (var i in Object) {
JsonS.push("\"" + i + "\":" + jExtension.JsonToString(Object[i]));
};
return '{' + JsonS.join(',') + '}';
};
case "[object Array]":
var JsonS = [];
for (var i = 0; i < Object.length; i++) {
JsonS.push(jExtension.JsonToString(Object[i]));
};
return "[" + JsonS.join(',') + "]";
case "[object Null]":
return null;
case "[object Undefined]":
return null;
case "[object DOMWindow]":
return null;
case "[object global]":
return null;
case "[object Function]":
return Object.toString();
default:
alert(Object.toString() + "JsonToString发现未知类型!" + Object.prototype.toString.apply(Object));
break;
};
}
, JsonStringToJson: function (JsonString, CatchValue) {
var JsonObject;
if (CatchValue != undefined && (JsonString == undefined || JsonString == null)) {
JsonString = CatchValue;
};
try {
JsonObject = eval("(" + JsonString + ")");
} catch (ex) {
JsonObject = JsonString;
};
return JsonObject;
}
, EntityListDistinct: function (EntityList, FieldName) {
var FieldValueArray = [];
var FieldValueDictionary = {};
if (EntityList != null && EntityList.length > 0) {
for (var i = 0; i < EntityList.length; i++) {
var FieldValue = EntityList[i][FieldName];
if (FieldValueDictionary[FieldValue] == undefined) {
FieldValueDictionary[FieldValue] = EntityList[i];
FieldValueArray.push(FieldValue);
};
};
};
return FieldValueArray
}
}

 

posted @ 2013-07-15 11:19  戒一字  阅读(259)  评论(0编辑  收藏  举报