├── Lgl.js ├── Lgl.min.js └── README.md /Lgl.js: -------------------------------------------------------------------------------- 1 | var Lgl=(function(){ 2 | return{ 3 | //常用判断函数 4 | 5 | isArray:function(arr){ 6 | return Object.prototype.toString.call(arr)==='[object Array]'; 7 | }, 8 | isFunction:function(fn){ 9 | return Object.prototype.toString.call(fn)==='[object Function]'; 10 | }, 11 | isObject:function (x) { return x === Object(x); 12 | }, 13 | 14 | //深拷贝 15 | clone:function(src){ 16 | var clone=src; 17 | // 对于Date引用类型的数据,需要考虑调用构造函数重新构造,直接赋值依然会有引用问题 18 | if(src instanceof Date){ 19 | clone=new Date(src.parse()); 20 | return clone; 21 | } 22 | 23 | if(this.isArray(src)){ 24 | clone=[]; 25 | for(var key in src){ 26 | clone[key]=this.clone(src[key]); 27 | } 28 | return clone; 29 | 30 | } 31 | if(this.isObject(src)){ 32 | clone={}; 33 | for(var key in src){ 34 | if(src.hasOwnProperty(key)){ 35 | clone[key]=this.clone(src[key]); 36 | } 37 | } 38 | return clone; 39 | } 40 | //对于number,string,boolean,null,undefined 41 | return src; 42 | }, 43 | 44 | //对数组去重 45 | uniqArray:function(arr){ 46 | var obj={}; 47 | for(var i=0,len=arr.length;iarr[j]){ 86 | d=arr[j]; 87 | arr[j]=arr[i]; 88 | arr[i]=d; 89 | } 90 | } 91 | } 92 | return arr; 93 | }, 94 | 95 | //快排 96 | quickSort:function(arr){ 97 | if(arr.length<=1){ 98 | return arr; 99 | } 100 | var halfindex=Math.floor(arr.length/2); 101 | var halfvalue=arr[halfindex]; 102 | var left=[],right=[]; 103 | for(var i=0;i@,;:\\\\\\\"\\[\\]\\?=\\{\\}\\/\\u0080-\\uffff]+\x24')) 182 | .test(cookieName); 183 | }, 184 | 185 | setCookie:function(name,value,expiredays){ 186 | if(!isValidCookie(name)){ 187 | return; 188 | } 189 | var exdate={}; 190 | if (expiredays) { 191 | exdate = new Date(); 192 | exdate.setDate(exdate.getDate() + expiredays); 193 | var expires = ';expires=' + exdate.toUTCString(); // toGMTString is deprecated and should no longer be used, it's only there for backwards compatibility, use toUTCString() instead 194 | } 195 | document.cookie = name + '=' + encodeURIComponent(value) + expires; 196 | }, 197 | 198 | getCookie:function(name){ 199 | if (!isValidCookieName(cookieName)) { 200 | return null; 201 | } 202 | 203 | var re = new RegExp(name + '=(.*?)($|;)'); 204 | return re.exec(document.cookie)[1] || null; 205 | }, 206 | 207 | //ajax封装 208 | ajax:function(url,options){ 209 | var xmlhttp; 210 | if (window.XMLHttpRequest) { 211 | xmlhttp = new XMLHttpRequest(); 212 | } 213 | else { //兼容 IE5 IE6 214 | xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 215 | } 216 | 217 | 218 | if (options.data) { 219 | var dataarr = []; 220 | for (var item in options.data) { 221 | dataarr.push(item + '=' + encodeURI(options.data[item])); 222 | } 223 | var data = dataarr.join('&'); 224 | } 225 | 226 | 227 | if (!options.type) { 228 | options.type = 'GET'; 229 | } 230 | options.type = options.type.toUpperCase(); 231 | 232 | 233 | if (options.type === 'GET') { 234 | var myURL = ''; 235 | if (options.data) { 236 | myURL = url + '?' + data; 237 | } 238 | else { 239 | myURL = url; 240 | } 241 | xmlhttp.open('GET', myURL, true); 242 | xmlhttp.send(); 243 | } 244 | else if (options.type === 'POST') { 245 | xmlhttp.open('POST', url, true); 246 | xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 247 | xmlhttp.send(data); 248 | } 249 | 250 | 251 | xmlhttp.onreadystatechange = function () { 252 | if (xmlhttp.readyState === 4) { 253 | if (xmlhttp.status === 200) { 254 | if (options.onsuccess) { 255 | options.onsuccess(xmlhttp.responseText, xmlhttp.responseXML); 256 | } 257 | } 258 | else { 259 | if (options.onfail) { 260 | options.onfail(); 261 | } 262 | } 263 | } 264 | } 265 | } 266 | } 267 | })(); 268 | window.Lgl=Lgl; -------------------------------------------------------------------------------- /Lgl.min.js: -------------------------------------------------------------------------------- 1 | var Lgl=function(){return{isArray:function(arr){return Object.prototype.toString.call(arr)==="[object Array]"},isFunction:function(fn){return Object.prototype.toString.call(fn)==="[object Function]"},isObject:function(x){return x===Object(x)},clone:function(src){var clone=src;if(src instanceof Date){clone=new Date(src.parse());return clone}if(this.isArray(src)){clone=[];for(var key in src){clone[key]=this.clone(src[key])}return clone}if(this.isObject(src)){clone={};for(var key in src){if(src.hasOwnProperty(key)){clone[key]=this.clone(src[key])}}return clone}return src},uniqArray:function(arr){var obj={};for(var i=0,len=arr.length;iarr[j]){d=arr[j];arr[j]=arr[i];arr[i]=d}}}return arr},quickSort:function(arr){if(arr.length<=1){return arr}var halfindex=Math.floor(arr.length/2);var halfvalue=arr[halfindex];var left=[],right=[];for(var i=0;i@,;:\\\\\\"\\[\\]\\?=\\{\\}\\/\\u0080-\\uffff]+$').test(cookieName)},setCookie:function(name,value,expiredays){if(!isValidCookie(name)){return}var exdate={};if(expiredays){exdate=new Date;exdate.setDate(exdate.getDate()+expiredays);var expires=";expires="+exdate.toUTCString()}document.cookie=name+"="+encodeURIComponent(value)+expires},getCookie:function(name){if(!isValidCookieName(cookieName)){return null}var re=new RegExp(name+"=(.*?)($|;)");return re.exec(document.cookie)[1]||null},ajax:function(url,options){var xmlhttp;if(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest}else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")}if(options.data){var dataarr=[];for(var item in options.data){dataarr.push(item+"="+encodeURI(options.data[item]))}var data=dataarr.join("&")}if(!options.type){options.type="GET"}options.type=options.type.toUpperCase();if(options.type==="GET"){var myURL="";if(options.data){myURL=url+"?"+data}else{myURL=url}xmlhttp.open("GET",myURL,true);xmlhttp.send()}else if(options.type==="POST"){xmlhttp.open("POST",url,true);xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xmlhttp.send(data)}xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState===4){if(xmlhttp.status===200){if(options.onsuccess){options.onsuccess(xmlhttp.responseText,xmlhttp.responseXML)}}else{if(options.onfail){options.onfail()}}}}}}}();window.Lgl=Lgl; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # My jsLibrary 2 | 3 | 4 | 5 | ## Introduction 6 | 7 | 包含了js中的常用函数 8 | 9 | ## Method 10 | 11 | 正则判断、排序、类操作、通用事件、ajax等 12 | 13 | 14 | ## 安装 15 | 16 | ``` 17 | npm install jslibrary 18 | 19 | ``` 20 | ## LICENSE 21 | 22 | MIT © [lgl1993](http://github.com/lgl1993) --------------------------------------------------------------------------------