/*操作类封装 */ /*调用方法 如下: * var str= new IStrManipulation();//实例化字符串处理接口 * console.log(str.StrManipulation('StrManipulation',"111sss23123").getLength()); * var convert =new IConvert();//实例化类型转换接口 * console.log(convert.Convert('Convert',"11123123").IsPositiveInteger()); * var date =new IDateTime();//实例化日期接口 * var timestamp3 = 1403058804; * var newDate = new Date(); * newDate.setTime(timestamp3 * 1000); * alert(date.DateTime('DateTime').DateFormat(newDate,"yyyy-MM-dd h:m:s")); * * * */ var Interface = function (name, methods) { if (arguments.length != 2) { throw new Error("接口的长度不等于2"); } this.Name = name; this.Method = []; for (var i = 0; i < methods.length; i++) { if(typeof methods[i]!== 'string') { throw new Error("方法名不是 String 类型"); } this.Method.push(methods[i]); } } Interface.ensureImplement = function (object) { if (arguments.length < 2) { throw new Error("没有接口被实例化"); } for (var i = 1; i < arguments.length; i++) { var interface1 = arguments[i]; if (interface1.constructor !== Interface) { throw new Error("参数不等于接口"); } for (var j = 0; j < interface1.Method.length; j++) { var method = interface1.Method[j]; if (!object[method] || typeof object[method] !== 'function') { throw new Error("实例不能继承接口"); } } } } /*function implements(instance){ for(var i = 1; i < arguments.length; i++) { var interfaceName = arguments[i]; var isFound = false; for(var j = 0; j < instance.implementsInterfaces.length; j++) { if(interfaceName == instance.implementsInterfaces[j]) { isFound = true; break; } } if(!isFound) return false; } return true; }*/ var IBaseUnityFactory =(function(){ }); IBaseUnityFactory.prototype={ CreateBaseFactory : function(model,val){ var obj; switch(model){ case 'StrManipulation': instantiation =new Interface('instantiation', ['getLength','getIndex','getReplace','getCount','getManyStr','getCharAt', 'getToLocaleUpperCase','getToLocaleLowerCase','getAnyUpperCase', 'getFirstUpperCase','getLastUpperCase','getFirstLowerCase','getLastLowerCase', 'getAnyLowerCase','getSplitStr','getSubStr','getSubString','getStrAsc','getStrDesc']); obj=new StrManipulation(val); break; case 'Convert': instantiation =new Interface('instantiation', ['IsNumber','IsPositiveInteger','ConvertToNumber','ConvertToString', 'IsMoney','NumberToFixed','NumberToInteger','StringToParseInt','ConvertToAbs', 'StringToAscii','AsciiToString']); obj=new Convert(val); break; case 'DateTime': instantiation =new Interface('instantiation', ['DateFormat','Now','CompareDate','GetDateDiff']); obj=new DateTime(); break; } //if(implements(instantiation)){ Interface.ensureImplement(obj,instantiation); //} //else{ // console.log("没有接口实例化"); //} return obj; } }; //定义字符串处理抽象类 var IStrManipulation=function(){ }; IStrManipulation.prototype={ StrManipulation:function(model,val){ var IFactory =new IBaseUnityFactory(); var obj= IFactory.CreateBaseFactory(model,val); return obj; } }; var StrManipulation=(function(val){ if(val!=null && val !="" && typeof(val)!="undefined"){ this.value=val; } else{ console.log("传入字符串非法"); } }); StrManipulation.prototype={ //返回字符串的长度 getLength: function(){ try{ return this.value.length; }catch(e){ console.log(e.message); } }, //根据返回指定字符串的索引 getIndex:function(str){ try{ return this.value.indexOf(str); }catch(e){ console.log(e.message); } }, //替换字符串 getReplace:function(str1,str2){ try{ return this.value.replace(str1,str2); }catch(e){ console.log(e.message); } }, //获取字符串中指定的字符出现次数 getCount:function(str){ try{ var index=-1,count=-1; do{ index=this.value.indexOf(str,index+1); count++; } while(index!=-1); return count; }catch(e){ console.log(e.message); } }, //获取字符串最多次数的字符 getManyStr:function(){ try{ var temp=-1; var str=""; for(var i=0;i<this.value.length;i++) { var index=-1,count=-1; do{ index=this.value.indexOf(value[i],index+1); count++; } while(index!=-1){ if(temp<count){ temp=count; str=value[i]; } }; } return str; }catch(e){ console.log(e.message); } }, //根据索引返回字符 getCharAt:function(index){ try{ return this.value.charAt(index); }catch(e){ console.log(e.message); } }, //将字符串转换为大写 getToLocaleUpperCase:function(){ try{ return this.value.toLocaleUpperCase(); }catch(e){ console.log(e.message); } }, //将字符串转换为小写 getToLocaleLowerCase:function(){ try{ return this.value.toLocaleLowerCase(); }catch(e){ console.log(e.message); } }, //首字母大写其余都小写 getFirstUpperCase:function(){ try{ return this.getToLocaleLowerCase().replace(this.getCharAt(0), this.getCharAt(0).toLocaleUpperCase()); }catch(e){ console.log(e.message); } }, //字符串末尾字母大写其余都小写 getLastUpperCase:function(){ try{ return this.getToLocaleLowerCase().replace( this.getCharAt(this.value.length), this.getCharAt(this.value.length).toLocaleUpperCase()); }catch(e){ console.log(e.message); } }, //字符串任意指定位置字符大写 getAnyUpperCase:function(index){ try{ return this.getToLocaleLowerCase().replace( this.getCharAt(index), this.getCharAt(index).toLocaleUpperCase()); }catch(e){ console.log(e.message); } }, //首字母小写其余都大写 getFirstLowerCase:function(){ try{ return this.getToLocaleUpperCase().replace(this.getCharAt(0), this.getCharAt(0).toLocaleLowerCase()); }catch(e){ console.log(e.message); } }, //字符串末尾字母小写其余都大写 getLastLowerCase:function(){ try{ return this.getToLocaleUpperCase().replace( this.getCharAt(this.value.length), this.getCharAt(this.value.length).toLocaleLowerCase()); }catch(e){ console.log(e.message); } }, //字符串任意指定位置字符小写 getAnyLowerCase:function(index){ try{ return this.getToLocaleUpperCase().replace( this.getCharAt(index), this.getCharAt(index).toLocaleLowerCase()); }catch(e){ console.log(e.message); } }, //字符串转换为数组指定长度 getSplitStr:function(separator,length){ try{ switch(arguments.length) { case 1: return this.value.split(separator); case 2: return this.value.split(separator,length); } }catch(e){ console.log(e.message); } }, //字符串截取返回一个从指定位置开始的指定长度的子字符串 getSubStr:function(start,length){ try{ switch(arguments.length) { case 1: return this.value.substr(start); case 2: return this.value.substr(start,length); } }catch(e){ console.log(e.message); } }, //字符串截取提取字符串中介于两个指定下标之间的字符 getSubString:function(start,length){ try{ switch(arguments.length) { case 1: return this.value.substring(start); case 2: return this.value.substring(start,length); } }catch(e){ console.log(e.message); } }, //字符串字母,数字顺序从小到大排列 getStrAsc:function(){ try{ var tempArr=[]; for(var i=0;i<this.value.length;i++){ tempArr.push(this.getCharAt(i)); } return tempArr.sort().join(''); }catch(e){ console.log(e.message); } }, //字符串字母,数字顺序从大到小排列 getStrDesc:function(){ try{ var tempArr=[]; for(var i=0;i<this.value.length;i++){ tempArr.push(this.getCharAt(i)); } return tempArr.sort().reverse().join(''); }catch(e){ console.log(e.message); } } }; //定义类型转换抽象类 var IConvert=function(){ }; IConvert.prototype={ Convert:function(model,val){ var IFactory =new IBaseUnityFactory(); var obj= IFactory.CreateBaseFactory(model,val); return obj; } }; var Convert=(function(val){ if(val!=null && val !="" && typeof(val)!="undefined"){ this.value=val; } else{ console.log("传入字符串非法"); } this.GetRegPositiveInteger = (function (){ return /^[1-9]\d*$/g; }); this.getRegMoney =(function(){ return /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/g; }); }); Convert.prototype={ //判断字符串只包含数字 IsNumber:function(){ try{ return !isNaN(this.value); }catch(e){ console.log(e.message); } }, //判断字符串否是正整数 IsPositiveInteger:function(){ try{ return (this.value.match(this.GetRegPositiveInteger())==this.value) ?true:false; }catch(e){ console.log(e.message); } }, //将字符串转换为数字类型 ConvertToNumber:function(){ try{ if(!this.IsNumber()) { console.log("字符串包含除了数字之外非法字符"); return; } else{ return Number(this.value); } }catch(e){ console.log(e.message); } }, //转换为字符串 ConvertToString:function(){ try{ return this.value.toString(); }catch(e){ console.log(e.message); } }, //判断金钱类型 IsMoney:function(){ try{ return (this.value.match(this.getRegMoney())==this.value) ?true:false; }catch(e){ console.log(e.message); } }, //保留小数 index 小数位数 NumberToFixed:function(index){ try{ return this.ConvertToNumber().toFixed(index); }catch(e){ console.log(e.message); } }, //取整数 NumberToInteger:function(functionName){ try{ switch(functionName) { case 'round'://四舍五入 return Math.round(this.ConvertToNumber()); case 'floor'://小于等于num的整数 return Math.floor(this.ConvertToNumber()); case 'parseInt'://小于等于num的整数,与floor的区别是parseInt参数可以是string类型,如'5abc'返回5 return parseInt(this.ConvertToNumber()); case 'ceil'://大于等于num的整数 return Math.ceil(this.ConvertToNumber()); } } catch(e){ console.log(e.message); } }, //字符串转整数 StringToParseInt : function(){ try{ return parseInt(this.ConvertToString()); }catch(e){ console.log(e.message); } }, //绝对值 ConvertToAbs:function(){ try{ return Math.abs(this.ConvertToNumber()); }catch(e){ console.log(e.message); } }, //string 转 Ascii StringToAscii:function(){ try{ return this.ConvertToString().charCodeAt(); }catch(e){ console.log(e.message); } }, //Ascii 转 string AsciiToString:function(){ try{ return String.fromCharCode(this.ConvertToString()); }catch(e){ console.log(e.message); } } }; //定义日期时间类型抽象类 var IDateTime=function(){ }; IDateTime.prototype={ DateTime:function(model){ var IFactory =new IBaseUnityFactory(); var obj= IFactory.CreateBaseFactory(model); return obj; } }; var DateTime=(function(){ }); DateTime.prototype={ DateFormat:function(date,format){ var v = ""; if (typeof date == "string" || typeof date != "object") { return; } var year = date.getFullYear(); var month = date.getMonth()+1; var day = date.getDate(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); var weekDay = date.getDay(); var ms = date.getMilliseconds(); var weekDayString = ""; if (weekDay == 1) { weekDayString = "星期一"; } else if (weekDay == 2) { weekDayString = "星期二"; } else if (weekDay == 3) { weekDayString = "星期三"; } else if (weekDay == 4) { weekDayString = "星期四"; } else if (weekDay == 5) { weekDayString = "星期五"; } else if (weekDay == 6) { weekDayString = "星期六"; } else if (weekDay == 7) { weekDayString = "星期日"; } v = format; //Year v = v.replace(/yyyy/g, year); v = v.replace(/YYYY/g, year); v = v.replace(/yy/g, (year+"").substring(2,4)); v = v.replace(/YY/g, (year+"").substring(2,4)); //Month var monthStr = ("0"+month); v = v.replace(/MM/g, monthStr.substring(monthStr.length-2)); //Day var dayStr = ("0"+day); v = v.replace(/dd/g, dayStr.substring(dayStr.length-2)); //hour var hourStr = ("0"+hour); v = v.replace(/HH/g, hourStr.substring(hourStr.length-2)); v = v.replace(/hh/g, hourStr.substring(hourStr.length-2)); //minute var minuteStr = ("0"+minute); v = v.replace(/mm/g, minuteStr.substring(minuteStr.length-2)); //Millisecond v = v.replace(/sss/g, ms); v = v.replace(/SSS/g, ms); //second var secondStr = ("0"+second); v = v.replace(/ss/g, secondStr.substring(secondStr.length-2)); v = v.replace(/SS/g, secondStr.substring(secondStr.length-2)); //weekDay v = v.replace(/E/g, weekDayString); return v; }, //获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS” Now:function(){ try{ var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds(); return currentdate; }catch(e){ console.log(e.message); } }, //日期比较 相等为 0 ,大于 为1 ,小于为-1。 CompareDate:function(date1,date2){ try{ if((new Date(date1.replace(/-/g,"\/")))==(new Date(date2.replace(/-/g,"\/")))) { return 0; } else if((new Date(date1.replace(/-/g,"\/")))>(new Date(date2.replace(/-/g,"\/")))) { return 1; } else{ return -1; } }catch(e){ console.log(e.message); } }, //日期之间的时间差 GetDateDiff:function(startTime,endTime,diffType){ //将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式 startTime = startTime.replace(/\-/g, "/"); endTime = endTime.replace(/\-/g, "/"); //将计算间隔类性字符转换为小写 diffType = diffType.toLowerCase(); var sTime =new Date(startTime); //开始时间 var eTime =new Date(endTime); //结束时间 //作为除数的数字 var timeType =1; switch (diffType) { case"second": timeType =1000; break; case"minute": timeType =1000*60; break; case"hour": timeType =1000*3600; break; case"day": timeType =1000*3600*24; break; } return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(timeType)); } };